Skip to content

Instantly share code, notes, and snippets.

@guytzhak
Created January 2, 2018 21:24
Show Gist options
  • Save guytzhak/c6e8b71aa43d743c6925ed399f021f19 to your computer and use it in GitHub Desktop.
Save guytzhak/c6e8b71aa43d743c6925ed399f021f19 to your computer and use it in GitHub Desktop.
Wordpress Ajax Call
add_action( 'wp_ajax_wordpress_ajax_action_name', 'wordpress_ajax_action_name' );
add_action( 'wp_ajax_nopriv_wordpress_ajax_action_name', 'wordpress_ajax_action_name' );
function wordpress_ajax_action_name(){
echo 'test :)';
wp_die();
}
wp_enqueue_script( 'myscripts', get_stylesheet_directory_uri() . '/assets/js/app.min.js', array('jquery'), '1.0.0', true );
wp_localize_script( 'myscripts', 'ajaxurl', admin_url( 'admin-ajax.php' ) );
$( 'body' ).on( 'event_ajax', function(){
jQuery.ajax({
url: ajax_url,
type: 'post',
data: {
action: 'wordpress_ajax_action_name',
},
success: function (response) {
console.log(response);
},
error: function (errorThrown) {
console.log(errorThrown);
}
});
} );
@baldawayash15
Copy link

Where do we need to add ajaxurl?

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment