I’ve a customized plugin with the next code
add_action( 'init', 'my_script_enqueuer' );
operate my_script_enqueuer() {
wp_register_script( "gift_card_redeem", WP_PLUGIN_URL.'/plugin-folder/gift_card_redeem.js', array('jquery') );
wp_localize_script( 'gift_card_redeem', 'myAjax', array( 'ajaxurl' => admin_url( 'admin-ajax.php' )));
wp_enqueue_script( 'jquery' );
wp_enqueue_script( 'gift_card_redeem' );
}
add_action("wp_ajax_gift_card_redeem", "gift_card_redeem");
operate gift_card_redeem(){
if(!empty($_SERVER['HTTP_X_REQUESTED_WITH']) && strtolower($_SERVER['HTTP_X_REQUESTED_WITH']) == 'xmlhttprequest') {
error_log("test !empty");
$outcome['type'] = "success";
$outcome = json_encode($outcome);
echo $outcome;
}
else {
error_log("test else");
header("Location: ".$_SERVER["HTTP_REFERER"]);
}
die();
}
and a js file within the plugin referred to as gift_card_redeem.js
jQuery(doc).prepared(operate () {
jQuery(".redeem_gift_card").click on(operate (e) {
e.preventDefault();
jQuery.ajax({
sort: "post",
dataType: "json",
url: myAjax.ajaxurl,
knowledge: { motion: "redeem_gift_card" },
success: operate (response) {
if (response.sort == "success") {
jQuery("#test").html(response);
} else {
alert("something broke");
}
},
});
});
});
and my php and html on the form-checkout web page is as follows
<?php
$hyperlink = admin_url('admin-ajax.php?motion=gift_card_redeem');
echo '<a category="redeem_gift_card" href="' . $link . '" >take a look at</a>';
?>
<h1 id="test">Check</h1>
I get a 400 unhealthy request error for admin-ajax.php and I’m not fairly positive why. Any assist can be enormously appreciated.
Thanks