Skip to content

Instantly share code, notes, and snippets.

View garyc40's full-sized avatar

Gary Cao garyc40

View GitHub Profile
@garyc40
garyc40 / ajax-bad-practice.php
Created October 3, 2012 14:53
WordPress AJAX bad practices
require_once( "../../../../wp-config.php" );
// or require_once( "../../../../wp-load.php" );
@garyc40
garyc40 / wp_localize_script.php
Created October 3, 2012 14:56
wp_localize_script
wp_localize_script( $handle, $namespace, $variable_array );
@garyc40
garyc40 / enqueue-localize-js.php
Created October 3, 2012 14:58
Enqueue & localize JS in WordPress
// embed the javascript file that makes the AJAX request
wp_enqueue_script( 'my-ajax-request', plugin_dir_url( __FILE__ ) . 'js/ajax.js', array( 'jquery' ) );
// declare the URL to the file that handles the AJAX request (wp-admin/admin-ajax.php)
wp_localize_script( 'my-ajax-request', 'MyAjax', array( 'ajaxurl' => admin_url( 'admin-ajax.php' ) ) );
@garyc40
garyc40 / enqueue-output.html
Created October 3, 2012 15:00
wp_enqueue_script() and wp_localize_script() HTML output
<script type="text/javascript" src="http://example.com/wordpress/wp-content/plugins/myajax/js/ajax.js"></script>
<script type="text/javascript">
/* <![CDATA[ */
var MyAjax = {
ajaxurl: "http://example.com/wordpress/wp-admin/admin-ajax.php"
};
/* ]]> */
</script>
@garyc40
garyc40 / ajax-hooks.php
Created October 3, 2012 15:15
WordPress AJAX hooks
// this hook is fired if the current viewer is not logged in
do_action( 'wp_ajax_nopriv_' . $_REQUEST['action'] );
// if logged in:
do_action( 'wp_ajax_' . $_POST['action'] );
@garyc40
garyc40 / ajax-submit.js
Created October 3, 2012 15:16
WordPress ajax submit code
jQuery.post(
// see tip #1 for how we declare global javascript variables
MyAjax.ajaxurl,
{
// here we declare the parameters to send along with the request
// this means the following action hooks will be fired:
// wp_ajax_nopriv_myajax-submit and wp_ajax_myajax-submit
action : 'myajax-submit',
// other parameters can be added along with "action"
@garyc40
garyc40 / ajax-hooks.php
Created October 3, 2012 15:27
Hooking into WordPress AJAX actions
// if both logged in and not logged in users can send this AJAX request,
// add both of these actions, otherwise add only the appropriate one
add_action( 'wp_ajax_nopriv_myajax-submit', 'myajax_submit' );
add_action( 'wp_ajax_myajax-submit', 'myajax_submit' );
function myajax_submit() {
// get the submitted parameters
$postID = $_POST['postID'];
// generate the response
@garyc40
garyc40 / ajax-nonce.php
Created October 3, 2012 15:30
WordPress AJAX & Nonces
// embed the javascript file that makes the AJAX request
wp_enqueue_script( 'my-ajax-request', plugin_dir_url( __FILE__ ) . 'js/ajax.js', array( 'jquery' ) );
wp_localize_script( 'my-ajax-request', 'MyAjax', array(
// URL to wp-admin/admin-ajax.php to process the request
'ajaxurl' => admin_url( 'admin-ajax.php' ),
// generate a nonce with a unique ID "myajax-post-comment-nonce"
// so that you can check it later when an AJAX request is sent
'postCommentNonce' => wp_create_nonce( 'myajax-post-comment-nonce' ),
@garyc40
garyc40 / ajax-nonce.js
Created October 3, 2012 15:31
WordPress AJAX & Nonces pt.2
jQuery.post(
MyAjax.ajaxurl,
{
action : 'myajax-submit',
postID : MyAjax.postID,
// send the nonce along with the request
postCommentNonce : MyAjax.postCommentNonce
},
function( response ) {
@garyc40
garyc40 / ajax-nonce.php
Created October 3, 2012 15:31
WordPress AJAX check nonces
// if both logged in and not logged in users can send this AJAX request,
// add both of these actions, otherwise add only the appropriate one
add_action( 'wp_ajax_nopriv_myajax-submit', 'myajax_submit' );
add_action( 'wp_ajax_myajax-submit', 'myajax_submit' );
function myajax_submit() {
$nonce = $_POST['postCommentNonce'];
// check to see if the submitted nonce matches with the