0

The plugin is activated.

The rest of the plugin works fine, but when I load the page, nothing happens. I also view the source and the script is not in the footer as it should be.

I tested this with a variety of other methods, but I'm completely missing what I'm doing wrong.

In my plugin I have:

if (!class_exists('AC')) {

    class AC
    {
         function __construct()
         {
             //...other things that work...
             add_action('wp_enqueue_scripts', array($this, 'enqueueScripts'));
         }
        function enqueueScripts()
        {
            wp_enqueue_script(
                'ac-ajax-js',
                 '/wp-content/plugins/ac/js/track.js',
                array(),
                '1.0',
                true
            );
        }
    }
    $AC = new AC();
}

In the js file I have:

alert("Plugin Loaded!");

1 Answer 1

1

Check your source path if your code calls the right path. Also, you could define a constant variable that stores your plugin path, and use this on all your enqueues functions. You could also use the built-in function, plugin_dir_url( __FILE__ ) , of WordPress that gets the plugin URL.

example.

define( 'YOUR_PLUGIN_URL', plugin_dir_url( __FILE__ ) );

add this code to your plugin main function before the line of methods that calls all your resources and classes then use this to point as the path of your file. on your example (I presume, this file is on your (AC) plugin directory), this could be

wp_enqueue_script(
   'ac-ajax-js',
   YOUR_PLUGIN_URL . '/ac/js/track.js',
   array(),
   '1.0',
   true
);
Sign up to request clarification or add additional context in comments.

3 Comments

> add this code to your plugin main function before the line of methods that calls all your resources < Turns out it was the order in which things were being loaded. I moved the wp_enqueue_script to the top of the __construct function and it worked right away. Thanks!!
You're welcome! By the way, did you move the whole wp_enqueue_script outside the class AC? I think the problem why it is not working before is because you did not create an instance of the class "AC".
Oops- I guess I didn't show the part where I instantiated the class. I'll edit the original post. However, what I did specifically was move the wp_enqueue_script() to the very top of the constructor. So it seems those other add_action()'s interfered with it.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.