Tutorial by Examples

functions.php: // We add the action twice, once for logged in users and once for non logged in users. add_action( 'wp_ajax_my_action', 'my_action_callback' ); add_action( 'wp_ajax_nopriv_my_action', 'my_action_callback' ); // Enqueue the script on the front end. add_action( 'wp_enqueue_script...
functions.php //Localize the AJAX URL and Nonce add_action('wp_enqueue_scripts', 'example_localize_ajax'); function example_localize_ajax(){ wp_localize_script('jquery', 'ajax', array( 'url' => admin_url('admin-ajax.php'), 'nonce' => wp_create_nonce('example_ajax_nonc...
functions.php: function rm_init_js() { wp_enqueue_script( 'custom-ajax-script', get_template_directory_uri() . '/js/ajax.js', array( 'jquery', 'wp-util' ), '1.0', true ); // pass custom variables to JS wp_localize_script( 'custom-ajax-script', 'BEJS', array( 'action' => ...
You can copy and paste this whole plugin to try it. The class skeleton is used from here. class-oop-ajax.cpp <?php /** * The plugin bootstrap file * * This file is read by WordPress to generate the plugin information in the plugin * Dashboard. This file defines a function that start...

Page 1 of 1