javascript - How to call the php function from the jQuery in Wordpress
one text
Solution:
I tried using ajax and it's working. I don't know it's the best way or not but it's solved my issue.
I was using the jquery-3.5.1.slim.min.js
so I change it because I was getting the error in the console $.ajax is not a function
.
I tried below ajax code
$output.='<script src="https://ajax.googleapis.com/ajax/libs/jquery/3.5.1/jquery.min.js" crossorigin="anonymous"></script><script>
(function($) { // ready handler
$("#catDropdown").change(function() {
$.ajax({
url: "/wp-admin/admin-ajax.php",
type: "post",
data: { action: "latestBlogView", keyword: $("#catDropdown").val() },
success: function(data) {
$("#latestblogs").html(data);
}
});
});
})(jQuery);</script>';
This is my function
add_action('wp_ajax_nopriv_latestBlogView', 'latestBlogView');
add_action('wp_ajax_latestBlogView', 'latestBlogView');
function latestBlogView($atts){
echo "hello"; // if you want to display the variable output on screen then use return $postData;
}
Source