如何为要使用的每个短代码分别使用wp\\u enqueue\\u脚本?
function shortA()
{
wp_enqueue_script(\'member\', plugins_url(\'js/member.js\', __FILE__), array(\'jquery\'), \'1.0\', true);
}add_shortcode(\'shortA\',\'shortA\');
function shortB()
{
wp_enqueue_script(\'nonmember\', plugins_url(\'js/nonmember.js\', __FILE__), array(\'jquery\'), \'1.0\', true);
}add_shortcode(\'shortB\',\'shortB\');
我试过这个,但运气不好:
Enqueue Scripts / Styles when shortcode is presentfunction this_is_my_shortcode(){
add_action(\'wp_enqueue_scripts\', function() {
wp_register_script(\'per-pas-belanja-online\', plugins_url(\'js/per-pas-belanja-online.js\', __FILE__), array(\'jquery\'), \'1.0.0\');
wp_enqueue_script(\'per-pas-belanja-online\');
});
return \'<div id="poppedout">Blah</div>\';
}
add_shortcode(\'bubba\', \'this_is_my_shortcode\');
SO网友:Welcher
你很接近!
您可以通过wp_enqueue_scripts
正常钩住它们,然后将它们放入短代码回调中。这是告诉WordPress有关脚本的信息,然后允许您仅在需要时将其排队:
add_action( \'wp_enqueue_scripts\' \'register_all_my_scripts\' );
function register_all_my_scripts() {
wp_register_script( \'per-pas-belanja-online\', plugins_url(\'js/per-pas-belanja-online.js\', __FILE__), array(\'jquery\'), \'1.0.0\' );
}
function this_is_my_shortcode(){
wp_enqueue_script(\'per-pas-belanja-online\');
return \'<div id="poppedout">Blah</div>\';
}
add_shortcode(\'bubba\', \'this_is_my_shortcode\');
希望有帮助!