I am writing a plugin that displays custom home page that is in fact only one post with comments. The first time home page is displayed by the initial call by php. Then user can click a menu button to get another post. This is handled by ajax. So the whole page does not reload.
Getting and displaying other posts via ajax works fine till I ‘refresh’ the page via ajax. Then the comment reply
link doesn’t work any more.
I implemented [AJAXified commenting system][1] but I guess it has nothing to do with reply link.
Reply link before ajax is used looks like
<a onclick="return addComment.moveForm("div-comment-11", "11", "respond", "8")"
href="/?replytocom=11#respond" class="comment-reply-link">Reply</a>
When the same post is loaded via ajax the reply button looks like
<a onclick="return addComment.moveForm("div-comment-11", "11", "respond", "8")"
href="/wp-admin/admin-ajax.php?replytocom=11#respond" class="comment-reply-link">Reply</a>
The difference is in the href
attribute. The second (ajax) one got wp-admin/admin-ajax.php
part that does not supposed to be there.
I use one php function to generate html for both the fist time post is being displayed and for ajax call too.
Part of the function looks like
function my_get_comments($post_id, $number_of_comments){
//Gather comments for a specific post
$comments = get_comments(array(
'number' => $number_of_comments,
'post_id' => $post_id,
'status' => 'approve' //Change this to the type of comments to be displayed
));
ob_start(); //workaround how to capture the output from comments_number() function
comments_number( 'no comments', 'one comment', '% comments' );
$capture_comments_number = ob_get_clean();
$return_string ='<div id="comments_container">'
. '<style type="text/css">.hidden{display:none;}</style>'
. '<div id="comments_number">'
. $capture_comments_number
. '<a class="comment_switch"> Show / Hide Comments</a>'
. '</div>'
. '<div class="comments">'
. '<ol class="commentlist">';
//Display / format the list of comments
ob_start(); //workaround how to capture the output from wp_list_comments() function
wp_list_comments(array(
'reverse_top_level' => false //Show the latest comments at the top of the list
), $comments);
$capture_wp_list_comments = ob_get_clean();
return $return_string;
}
ob_start(); //workaround how to capture the output from comment_form() function
comment_form("", $post_id);
$capture_comment_form = ob_get_clean();
$return_string = $return_string
. $capture_comment_form
. '</div>'
. '</div>';
I need to fix the href
part of the reply link?
I realized that
comment-reply.js
is not loaded and it fixed the issue.Could someone explain? I have no idea what happened.
So the fix was adding
wp_enqueue_script( 'comment-reply' );
to my plugin code.