Created
April 29, 2019 22:29
-
-
Save raiden808/e9f42c04bbdebe89df3067733a368e6e to your computer and use it in GitHub Desktop.
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
function cb_related_ajax() | |
{ | |
$sample_ajax = $_POST['hello']; | |
echo $sample_ajax; | |
wp_die(); | |
} | |
add_action('wp_ajax_cb_related_ajax','cb_related_ajax'); | |
add_action('wp_ajax_nopriv_cb_related_ajax','cb_related_ajax'); | |
add_action('wp_head',function(){ | |
?> | |
<script type="text/javascript"> | |
document.addEventListener('DOMContentLoaded', function() { | |
// code | |
[].forEach.call(document.querySelectorAll('.cb_relate_btn'), function(el) { | |
el.addEventListener('click', function() { | |
// code… | |
cb_related_ajax(); | |
}) | |
}) | |
//ajax | |
function cb_related_ajax(){ | |
var ajax_url = window.location.protocol + "//" + window.location.host+"/wp-admin/admin-ajax.php"; | |
var request = new XMLHttpRequest(); | |
request.open('POST', ajax_url, true); | |
request.setRequestHeader('Content-Type', 'application/x-www-form-urlencoded; charset=UTF-8'); | |
request.onload = function () { | |
if (this.status >= 200 && this.status < 400) { | |
alert("test"); | |
}else { | |
// Response error | |
} | |
} | |
request.onerror = function() { | |
// Connection error | |
}; | |
request.send('action=cb_related_ajax'); | |
}; | |
}); | |
</script> | |
<?php | |
}); | |
function cb_related_display_callback(){ | |
?> | |
<button class="cb_relate_btn">Click Me</button> | |
<?php | |
} | |
add_shortcode( 'cb_related_display', 'cb_related_display_callback' ); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment