admin管理员组文章数量:1130349
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/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');
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/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');
Share
Improve this question
asked Jan 7, 2013 at 22:50
Travis PflanzTravis Pflanz
1,9535 gold badges31 silver badges57 bronze badges
4 Answers
Reset to default 3EDIT: 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');
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__)
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');
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');
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/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');
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/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');
Share
Improve this question
asked Jan 7, 2013 at 22:50
Travis PflanzTravis Pflanz
1,9535 gold badges31 silver badges57 bronze badges
4 Answers
Reset to default 3EDIT: 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');
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__)
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');
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');
本文标签: Can the wpplugins (Must Use Plugins) URL be targeted for use in functionsphp
版权声明:本文标题:Can the wp-plugins (Must Use Plugins) URL be targeted for use in functions.php? 内容由热心网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:https://it.en369.cn/questions/1749150053a2323660.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。


发表评论