Skip to content

Instantly share code, notes, and snippets.

@dianjuar
Last active March 4, 2017 06:01
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save dianjuar/9e9903ec7e909832a839c5f52e4a829d to your computer and use it in GitHub Desktop.
Save dianjuar/9e9903ec7e909832a839c5f52e4a829d to your computer and use it in GitHub Desktop.
A simple WP plugin to know how AJAX works on WordPress
/**
* @param {string} security_nonce
* Code of the Nonce. Security Stuff
*/
jQuery(function($)
{
// The call trigered
$('#publish').click(function(event)
{
var day = $('#jj').val();
event.preventDefault();
//---------------- ajax call ----------------
$.ajax(
{
url: ajaxurl,
type: 'POST',
dataType: 'json',
data:
{
//the WP actions that is going to receive this
action: 'ajax_action_example',
//security stuff
security_nonce: security_nonce,
// Custom data
day: day
},
//timeout: 30000,
success: function( response )
{
if(response.success)
{
console.log( 'succ: ', response );
$('#publish').unbind('click');
$('#publish').trigger('click');
}
else
console.log( 'fail: ', response );
},
// Conection Error
error: function( error )
{
console.log( 'Conection error: ', error );
}
});
//---------------- ajax call ----------------
});
});
<?php
/*
Plugin Name: Ajax Example
Description: A simple WP plugin to know how AJAX works on WordPress
Author: Diego Juliao
Version: 0.1
Author URI: https://about.me/dianjuar
*/
if( !class_exists('ajax_example') )
{
define( 'AJAXEX_PLUGIN_DIR', plugin_dir_path( __FILE__ ) );
define( 'AJAXEX_PLUGIN_URL', plugin_dir_url( __FILE__ ) );
define('AJAXEX_PLUGIN_DIRNAME', plugin_basename(dirname(__FILE__)));
define( 'AJAXEX', 'ajax_example' );
/**
* A very simple example to demonstrate how AJAX calls works.
*
* The logic is, the user can publish a post only the 4th days of every month
*/
class ajax_example
{
function __construct()
{
# Enqueue the JS scripts
add_action( 'admin_enqueue_scripts',
array(&$this, 'enqueueScripts'));
# Ajax listener, teacher crate a payment request
add_action('wp_ajax_ajax_action_example',
array(&$this, 'ajax_request_receiver') );
}
/**
* Enqueue the JavaScript files
*/
public function enqueueScripts()
{
#---------------WHERE TO PUT THE SCRIPTS ON---------------
# Here you will specify where you want to enqueue the js file to make the ajax request
global $current_screen;
# Only when the user is viewing the owlo_paid_to_teacher screen
if( $current_screen->post_type !== 'post' )
return;
#---------------WHERE TO PUT THE SCRIPTS OFF --------------
wp_enqueue_script( 'ajax_request_script',
AJAXEX_PLUGIN_URL.'ajax_example.js',
array( 'jquery' ),
'0.0',
true );
# Send the parameters to the script
# Security stuff.
$security_nonce = wp_create_nonce( IS_NONCE );
wp_localize_script( 'ajax_request_script',
'security_nonce',
$security_nonce );
}
/**
* Function to attend the ajax call
*/
public function ajax_request_receiver()
{
# ------------------------- SECURITY VALIDATION ON -----------------------------
/**
* Checks if the nonce of the ajax is valid
*
* First parameters comes for the wp_nonce_field string
* and the second one comes for the ajax parameter
*/
if( !check_ajax_referer(IS_NONCE, 'security_nonce') )
return wp_send_json_error( 'Invalid Nonce' );
/**
* If the actual user can not update the post
* GET OUT OF HERE
*/
if( !current_user_can('publish_posts') )
return wp_send_json_error( 'You are not allow to do this' );
# ------------------------- SECURITY VALIDATION OFF -----------------------------
# ------------------------ BUSINESS LOGIC ON ---------------------------------------
#Create the request
$day = $_POST['day'];
if( $day == '4' )
wp_send_json_success('Success');
else
wp_send_json_error('Only can publish on 4th, not on ' . $day);
# ------------------------ BUSINESS LOGIC OFF --------------------------------------
}
}# End class
$ajax_example = new ajax_example();
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment