I'm trying to figure out a way to not redirect users to the single post when adding a threaded comment on any post on the "Home" Posts page.
What I have now:
• My Home page shows latest posts (say 10 as an example) and uses index.php
instead of archives.php
.
• Threaded comments are enabled in Dashboard>Discussion (User must be logged in, if it matters).
• Under each post on the Home page, all comments and a comment reply box are shown. When a user adds a new comment to any of the posts, they stay on the Home page and are not taken to the single post.
But when a user tries to add a new threaded comment via the "Reply" link, they are redirected to the single post to comment.
Right now, by using this in functions.php:
add_filter('comment_post_redirect', 'redirect_after_comment');
function redirect_after_comment($location)
{
return $_SERVER["HTTP_REFERER"];
}
I can redirect users back to the Home page after they submit a new (unthreaded) comment.
However, this doesn't work for threaded comments. The "Reply" link to submit a threaded comment points to the single post and then doesn't redirect back to Home/Posts.
How can I change the "Reply" link to allow users to add a threaded comment while staying on the Home/Posts page?
Is there any other functionality that I need to be aware of in order to keep users on the Home page for all threaded comments?
index.php:
<?php
/**
* The main template file
*
* This is the most generic template file in a WordPress theme
* and one of the two required files for a theme (the other being style.css).
* It is used to display a page when nothing more specific matches a query.
* E.g., it puts together the home page when no home.php file exists.
*
* @link https://developer.wordpress.org/themes/basics/template-hierarchy/
*
* @package newco_theme
*/
get_header();
?>
<div class="g-columns__group">
<div class="g-columns__item--nine">
<main id="primary" class="g-layout__content">
<?php
if ( have_posts() ) :
if ( is_home() && ! is_front_page() ) :
?>
<header>
<h1 class="page-title screen-reader-text"><?php single_post_title(); ?></h1>
</header>
<?php
endif;
/* Start the Loop */
while ( have_posts() ) :
the_post();
/*
* Include the Post-Type-specific template for the content.
* If you want to override this in a child theme, then include a file
* called content-___.php (where ___ is the Post Type name) and that will be used instead.
*/
get_template_part( 'template-parts/content', get_post_type() );
endwhile;
endif;
?>
<br />
<?php if(function_exists('wp_paginate')) { wp_paginate(); } ?>
</main><!-- #main -->
</div>
<?php
get_sidebar();
get_footer();
content.php:
<?php
/**
* Template part for displaying posts
*
* @link https://developer.wordpress.org/themes/basics/template-hierarchy/
*
* @package newco_theme
*/
?>
<article id="post-<?php the_ID(); ?>" <?php post_class(); ?>>
<header class="entry-header">
<?php
if ( is_singular() ) :
the_title( '<h1 class="entry-title">', '</h1>' );
else :
the_title( '<h2 class="entry-title"><a href="' . esc_url( get_permalink() ) . '" rel="bookmark">', '</a></h2>' );
endif;
if ( 'post' === get_post_type() ) :
?>
<div class="entry-meta">
<?php
echo 'Posted on: ' .get_the_date();
newco_theme_posted_by();
?>
</div><!-- .entry-meta -->
<?php endif; ?>
</header><!-- .entry-header -->
<div class="entry-content">
<?php
the_content(
sprintf(
wp_kses(
/* translators: %s: Name of current post. Only visible to screen readers */
__( 'Continue reading<span class="screen-reader-text"> "%s"</span>', 'newco-theme' ),
array(
'span' => array(
'class' => array(),
),
)
),
wp_kses_post( get_the_title() )
)
);
wp_link_pages(
array(
'before' => '<div class="page-links">' . esc_html__( 'Pages:', 'newco-theme' ),
'after' => '</div>',
)
);
?>
</div><!-- .entry-content -->
<?php if(!is_single()){?>
<footer class="entry-footer">
<div class="_post-loop">
<div class="_post-links"><?php no_comments_entry_footer(); ?></div>
<?php global $withcomments; $withcomments = true; comments_template(); ?>
</div>
<?php comment_form(); ?>
<hr>
</footer><!-- .entry-footer -->
<?php } ?>
</article><!-- #post-<?php the_ID(); ?> -->
comments.php
<?php
/**
* The template for displaying comments
*
* This is the template that displays the area of the page that contains both the current comments
* and the comment form.
*
* @link https://developer.wordpress.org/themes/basics/template-hierarchy/
*
* @package newco_theme
*/
/*
* If the current post is protected by a password and
* the visitor has not yet entered the password we will
* return early without loading the comments.
*/
if ( post_password_required() ) {
return;
}
?>
<div id="comments" class="comments-area">
<?php
// You can start editing here -- including this comment!
if ( have_comments() ) :
?>
<h2 class="comments-title">
<?php
$newco_theme_comment_count = get_comments_number();
if ( '1' === $newco_theme_comment_count ) {
printf(
/* translators: 1: title. */
esc_html__( 'One comment on “%1$s”', 'newco-theme' ),
'<span>' . wp_kses_post( get_the_title() ) . '</span>'
);
} else {
printf(
/* translators: 1: comment count number, 2: title. */
esc_html( _nx( '%1$s comments on “%2$s”', '%1$s comments on “%2$s”', $newco_theme_comment_count, 'comments title', 'newco-theme' ) ),
number_format_i18n( $newco_theme_comment_count ), // phpcs:ignore WordPress.Security.EscapeOutput.OutputNotEscaped
'<span>' . wp_kses_post( get_the_title() ) . '</span>'
);
}
?>
</h2><!-- .comments-title -->
<?php the_comments_navigation(); ?>
<ol class="comment-list">
<?php
wp_list_comments(
array(
'style' => 'ol',
'short_ping' => true,
)
);
?>
</ol><!-- .comment-list -->
<?php
the_comments_navigation();
// If comments are closed and there are comments, let's leave a little note, shall we?
if ( ! comments_open() ) :
?>
<p class="no-comments"><?php esc_html_e( 'Comments are closed.', 'newco-theme' ); ?></p>
<?php
endif;
endif; // Check for have_comments().
comment_form();
?>
</div><!-- #comments -->
the function no_comments_entry_footer
:
if ( ! function_exists( 'no_comments_entry_footer' ) ) :
/**
* Prints HTML with meta information for the categories, tags and comments.
*/
function no_comments_entry_footer() {
// Hide category and tag text for pages.
if ( 'post' === get_post_type() ) {
/* translators: used between list items, there is a space after the comma */
$categories_list = get_the_category_list( esc_html__( ', ', 'newco-theme' ) );
if ( $categories_list ) {
/* translators: 1: list of categories. */
printf( '<span class="cat-links">' . esc_html__( 'Posted in %1$s', 'newco-theme' ) . '</span>', $categories_list ); // phpcs:ignore WordPress.Security.EscapeOutput.OutputNotEscaped
}
/* translators: used between list items, there is a space after the comma */
$tags_list = get_the_tag_list( '', esc_html_x( ', ', 'list item separator', 'newco-theme' ) );
if ( $tags_list ) {
/* translators: 1: list of tags. */
printf( '<span class="tags-links">' . esc_html__( 'Tagged %1$s', 'newco-theme' ) . '</span>', $tags_list ); // phpcs:ignore WordPress.Security.EscapeOutput.OutputNotEscaped
}
}
edit_post_link(
sprintf(
wp_kses(
/* translators: %s: Name of current post. Only visible to screen readers */
__( 'Edit <span class="screen-reader-text">%s</span>', 'newco-theme' ),
array(
'span' => array(
'class' => array(),
),
)
),
wp_kses_post( get_the_title() )
),
'<span class="edit-link">',
'</span>'
);
}
endif;