400 bad request on admin-ajax.php only using wp_enqueue_scripts action hook

Question

I’ve been working on ajax lately. The tutorials you find on the net are all very similar and quite easy to implement.
But I always get a bad request 400 on my ajax-admin.php file.

After a long and intensive search, I have now found out that’s because of the time of integration.

If I use the init action hook to initialize script and wp_localize_script, everything works fine. So the code itself must be correct.

my-page-test-functions.php

function ajax_login_init(){
   wp_register_script('ajax-login-script',get_stylesheet_directory_uri().'/js/ajax-login-script.js',array('jquery'));
   wp_enqueue_script('ajax-login-script');
   wp_localize_script('ajax-login-script','ajax_login_object',array('ajaxurl' => admin_url('admin-ajax.php'),'redirecturl' => 'REDIRECT_URL_HERE','loadingmessage' => __('Sending user info, please wait...')));
   add_action('wp_ajax_nopriv_ajaxlogin','ajax_login');
}

if(!is_user_logged_in()){
   add_action('init','ajax_login_init');
}

function ajax_login(){
    //nonce-field is created on page
    check_ajax_referer('ajax-login-nonce','security');
    //CODE
    die();
}

But if I use e.g. wp_enqeue_scripts action hook I always get the bad request.

if(!is_user_logged_in()){
    add_action('wp_enqueue_scripts','ajax_login_init');
}

The problem with this is:

I would like to have the functions in an extra php file and load them only if they are needed on a particular page. For this I need, for example is_page().
But is_page() works at the earliest when I hook the function with the include into the parse_query action hook:

functions.php

function sw18_page_specific_functions(){
    if(is_page('page-test')){
        include_once dirname(__FILE__).'/includes/my-page-test-functions.php';
    }
}

add_action('parse_query','sw18_page_specific_functions');

So then functions hooked to init hook in my-page-test-functions.php file does not triggered, I suppose, because init comes before parse_query.

Is there a best practices to organize this, so it works? Or how can I fix the admin-ajax.php bad request when using the wp_enqeue_scripts action hook?

0
Sin 2 months 0 Answers 7 views 0

Leave an answer