wordpress ajax url php code example
Example: wordpress ajax url
<?php
add_action( 'wp_footer', 'my_ajax_without_file' );
function my_ajax_without_file() { ?>
<script type="text/javascript" >
jQuery(document).ready(function($) {
ajaxurl = '<?php echo admin_url( 'admin-ajax.php' ) ?>';
var data = {
'action': 'frontend_action_without_file',
'variable_name': "Some value"
};
jQuery.ajax({
url: ajaxurl,
type: 'POST',
data: data,
success: function (response) {
console.log(response);
}
});
});
</script>
<?php
}
add_action("wp_ajax_frontend_action_without_file" , "frontend_action_without_file");
add_action("wp_ajax_nopriv_frontend_action_without_file" , "frontend_action_without_file");
function frontend_action_without_file(){
echo json_encode($_POST);
wp_die();
}
?>