Calling A Function After A Click On A Button In Wordpress (dev-plugin)
This is my code and I wanna do what is commented near line 73 (//HOW CAN I CALL FUNCTION post_type_search_callback HERE???). The results need to be in the same page, below the 'for
Solution 1:
php function
Note you are declaring all your functions inside initialize.....better to declare functions outside of other functions and call them as you need them.
add_action( 'wp_ajax_post_type_search_callback', array( 'OwnersTriplify', 'my_action_post_type_search_callback' ) );
add_action( 'wp_ajax_post_type_search_callback', array( 'OwnersTriplify', 'my_action_post_type_search_callback' ) );
functionpost_type_search_callback() {
$data= $_POST['variable'];
$output= 'i was returned with ajax';
//need to echo output and exit hereecho$output;
exit();
}
jQuery You need to use the word jQuery or define $ for jQuery to work in wordpress.
jQuery(document).ready(function() { // wait for page to finish loading jQuery("#button1").click(function () {
jQuery.ajax({
type: "POST",
url: "/wp-admin/admin-ajax.php",
data: {
action: 'post_type_search_callback',
variable: 45// enter in anyname here instead of variable, you will need to catch this value using $_POST['variable'] in your php function.
},
success: function (output) {
console.log(output);
}
});
});
});
further reading:
Post a Comment for "Calling A Function After A Click On A Button In Wordpress (dev-plugin)"