do_action( "wp_ajax_nopriv_{$_REQUEST[‘action’]}" )

Fires non-authenticated Ajax actions for logged-out users.


Description Description

The dynamic portion of the hook name, $_REQUEST['action'], refers to the name of the Ajax action callback being fired.


Source Source

File: wp-admin/admin-ajax.php

View on Trac


Top ↑

Changelog Changelog

Changelog
Version Description
2.8.0 Introduced.

Top ↑

User Contributed Notes User Contributed Notes

  1. Skip to note 1 content
    Contributed by Aurovrata Venet

    Used to hook an ajax action to server function when a user is not logged into the front-end,

    add_action('wp_ajax_nopriv_front-end-action', 'server_function');
    function server_function(){
    	//validate a nonce, and other $_POST data elements and execute some code.
    	wp_json_send($data); //use wp_json_send to return some data to the client.
    	wp_die(); //use wp_die() once you have completed your execution.
    }
    

    On the front-end you would have some js script executed based on some event,

    //some event on the page would trigget the following 
    var data = {
      'action' : 'front-end-action',
      //some more data to pass to the server...
    };
    //send the data to the server using the jQuery post method
    //note for the ajaxurl, its best to set it dynamically using the wp_localize_script function, see the link below
     $.post(ajaxurl, data, function(response) {
      //handle the response from the server
    });
    

    for more info, checkout https://developer.wordpress.org/plugins/javascript/ajax/

You must log in before being able to contribute a note or feedback.