I have a shortcode in Wordpress which I want to call it with jQuery with the click of a button. I read a few websites and tutorials, but I cannot figure exactly what happen.
I put this on function.php:
add_action( 'init', function() {
ps_register_shortcode_ajax( 'ps_get_survey_form', 'ps_get_survey_form' );
} );
function ps_register_shortcode_ajax( $callable, $action ) {
if ( empty( $_POST['action'] ) || $_POST['action'] != $action )
return;
call_user_func( $callable );
}
function ps_get_survey_form() {
echo do_shortcode( '[dopbsp id=6 lang=el]' );
die();
}
And this on the page-template.php I use:
<button id="testonclick" onclick="test()"></button>
<div id="testresults"></div>
<script>
function test() {
jQuery.ajax({
url: "http://localhost/myweb" + "/wp-admin/admin-ajax.php",
data : {action: 'ps_get_survey_form'},
success: function(results){
jQuery("#testresults").html(results)
},
error: function(errorThrown){console.log(errorThrown);}
});// end of ajax
}
</script>
However, the results is 0. Any idea what happened wrong?
Aucun commentaire:
Enregistrer un commentaire