1

I'm creating a plugin that always enqueues a script in the footer. I also have a function that if called would enqueue an additional script that depends on the other one. Here is the basic format of my plugin...

<?php
class MyPlugin {
    public function __construct() {
        add_action('wp_enqueue_scripts', array($this, 'enqueue_scripts'));
    }

    public function enqueue_scripts() {
        wp_enqueue_script('myscript', plugins_url('js/myscript.js', __FILE__), array('jquery'), null, true);
    }

    public function api() {
        add_action('wp_enqueue_scripts', array($this, 'api_scripts'));
    }

    public function api_scripts() {
        wp_enqueue_script('api', plugins_url('js/api.js', __FILE__), array('myscript'), null, true);
    }
}
$_GLOBALS['myplugin'] = new MyPlugin();

In my theme's functions.php file I would like to know when I can make a call to enqueue the script. I've tried attaching to plugins_loaded, but the script is not called.

<?php
function myfunction() {
    global $myplugin;
    $myplugin->api();
}
add_action('plugins_loaded', 'myfunction');
2
  • Why would you want to do this?
    – vancoder
    Jul 2, 2013 at 23:50
  • I tried to simplify the example, but actually the api function takes some parameters and localizes them. This is actually for Marketo Muchkin. The plugin has a settings screen that you add your app id and secret key. It then enqueues the tracking code for Munchkin on your pages. The api portion of the plugin would create the ability to generate the javascript code needed to pass form data (which requires the tracking code to have been called already) from another plugin or from a theme.
    – livearoha
    Jul 3, 2013 at 0:37

1 Answer 1

2

The function wp_enqueue_scripts-- with an 's'-- is used as a callback to wp_head. All that the function wp_enqueue_scripts does is fire the wp_enqueue_scripts action. So the last time that you would be able to use the action wp_enqueue_scripts is before the wp_enqueue_scripts callback on the wp_head hook.

In terms of theme templates, that means you need to hook it in before get_header, or on a hook that runs early enough in the hook sequence fired as a consequence.

Couple of notes:

  1. It is $GLOBAL not $_GLOBALS-- inconsistent, I know.
  2. It works perfectly fine if not hooked to plugins_loaded. That is, just this in the theme's functions.php: global $myplugin; $myplugin->api();. I can't say that I know why the hooked version doesn't work. I'd guess it is simply too early but would have to do some research to workout the mechanics. after_setup_theme works and so does wp_loaded
4
  • That makes sense. The issue I guess I am having is that the global plugin object does not appear to be available at that early. I tried hooking into 'wp' for example. I think I need to come up with an alternative way of doing this.
    – livearoha
    Jul 3, 2013 at 16:32
  • It should be available. Your plugins will load before the theme.
    – s_ha_dum
    Jul 3, 2013 at 16:48
  • The $GLOBALS was the issue, I needed to remove the underscore. Everything is working as expected now.
    – livearoha
    Jul 9, 2013 at 19:40
  • I would argue that you can enqueue scripts later than get_header() assuming that you want scripts displayed in the footer. Jun 9, 2017 at 19:49

Your Answer

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

Not the answer you're looking for? Browse other questions tagged or ask your own question.