0

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 ?

1 Answer 1

1

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

main in this case.

5
  • awesome sir thanks ... one more thing if you have time ... i have changed this and now on click i am getting this error prntscr.com/nwbity
    – Flutterer
    Jun 1, 2019 at 15:34
  • 1
    Have you registered your ajax handler with wp_ajax_ and wp_ajax_nopriv_ action hooks, too? Jun 1, 2019 at 15:38
  • you are awesome sir :) thanks for the very quick help ...
    – Flutterer
    Jun 1, 2019 at 15:40
  • E.g. add_action( 'wp_ajax_ajax_pagination', 'my_ajax_pagination_function' ); Jun 1, 2019 at 15:40
  • yes sir all is ok now , moving towards next thing :)
    – Flutterer
    Jun 1, 2019 at 15:40

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.