Are 'wp_ajax' and 'wp_ajax_nopriv' exclusive to authetnicated and non-authenticated users?
Basically what I'm asking is, is wp_ajax_nopriv
exclusive to non-logged-in users?
Will a wp_ajax_nopriv
action fire if a user is logged in?
ajax actions
add a comment |
Basically what I'm asking is, is wp_ajax_nopriv
exclusive to non-logged-in users?
Will a wp_ajax_nopriv
action fire if a user is logged in?
ajax actions
add a comment |
Basically what I'm asking is, is wp_ajax_nopriv
exclusive to non-logged-in users?
Will a wp_ajax_nopriv
action fire if a user is logged in?
ajax actions
Basically what I'm asking is, is wp_ajax_nopriv
exclusive to non-logged-in users?
Will a wp_ajax_nopriv
action fire if a user is logged in?
ajax actions
ajax actions
asked 47 mins ago
SwenSwen
5392824
5392824
add a comment |
add a comment |
2 Answers
2
active
oldest
votes
Looking at the WordPress source code, I'd say that wp_ajax_nopriv_*
fires only if you're not logged in, and wp_ajax_*
fires otherwise.
Here's the relevant bit, in admin-ajax.php
, lines 85-115 in version 5.0.3:
if ( is_user_logged_in() ) {
// If no action is registered, return a Bad Request response.
if ( ! has_action( 'wp_ajax_' . $_REQUEST['action'] ) ) {
wp_die( '0', 400 );
}
/**
* Fires authenticated Ajax actions for logged-in users.
*
* The dynamic portion of the hook name, `$_REQUEST['action']`,
* refers to the name of the Ajax action callback being fired.
*
* @since 2.1.0
*/
do_action( 'wp_ajax_' . $_REQUEST['action'] );
} else {
// If no action is registered, return a Bad Request response.
if ( ! has_action( 'wp_ajax_nopriv_' . $_REQUEST['action'] ) ) {
wp_die( '0', 400 );
}
/**
* Fires non-authenticated Ajax actions for logged-out users.
*
* The dynamic portion of the hook name, `$_REQUEST['action']`,
* refers to the name of the Ajax action callback being fired.
*
* @since 2.8.0
*/
do_action( 'wp_ajax_nopriv_' . $_REQUEST['action'] );
}
So, if you're logged in (ie, is_user_logged_in()
is true
), it runs the wp_ajax_*
action(s), otherwise it runs the wp_ajax_nopriv_*
actions.
If you want the same action run regardless whether your user is logged in or not, I'd recommend you hook to both wp_ajax_*
and wp_ajax_nopriv_*
.
add a comment |
As per wp_ajax_(action)
codex:
This hook is functionally the same as
wp_ajax_(action)
, except the
"nopriv" variant is used for handling AJAX requests from
unauthenticated users, i.e. whenis_user_logged_in()
returns false.
is_user_logged_in()
determines whether the current visitor is a logged in user. It will return true
if user is logged in, false
if not logged in.
add a comment |
Your Answer
StackExchange.ready(function() {
var channelOptions = {
tags: "".split(" "),
id: "110"
};
initTagRenderer("".split(" "), "".split(" "), channelOptions);
StackExchange.using("externalEditor", function() {
// Have to fire editor after snippets, if snippets enabled
if (StackExchange.settings.snippets.snippetsEnabled) {
StackExchange.using("snippets", function() {
createEditor();
});
}
else {
createEditor();
}
});
function createEditor() {
StackExchange.prepareEditor({
heartbeatType: 'answer',
autoActivateHeartbeat: false,
convertImagesToLinks: false,
noModals: true,
showLowRepImageUploadWarning: true,
reputationToPostImages: null,
bindNavPrevention: true,
postfix: "",
imageUploader: {
brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
allowUrls: true
},
onDemand: true,
discardSelector: ".discard-answer"
,immediatelyShowMarkdownHelp:true
});
}
});
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fwordpress.stackexchange.com%2fquestions%2f326387%2fare-wp-ajax-and-wp-ajax-nopriv-exclusive-to-authetnicated-and-non-authentica%23new-answer', 'question_page');
}
);
Post as a guest
Required, but never shown
2 Answers
2
active
oldest
votes
2 Answers
2
active
oldest
votes
active
oldest
votes
active
oldest
votes
Looking at the WordPress source code, I'd say that wp_ajax_nopriv_*
fires only if you're not logged in, and wp_ajax_*
fires otherwise.
Here's the relevant bit, in admin-ajax.php
, lines 85-115 in version 5.0.3:
if ( is_user_logged_in() ) {
// If no action is registered, return a Bad Request response.
if ( ! has_action( 'wp_ajax_' . $_REQUEST['action'] ) ) {
wp_die( '0', 400 );
}
/**
* Fires authenticated Ajax actions for logged-in users.
*
* The dynamic portion of the hook name, `$_REQUEST['action']`,
* refers to the name of the Ajax action callback being fired.
*
* @since 2.1.0
*/
do_action( 'wp_ajax_' . $_REQUEST['action'] );
} else {
// If no action is registered, return a Bad Request response.
if ( ! has_action( 'wp_ajax_nopriv_' . $_REQUEST['action'] ) ) {
wp_die( '0', 400 );
}
/**
* Fires non-authenticated Ajax actions for logged-out users.
*
* The dynamic portion of the hook name, `$_REQUEST['action']`,
* refers to the name of the Ajax action callback being fired.
*
* @since 2.8.0
*/
do_action( 'wp_ajax_nopriv_' . $_REQUEST['action'] );
}
So, if you're logged in (ie, is_user_logged_in()
is true
), it runs the wp_ajax_*
action(s), otherwise it runs the wp_ajax_nopriv_*
actions.
If you want the same action run regardless whether your user is logged in or not, I'd recommend you hook to both wp_ajax_*
and wp_ajax_nopriv_*
.
add a comment |
Looking at the WordPress source code, I'd say that wp_ajax_nopriv_*
fires only if you're not logged in, and wp_ajax_*
fires otherwise.
Here's the relevant bit, in admin-ajax.php
, lines 85-115 in version 5.0.3:
if ( is_user_logged_in() ) {
// If no action is registered, return a Bad Request response.
if ( ! has_action( 'wp_ajax_' . $_REQUEST['action'] ) ) {
wp_die( '0', 400 );
}
/**
* Fires authenticated Ajax actions for logged-in users.
*
* The dynamic portion of the hook name, `$_REQUEST['action']`,
* refers to the name of the Ajax action callback being fired.
*
* @since 2.1.0
*/
do_action( 'wp_ajax_' . $_REQUEST['action'] );
} else {
// If no action is registered, return a Bad Request response.
if ( ! has_action( 'wp_ajax_nopriv_' . $_REQUEST['action'] ) ) {
wp_die( '0', 400 );
}
/**
* Fires non-authenticated Ajax actions for logged-out users.
*
* The dynamic portion of the hook name, `$_REQUEST['action']`,
* refers to the name of the Ajax action callback being fired.
*
* @since 2.8.0
*/
do_action( 'wp_ajax_nopriv_' . $_REQUEST['action'] );
}
So, if you're logged in (ie, is_user_logged_in()
is true
), it runs the wp_ajax_*
action(s), otherwise it runs the wp_ajax_nopriv_*
actions.
If you want the same action run regardless whether your user is logged in or not, I'd recommend you hook to both wp_ajax_*
and wp_ajax_nopriv_*
.
add a comment |
Looking at the WordPress source code, I'd say that wp_ajax_nopriv_*
fires only if you're not logged in, and wp_ajax_*
fires otherwise.
Here's the relevant bit, in admin-ajax.php
, lines 85-115 in version 5.0.3:
if ( is_user_logged_in() ) {
// If no action is registered, return a Bad Request response.
if ( ! has_action( 'wp_ajax_' . $_REQUEST['action'] ) ) {
wp_die( '0', 400 );
}
/**
* Fires authenticated Ajax actions for logged-in users.
*
* The dynamic portion of the hook name, `$_REQUEST['action']`,
* refers to the name of the Ajax action callback being fired.
*
* @since 2.1.0
*/
do_action( 'wp_ajax_' . $_REQUEST['action'] );
} else {
// If no action is registered, return a Bad Request response.
if ( ! has_action( 'wp_ajax_nopriv_' . $_REQUEST['action'] ) ) {
wp_die( '0', 400 );
}
/**
* Fires non-authenticated Ajax actions for logged-out users.
*
* The dynamic portion of the hook name, `$_REQUEST['action']`,
* refers to the name of the Ajax action callback being fired.
*
* @since 2.8.0
*/
do_action( 'wp_ajax_nopriv_' . $_REQUEST['action'] );
}
So, if you're logged in (ie, is_user_logged_in()
is true
), it runs the wp_ajax_*
action(s), otherwise it runs the wp_ajax_nopriv_*
actions.
If you want the same action run regardless whether your user is logged in or not, I'd recommend you hook to both wp_ajax_*
and wp_ajax_nopriv_*
.
Looking at the WordPress source code, I'd say that wp_ajax_nopriv_*
fires only if you're not logged in, and wp_ajax_*
fires otherwise.
Here's the relevant bit, in admin-ajax.php
, lines 85-115 in version 5.0.3:
if ( is_user_logged_in() ) {
// If no action is registered, return a Bad Request response.
if ( ! has_action( 'wp_ajax_' . $_REQUEST['action'] ) ) {
wp_die( '0', 400 );
}
/**
* Fires authenticated Ajax actions for logged-in users.
*
* The dynamic portion of the hook name, `$_REQUEST['action']`,
* refers to the name of the Ajax action callback being fired.
*
* @since 2.1.0
*/
do_action( 'wp_ajax_' . $_REQUEST['action'] );
} else {
// If no action is registered, return a Bad Request response.
if ( ! has_action( 'wp_ajax_nopriv_' . $_REQUEST['action'] ) ) {
wp_die( '0', 400 );
}
/**
* Fires non-authenticated Ajax actions for logged-out users.
*
* The dynamic portion of the hook name, `$_REQUEST['action']`,
* refers to the name of the Ajax action callback being fired.
*
* @since 2.8.0
*/
do_action( 'wp_ajax_nopriv_' . $_REQUEST['action'] );
}
So, if you're logged in (ie, is_user_logged_in()
is true
), it runs the wp_ajax_*
action(s), otherwise it runs the wp_ajax_nopriv_*
actions.
If you want the same action run regardless whether your user is logged in or not, I'd recommend you hook to both wp_ajax_*
and wp_ajax_nopriv_*
.
answered 36 mins ago
Pat JPat J
6,57311831
6,57311831
add a comment |
add a comment |
As per wp_ajax_(action)
codex:
This hook is functionally the same as
wp_ajax_(action)
, except the
"nopriv" variant is used for handling AJAX requests from
unauthenticated users, i.e. whenis_user_logged_in()
returns false.
is_user_logged_in()
determines whether the current visitor is a logged in user. It will return true
if user is logged in, false
if not logged in.
add a comment |
As per wp_ajax_(action)
codex:
This hook is functionally the same as
wp_ajax_(action)
, except the
"nopriv" variant is used for handling AJAX requests from
unauthenticated users, i.e. whenis_user_logged_in()
returns false.
is_user_logged_in()
determines whether the current visitor is a logged in user. It will return true
if user is logged in, false
if not logged in.
add a comment |
As per wp_ajax_(action)
codex:
This hook is functionally the same as
wp_ajax_(action)
, except the
"nopriv" variant is used for handling AJAX requests from
unauthenticated users, i.e. whenis_user_logged_in()
returns false.
is_user_logged_in()
determines whether the current visitor is a logged in user. It will return true
if user is logged in, false
if not logged in.
As per wp_ajax_(action)
codex:
This hook is functionally the same as
wp_ajax_(action)
, except the
"nopriv" variant is used for handling AJAX requests from
unauthenticated users, i.e. whenis_user_logged_in()
returns false.
is_user_logged_in()
determines whether the current visitor is a logged in user. It will return true
if user is logged in, false
if not logged in.
answered 35 mins ago
Kashif RafiqueKashif Rafique
2039
2039
add a comment |
add a comment |
Thanks for contributing an answer to WordPress Development Stack Exchange!
- Please be sure to answer the question. Provide details and share your research!
But avoid …
- Asking for help, clarification, or responding to other answers.
- Making statements based on opinion; back them up with references or personal experience.
To learn more, see our tips on writing great answers.
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fwordpress.stackexchange.com%2fquestions%2f326387%2fare-wp-ajax-and-wp-ajax-nopriv-exclusive-to-authetnicated-and-non-authentica%23new-answer', 'question_page');
}
);
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown