Load custom field value into div with AJAX

Firstly, a POST request is the wrong type of request to send. As stated here, POST: Submits data to be processed to a specified resource You should be using GET, which Requests data from a specified resource The next problem is that in your callback function you are checking for $_POST[“pid”], but you are not … Read more

AJAX Implementation

First, enqueue your script correctly, in functions.php: function my_enqueue_scripts() { wp_enqueue_script(‘jquery’); } add_action(‘wp_enqueue_scripts’, ‘my_enqueue_scripts’); Then, Authenticated users only (see https://codex.wordpress.org/Plugin_API/Action_Reference/wp_ajax_nopriv_(action)) (can also be in functions.php): function fetchData(){ // Not needed anymore, as you manage the case through the `add_action` method // if ( ! is_user_logged_in() ) { // return; // } // this can be … Read more

Is there a hook that fires before an ajax call?

If you look into this file wp-admin/admin-ajax.php where the wp_ajax_ actions are called, you will find that the admin_init action is being called before it. Here is how to return a different json object based on if the user is admin or not: add_action( ‘wp_ajax_testing’, ‘my_ajax’ ); add_action( ‘admin_init’, ‘my_ajax_checker’, 10, 2); function my_ajax_checker() { … Read more