📜  wp_ajax_nopriv (1)

📅  最后修改于: 2023-12-03 14:48:34.459000             🧑  作者: Mango

WP_AJAX_NOPRIV

WP_AJAX_NOPRIV is a function in WordPress that allows developers to create AJAX actions that can be accessed by both logged in and non-logged in users. This function is a variation of the wp_ajax function, but specifically designed for non-logged in users as well.

Parameters

  • Action (string) (required): The name of the AJAX action to be called.
  • Function (callback) (required): The function that will be called when the AJAX action is triggered.
  • Nonce (bool) (optional): Whether or not to include a nonce in the request. Default is true.

Usage

  1. Define an AJAX action using WP_AJAX_NOPRIV:
add_action( 'wp_ajax_nopriv_my_action', 'my_callback_function' );

function my_callback_function() {
    // Do something
}
  1. Call the AJAX action in your JavaScript file:
jQuery.ajax({
    type: 'POST',
    url: ajaxurl,
    data: {
        action: 'my_action',
        // Add any additional data here
    },
    success: function(response){
        // Handle response data here
    }
});

Markdown Code Snippet

# WP_AJAX_NOPRIV

WP_AJAX_NOPRIV is a function in WordPress that allows developers to create AJAX actions that can be accessed by both logged in and non-logged in users. This function is a variation of the wp_ajax function, but specifically designed for non-logged in users as well.

## Parameters
- **Action (string) (required):** The name of the AJAX action to be called.
- **Function (callback) (required):** The function that will be called when the AJAX action is triggered.
- **Nonce (bool) (optional):** Whether or not to include a nonce in the request. Default is true.

## Usage
1. Define an AJAX action using WP_AJAX_NOPRIV:

add_action( 'wp_ajax_nopriv_my_action', 'my_callback_function' );

function my_callback_function() { // Do something }

2. Call the AJAX action in your JavaScript file:

jQuery.ajax({ type: 'POST', url: ajaxurl, data: { action: 'my_action', // Add any additional data here }, success: function(response){ // Handle response data here } });