Get posts by category via ajax

Add the category ID to category list items <div class=”category-block” data-cid=”{ID-OF-CAT}”>, you will need it in the ajax request. You must also include action in the request and display received data. Here you can read more about AJAX in WP. blog.js (function($) { $(document).ready(function() { var cat_buttons = $(“.category-block”); cat_buttons.on( ‘click’, function(event){ // event.preventDefault(); var … Read more

How can I run two AJAX requests simultaneously in WordPress?

Eureka! So I ended up just adding in a second ajax call into the function. For some reason it’s returning successful but not with a 200 response. Anyway, it’s working. If anyone knows why it’s returning an unknown response (besides 200 or 201) please share. Thanks! function get_posts($params) { $.ajax({ url: psc.ajax_url, data: { action: … Read more

Ajax Comment Upvotes – Votes don’t always register

Please try this and inform me what you got in the console. and sure console.log the type add_action( ‘wp_ajax_comment_meta_update_vote’, ‘comment_meta_update_vote’ ); function comment_meta_update_vote( ) { check_ajax_referer( ‘upvote_nonce’ ); $comment_id = $_POST[‘comment_id’]; $vote_score = get_comment_meta($comment_id , ‘vote’, true); $vote_score++; $updated = update_comment_meta($comment_id , ‘vote’, $vote_score ); if($updated){ $msg = ‘success’; }else{ $msg = ‘failed’; } $response … Read more

WordPress custom post type post item data – show/fetch only on click with Javascript dynamically?

I think you should be able to do this: HTML: <a class=”view_post_details” href=”#” post_id=”<?php the_ID(); ?>”>View Post Details</a> JS: $(“.view_post_detals”).click(function () { var id_post = $(this).attr(‘post_id’); $.ajax({ type: ‘POST’, url: ‘<?php echo admin_url(‘admin-ajax.php’); ?>’, data: { ‘post_id’: id_post, ‘action’: ‘f711_get_post_content’ //this is the name of the AJAX method called in WordPress }, success: function (result) … Read more

use with class file into wp_ajax hook

Use include (yourclass.php) only if the file yourclass.php is on the same folder. Otherwise you will need to put the full path to your file or use a variable for the plugin path: public $plugin_path; $this->plugin_path = plugin_dir_path( dirname( dirname( FILE ) ) ); and include the file using plugin_path.