Load More Comments via AJAX
In this tutorial, we will discuss different ways of how you can create paginated comments in WordPress, in every of the mentioned ways we’re going to use AJAX (it means, that the comments pagination, load more button, or infinite scroll will work without page refresh).
This tutorial was first published quite a while ago, so I decided to leave the original screenshots of the Twenty Seventeen default WordPress theme. Of course, today we have block themes available, the latest one at the moment of updating this tutorial is Twenty Twenty-Five.
And here we go, that’s what we’re going to create in this tutorial:
Before going ahead and trying to implement any tips from this tutorial, make sure, that you have the comment settings configured as follows:

Of course, it doesn’t matter also, how many comments you would like to display on a single page.
Step 1. Comments Load More Button
First things first, let’s add a comments load more button to our theme. In many classic WordPress themes, you can easily do it just by replacing the paginate_comments_links() function with the code below. If you can not find this function in your active theme templates, then try to search for something like <ol class="comment-list">...</ol> and add the load more button somewhere after that.
<?php
$cpage = get_query_var( 'cpage' ) ? get_query_var( 'cpage' ) : 1;
if( $cpage > 1 ) {
?>
<div class="rudr-comments-load-more">More comments</div>
<script>
var ajaxurl = '<?php echo admin_url( 'admin-ajax.php' ) ?>',
parent_post_id = '<?php the_ID() ?>',
cpage = <?php echo absint( $cpage ) ?>;
</script>
<?php
}Wait, isn’t the cpage query var equal to the current comment page number?
Yes, but since we have the last comment page displayed first (you remember, we did it in the settings?), now the cpage parameter is equal to the maximum number of comment pages.
So, the condition if( $cpage > 1 ) { means two things at the same time — print the comments load more button and the scripts if:
- there are 2 or more comment pages,
- currently, we are not on the first comment page, which will be displayed as the last one.
If you are looking at the wp_localize_script() direction, please do, but note that get_query_var( 'cpage' ) doesn’t work inside the wp_enqueue_scripts action hook. If you find a simple way to do it, I would appreciate it a lot, if you share your ideas in the comments section, if you have no idea what I’m talking about, a good wp_localize_script() example you can find here.
Step 2. Load More Comments with AJAX and jQuery
I hope you don’t mind if I use jQuery in this example. Maybe writing code in vanilla JavaScript will make someone feel more professional, but in our case, especially if we’re working with the classic WordPress theme, in most of them, jQuery is already included by default, so there is nothing wrong with that.
Let’s create an empty JS file, for example, load-more-comments.js, and put it somewhere in our theme. Don’t forget to consider creating a child WordPress theme, unless you’re working with your own custom theme.
This is the code for the file:
jQuery( function( $ ){
// comments load more button click event
$( '.rudr-comments-load-more' ).click( function( e ){
e.preventDefault(); // in case you decide to use <a> tag
const button = $(this);
// decrease the current comment page value (declared before)
cpage--;
$.ajax({
url : ajaxurl, // AJAX handler URL (declared before)
data : {
'action': 'cloadmore', // wp_ajax_cloadmore
'post_id': parent_post_id, // the current post (declared before)
'cpage' : cpage, // current comment page
},
type : 'POST',
beforeSend : function ( xhr ) {
button.text( 'Loading...' ); // preloader here
},
success : function( data ){
if( data ) {
$( 'ol.comment-list' ).append( data );
// restore the text of the load more button
button.text( 'More comments' );
// if it is the last page, remove the button
if ( 1 == cpage ) {
button.remove();
}
} else {
button.remove();
}
}
});
});
});Just in case you’re not sure, how to include this load-more-comments.js file into your theme, then here you go:
add_action( 'wp_enqueue_scripts', function() {
wp_enqueue_script( 'jquery' );
wp_enqueue_script(
'comments-load-more',
get_stylesheet_directory_uri() . '/load-more-comments.js',
array( 'jquery' ),
'1.0', // file version
true // in the footer
);
} );Once done, please try to click your comments load more button. Does it change the text to “Loading…” at this moment? If yes, I guess everything has been done correctly and we can continue.
If not, I’d recommend you to check the browser console, probably something is not right with your JavaScript code or how you included it into your theme.
Step 3. Retrieve the Next Comments Page in PHP
At this moment, you should know how to use AJAX in WordPress. If you do, the code below will be pretty simple for you.
In any case, I will try to describe everything in detail along the way.
// add_action( 'wp_ajax_{ACTION}', ...
// we passed the action parameter in the jQuery code before
add_action( 'wp_ajax_cloadmore', 'rudr_comments_load_more' );
add_action( 'wp_ajax_nopriv_cloadmore', 'rudr_comments_load_more' );
function rudr_comments_load_more(){
global $post;
$post = get_post( $_POST[ 'post_id' ] );
setup_postdata( $post );
wp_list_comments( array(
'avatar_size' => 100,
'page' => $_POST[ 'cpage' ], // current comments page
'per_page' => get_option( 'comments_per_page' ),
'style' => 'ol', // comments won't be wrapped in this tag and it is awesome!
'short_ping' => true,
// we just copy the params from wp_list_comments() used in our theme
'reply_text' => twentyseventeen_get_svg( array( 'icon' => 'mail-reply' ) ) . __( 'Reply', 'twentyseventeen' ),
) );
die; // don't forget this thing if you don't want "0" to be displayed
}I think it is pretty much it guys. We have just configured the comments load more functionality for the classic WordPress theme, Twenty Seventeen.
Load More Comments in Block Themes
Well, developing a load more comments button in a WordPress block theme is a completely different story. Here we’re going to try to figure out how we can do that.
Let’s activate a block theme right now, for example, Twenty Twenty-Five, and then navigate to Appearance > Editor, then to the Templates. We will need to open a “Single Posts” template.
We will see the following block structure in the “Document Overview”:

Ok, what options do we have here to allow our site visitors to load more comments asynchronously?
To be honest, not many.
First of all, let’s take a look at the “Comments Pagination” block. It may seem similar to the “Pagination” block for posts, but they are completely different because the latter supports the Interactivity API and works without page refresh. So, this option is out. Or we can just wait when this functionality appears in the next WordPress versions.
Basically, the only option we have here is to develop a custom Gutenberg block that will add a comments load more button, kind of similar to a block I developed for loading more posts. What are your thoughts on this, guys? Let me know in the comments below.
Misha Rudrastyh
Hey guys and welcome to my website. For more than 10 years I've been doing my best to share with you some superb WordPress guides and tips for free.
Need some developer help? Contact me
Thanks! That’s a great tutorial but I found some issues (not to blame you, but because of WordPress logic).
WordPress breaks comments per page by strange logic so if you set discussion settings like this, you will have comments in the mixed order in result.
Even in the video, the order of comments is kinda strange. Check out the time of comment published in your video:
1st page: 6.49 am -> 6.50 am
2nd page: 6.48 am -> 8 am
Also, it doesn’t account for a situation when there is only 1 comment on the last (newest) page. So you have 8 comments and you break by 2 so the result is always smooth.
If you had 7 comments and break by 6 you’d only have 1 comment loaded by default (and 6 hidden behind the button).
Hey,
The comments publish dates may look like this because we allow nested comments. And I could move some comments manually on that video 🙃
About your last question – yes, it seems not perfect but that’s how comment paging works in WordPress.
#1 if you don’t want “1 comment only”, you can select “the first page displayed by default” in admin control panel.
#2 the nested comment won’t be weird if we stylize the children comment with indent margin.
Thank you!
Thanks Misha!
I googled for load more comments on button click. Here’s my view:
I and most other admins don’t need pagination, or limiting comment loading to X per click.
With a goal of improving site speed(!), all we really need is a way to load all comments (facebook and site comments) with only on button click. That’s why the large media corporations do exactly this.
Where admins display gravatars, all those calls can be saved for, if and when, the visitor wants to view comments, or to leave one.
So basically, even after reading your article and watching the animated gif above it, I am unsure if this can be achieved with your code – because clearly you had a different goal when coding it, it seems?
But with a goal of improving site speed(!) here’s what is needed:
– page loads only the article at first
– when a visitor clicks button “Leave a comment”, the comment form is loaded below (this can alternatively always be loaded immediately, as it isn’t that much code normally)
– and when a visitor clicks the button “View comments”, ajax(?) loads ON THE SAME URL (no pagination!) all the comments below the article (or if desired, loads them in batches of 20 or whatever, if it is a large number).
Simple requirement! Insanely helpful for page speed! Particularly, when gravatars are used!
Just my view of the whole topic. Based on my experience as a reader of media sites.
Added: So basically,
– find in the active theme the common place where comments start.
– for WordPress, this typically is
<section id="comments">– have ajax(?) load the comments (and possibly commentform) in a container below that!
In a “container”, because already on first pageload of course you want the page footer etc be displayed. The comments just get loaded in a container above that.
Like I said, I can’t find any code that achieves this. Not sure if yours does, haven’t fully understood yours yet.
Hi David,
Yes, you can use my tutorial. But you need additional code that will load the first comments page on button click or on scroll.
Hi! Great tutorial. However, the code seems to not work with comments on pages, just for comments on posts… Do anyone know how the code can edited to work with pages?
Hey Anton,
Could you clarify what exactly doesn’t work and how?
Misha, I think he means this:
wordpress does pages and posts, and on HIS website apparently your code only works on POST type.
I can’t say if it would be the same for me, as said, I haven’t even understood your code yet. Thanks for your reply by the way – even that I didn’t understand though (“additional code”, tell that someone who doesn’t code, lol)… :-)
Give my suggestion a thought, I think you’ll realize that THAT is what is needed – and nothing else. Quite simple then really. Much simpler than what your code and other people’s code aims to achieve. ;-)
Thank for sharing the codes. It works as expected.
However I use custom comment walker and when load more comments, comment lists appear as default.
I tried to edit code in functions.php but no luck.
Here is what I added :
But comment lists still appear as default.
Hi,
Try to add
walkerparameter towp_list_comments()function.Thank you. It worked.
Thanks! You helped me a lot!
Thanks for the tutorial!
In step 1 you can use
dataattribute:echo '<div class="rudr-comments-load-more" data-cpage="' . $cpage . '" data-parent_post_id="' . get_the_ID() . '">...and in javascript:
Thanks so much for your plugin.
The amazing question is..Why you didn’t used any of your codes in your site?
I use 🙃
But only what I’m needed.
sorry but it didn’t work with me, also i got confused with the logic and code structure :), also i know php concept but WordPress making php look bad :(
thank you anyway :)
Well, ask specific questions, maybe I can help :)
Hi! Thanks for the code, it helped me a lot.
I’d like to change this line :
'per_page' => get_option('comments_per_page')into this one :'per_page' => '3', but it doesn’t seem to work. Could you help?Thank you very much,
Clara
Hey Clara,
Are you sure, that it doesn’t work? Because in your case it will display 3 top level comments. If those comments have replied, more comments will be displayed.
Hey,
Thanks for the feedback.
No, it doesn’t work. It does display 3 top level comments, but the 3 last ones, and it doesn’t display the “More comments” link.
It works if I put 3 in the admin discussions page but it makes another problem on my website.. I don’t know how to make this work otherwise.
Thank you for your help,
Clara
Hey,
Well, it is hard to figure it out in comments. I have to look at your website, please contact me and send me website URL by email,
Hello Misha, thank you for you How to.
I followed all steps with attention but when I click ont “More comments”, it’s display “loading…” but nothing happens.
Hey Alex,
Most likely it is the error somewhere in you PHP code.
This is great. Couple notes:
1. You have
parent_post_idas an ajax parameter, butpost_idin the php function.2. In the latest versions (or at least in mine),
.comment-listwas changed to.commentlist. Might be worth double-checking.3. For WooCommerce, you need to include the callback, such as:
Loved your work. Thank you!
WordPress breaks comments per pages by strange logic so if you set discussion setting like this, you will have comments in the mixed order in result.
Hey Lisa,
Agreed with you. I’m trying to figure it out the best way right now.
Hi Misha,
great tutorial, I really appreciate your work and effort to help others, thank you for that!
I was wondering whether this could be used for woocommerce product reviews as well.
Hey Barbara,
Sorry, I always remove unformatted code. Please check this comment.
And maybe you need to pass
'type' => 'review'parameter as well.Hi i want to know that is there any code of yours that will do ajax pagination for comments, if you have please let me know, Thank you!
Hi,
No, at this moment I do not have this codes 🙃
It was great. It helped me a lot with the page loading speed because there were a lot of comments and it was making it very slow.
I find myself returning to your website day after day. The design is so appealing and the content is always top-notch. Keep it up!
Thank you!!
How would you go about loading comments without a button, just updating the comment list once there is a new comment?
Hi, why are you using “POST” instead of “GET” in your code.