Script not included with wp_enqueue_scripts

2019-09-14 15:27发布

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);
}

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;
})

When this is clicked:

<a id="button_shake" class="button_sub_chooser" '.$woomps_ajax_url.'>VElg denne</a>

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?

2条回答
Anthone
2楼-- · 2019-09-14 16:00

Can you please try this plugin_dir_url(__FILE__) insted of plugins_url().

add_action( 'wp_enqueue_scripts', 'woomps_ajax_frontend' );

function woomps_ajax_frontend() {

    wp_enqueue_script( 'woomps_sub_slider', plugin_dir_url(__FILE__). 'js/woomps-ajax-frontend.js' , array('jquery'), '1.0', true );

    $parameters =  array(
        'ajax_url' => admin_url( 'admin-ajax.php' ),
        );
    wp_localize_script( 'woomps_sub_slider', 'subpost', $parameters);


}
查看更多
Deceive 欺骗
3楼-- · 2019-09-14 16:16

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');

If I put the require_once ('includes/woomps-ajax-sub-chooser.php'); outside this function woomps_scripts it got included.

So it seems I cant do wp_enqueue_scripts two times down.

查看更多
登录 后发表回答