WordPress : main different between wp_ajax and wp_ajax_nopriv

Sometime ago I’ve faced the issue with AJAX in wordpress that always returns 0 so i share these artical that help the beginner to work with ajax in wordpres.

wp_ajax and wp_ajax_nopriv are actions of wordpress cms.

wp_ajax_

This hook allows you to create custom handlers for your own custom AJAX requests. The wp_ajax_ hook follows the format “wp_ajax_$youraction”, where $youraction is your AJAX request’s ‘action’ property.

The wp_ajax_ hook only fires for logged-in users. If you need to also listen for Ajax requests that don’t come from logged-in users.

wp_ajax_nopriv_

This hook is functionally the same as wp_ajax_(action), however it is used to handle AJAX requests on the front-end for unauthenticated users, i.e. when is_user_logged_in() returns false. Unlike wp_ajax_(action) the ajaxurl javascript global property will not be automatically defined and must be included manually or by using wp_localize_script() with admin_url( ‘admin-ajax.php’ ) as the data

REF : https://codex.wordpress.org/Plugin_API/Action_Reference/wp_ajax_(action)
https://codex.wordpress.org/Plugin_API/Action_Reference/wp_ajax_nopriv_(action)

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s