This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
require_once( "../../../../wp-config.php" ); | |
// or require_once( "../../../../wp-load.php" ); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
wp_localize_script( $handle, $namespace, $variable_array ); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// 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' ) ) ); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<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> |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// 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'] ); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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" |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// 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 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// 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' ), |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
jQuery.post( | |
MyAjax.ajaxurl, | |
{ | |
action : 'myajax-submit', | |
postID : MyAjax.postID, | |
// send the nonce along with the request | |
postCommentNonce : MyAjax.postCommentNonce | |
}, | |
function( response ) { |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// 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 |
OlderNewer