среда, 2 ноября 2016 г.

AJAX IN WORDPRESS - Simple Example - 2 steps!

No more words needed to explain it.
I made a best simple solution - without any plugin!

Take it and use this simple code.
* Please share and like.


YOURACTION_NAME - 3 places to update
POST_VAR_1  (optional) - 2 place to update

1. JS

jQuery(document).ready( function($) {

  $.ajax({
        url: "/wp-admin/admin-ajax.php",
        data: { action:'YOURACTION_NAME', POST_VAR_1 : 1 },
        type: 'POST',
success: function( data ) {
alert( data ); // do whatever you want
}
})

});



2. ADD TO FUNCTIONS.PHP


function implement_ajax() {
if(isset($_POST['POST_VAR_1']))
{
 echo  ' DATA IS '. time();  // send whatever you need
die();
}
}
add_action('wp_ajax_YOURACTION_NAME', 'implement_ajax');
add_action('wp_ajax_nopriv_YOURACTION_NAME', 'implement_ajax');  //for users that are not logged in.

Комментариев нет:

Отправить комментарий