Created
January 2, 2018 21:24
-
-
Save guytzhak/c6e8b71aa43d743c6925ed399f021f19 to your computer and use it in GitHub Desktop.
Wordpress Ajax Call
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
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(); | |
} |
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_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' ) ); |
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
$( '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); | |
} | |
}); | |
} ); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Where do we need to add ajaxurl?