Question

I am initializing wp_localize_script after enqueueing scripts like that

wp_enqueue_script('main',get_template_directory_uri().'/js/mains.js','','1.1',true);
    wp_localize_script( 'ajax-pagination', 'ajaxpagination', array(
        'ajaxurl' => admin_url( 'admin-ajax.php' )
    ));

but i am unable to get it in jquery file/code jquery code is

(function($) {
    $(document).on( 'click', '.page-numbers', function( event ) {
        event.preventDefault();
        $.ajax({
            url: ajaxpagination.ajaxurl,
            type: 'post',
            data: {
                action: 'ajax_pagination'
            },
            success: function( result ) {
                alert( result );
            }
        })
    })
})(jQuery);

on clicking i am seeing the error ajaxpagination is not defined , what can be the possible solutions for this ?

Was it helpful?

Solution

Change the first param of wp_localize_script to same as the handle of your main script file.

main in this case.

Licensed under: CC-BY-SA with attribution
Not affiliated with wordpress.stackexchange
scroll top