Skip to content

Instantly share code, notes, and snippets.

View garyc40's full-sized avatar

Gary Cao garyc40

View GitHub Profile
@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 / wp_localize_script.php
Created October 3, 2012 14:56
wp_localize_script
wp_localize_script( $handle, $namespace, $variable_array );
@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" );