Quantcast
Viewing latest article 9
Browse Latest Browse All 10

wordpress:Can the wp-plugins (Must Use Plugins) URL be targeted for use in functions.php?

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:

1
2
3
4
5
6
7
8
9
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');

Answers

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 Image may be NSFW.
Clik here to view.
:)

1
2
3
4
5
6
7
8
9
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.

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

1
2
3
4
5
6
7
8
9
10
11
12
13
    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');

Source


Viewing latest article 9
Browse Latest Browse All 10

Trending Articles