脚本不包含在wp_enqueue_scripts中 [英] Script not included with wp_enqueue_scripts
问题描述
我学会了通过以下方式包含脚本.
I have learned to include scripts the following way.
add_action( 'wp_enqueue_scripts', 'woomps_ajax_frontend' );
function woomps_ajax_frontend() {
wp_enqueue_script( 'woomps_sub_slider', plugins_url( '/js/woomps-ajax-frontend.js', dirname(__FILE__)) , array('jquery'), '1.0', true );
$parameters = array(
'ajax_url' => admin_url( 'admin-ajax.php' ),
);
wp_localize_script( 'woomps_sub_slider', 'subpost', $parameters);
}
这应该触发我的jQuery:
This should fire of my jQuery:
jQuery( document ).on( 'click', '.button_sub_chooser', function() {
alert("test");
jQuery.ajax({
url : subpost.ajax_url,
type : 'post',
data : {
action : 'woomps_update_subscription_chooser',
},
success : function( response ) {
jQuery('#button_sub_chooser').effect( "bounce", "slow" );
}
});
return false;
})
单击时:
<a id="button_shake" class="button_sub_chooser" '.$woomps_ajax_url.'>VElg denne</a>
但是wp_enqueue_scripts不会触发jQuery.如果我只是像这样调用函数woomps_ajax_frontend()
,它将触发.为什么当像函数一样调用而不通过wp_enqueue_scripts时,它能起作用?
But wp_enqueue_scripts does not fire of the jQuery. If i just call the function like this woomps_ajax_frontend()
it will fire. Why does it work when called like a function and not through wp_enqueue_scripts?
推荐答案
将PHP代码放入一个包含的(required_once)文件中,该文件已通过以下方式入队.
The PHP code was put in an included (required_once) file which i had enqueue the following way.
function woomps_scripts() {
require_once ('includes/woomps-ajax-sub-chooser.php');
} //END woomps_scripts
add_action('wp_enqueue_scripts','woomps_scripts');
如果将require_once ('includes/woomps-ajax-sub-chooser.php');
放在此function woomps_scripts
之外,则会包含在内.
If I put the require_once ('includes/woomps-ajax-sub-chooser.php');
outside this function woomps_scripts
it got included.
所以看来我不能两次做wp_enqueue_scripts.
So it seems I cant do wp_enqueue_scripts two times down.
这篇关于脚本不包含在wp_enqueue_scripts中的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!