Wordpress AjaxAjax Wordpress
Next, create a PHP feature to process the AJAX query.
Failure to specify an operation will cause admin-ajax.php to terminate and returns 0. You' ll need to include some detail, such as debugging and whether the query came from the right place (with check_ajax_referer()), but hopefully the above example will be enough to launch you with your own Ajax administrative side plug-in.
So if you want it to be triggered for both visitor and user logins on the frontend, you can do this: add_action('wp_ajax_my_action','my_action'); add_action('wp_ajax_nopriv_my_action','my_action'); hint 2: Both frontend and backend use admin-ajax queries. php thus is_admin() always returns real in your actions handler file.
If you load your Ajax scripts handler for the frontend and backend selective and use the is_admin() feature, your wp_ajax_(action) and wp_ajax_nopriv_(action) hook MUST be inside the is_admin() ====real part. If an Ajax request is linked to either wp_ajax_ or wp_ajax_nopriv_ action, it will be performed in the WP Admin environment. Check thoroughly the operations you perform in your source file because unauthorized or visitor access can cause increased privilege queries for which they may not be eligible.
add_action ('wp_ajax_my_backend_action','my_backend_action'); Here the Ajax actions set to call the PHP functions my_frontend_action_callback() for all user. Ajax' operation, named ma_backend_action, triggers the PHP callback operation ma_backend_action_callback() for registered user only. Plug-ins and designs that include contents via Ajax must initiate the post-load events on document.body after contents have been included.
For this reason, be careful when debugging Ajax, as any PHP message or message you return may cause the results to be confusingly parsed, or may cause your script to fail.