2

Can the wp-plugins (Must Use Plugins) URL be targeted for use in functions.php? I have a mu-functions.php file in the mu-plugins folder. One function calls a javascript file (alerts.js) located in example.com/wp-content/mu-plugins/js/. How can I target the mu-plugins folder in my function?

Currently, I am using get_site_url() . '/wp-content/mu-plugins/js/alerts.js',

Although not completely relevant to the question, this is the complete function used to call the javascript file:

function load_my_alerts(){
      wp_register_script( 
        'my_alerts', 
        get_site_url() . '/wp-content/mu-plugins/js/alerts.js', 
        array( 'jquery' )
    );
    wp_enqueue_script( 'my_alerts' );
}
add_action('admin_enqueue_scripts', 'load_my_alerts');
2

EDIT: this solution is not a best-practice. Please use the solution submitted by Nathan below!

Use the WPMU_PLUGIN_DIR and WPMU_PLUGIN_URL constants :)

function load_my_alerts(){
    wp_register_script( 
        'my_alerts', 
        WPMU_PLUGIN_URL . '/js/alerts.js', 
        array( 'jquery' )
    );
    wp_enqueue_script( 'my_alerts' );
}
add_action('admin_enqueue_scripts', 'load_my_alerts');
  • Thanks. I swear I used that. Maybe I missed the leading slash. – Travis Pflanz Jan 7 '13 at 23:39
  • Please see my answer below, neither this nor the referenced answer is correct. – shahar Dec 2 '18 at 6:39
3

FYI, Nathan's example only works for regular plugins, not "Must Use Plugins". To make it work for MU plugins, you need to pass the invoking file:

plugins_url('/path/to/whatever', __FILE__)
1

It is not good practice to use constants. For this functionality one should ALWAYS use the plugins_url() function seen here in the codex.

    function load_my_alerts(){

        wp_register_script(
            'my_alerts',
            plugins_url('js/alerts.js'),
            array( 'jquery' )
        );

        wp_enqueue_script( 'my_alerts' );

    }

    add_action('admin_enqueue_scripts', 'load_my_alerts');
  • 1
    this won't give you the mu-plugins url when used in functions.php which is in the theme. – shahar Nov 19 '18 at 21:43
  • 1
    @shahar yes it will. Read the description in the codex linked above. – Nathan Powell Nov 26 '18 at 16:22
  • your example is missing the $path parameter for plugins_url() which is necessary to return the mu-plugins directory when used outside of an mu-plugin. See my answer below for a correct working example. – shahar Nov 27 '18 at 6:50
  • 1
    @NathanPowell looking at the source, I think @shahar may be correct - this function is a lot dumber than one would hope. It looks like it only uses the MU path if the second argument is present and starts with the MU directory filepath. Stupidly, it seems that the "best practice" might be to use your MU-plugin-directory-relative filepath as the first argument and the WPMU_PLUGIN_DIR constant as (or prepending) the second argument. A little ridiculous. – bosco Dec 4 '18 at 19:58
0

When using plugins_url() outside of an mu-plugin, you need to specify the full path to the plugin that is in the mu-plugins directory. For example, when using it in a theme's functions.php file.

Below is a correct working version, where the second parameter of plugins_url() is a full path to the plugin in the mu-plugins directory.

Please see the codex for more info.

function load_my_plugin_script(){

    wp_register_script(
        'my_plugin_script',
        plugins_url('my_plugin.js', '/wp/wp-content/mu-plugins/my_plugin')
    );

    wp_enqueue_script( 'my_plugin_script' );

}

add_action('admin_enqueue_scripts', 'load_my_plugin_script');

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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