0

I'm going to replace default list of comments and comment form with Facebook's comment box. As far as I know there is function wp_list_comments(). How can I replace this function with my custom one?

Update: I want completely ignore built-in WordPress comments.

When I try to add my code to the end of content, it works:

add_filter ('the_content', 'fbcommentbox', 100);

But the following code does not:

add_filter ('comments_template', 'fbcommentbox', 100);
2

Note that with the following:

add_filter ('comments_template', 'fbcommentbox', 100);

we expect fbcommentbox() to return the path to the new comments template file. The default is the path of comments.php.

If you create the fbcommentbox.php file in your theme, then you could try:

add_filter( 'comments_template', 'fbcommentbox', 100);

function fbcommentbox( $theme_template )
{
    // Path to our new comment template file
    $new_theme_template = get_template_directory() . '/fbcommentbox.php';

    // Override if it exsits
    if( file_exists( $new_theme_template ) )
        $theme_template = $new_theme_template;

    return $theme_template;
}

If the file fbcommentbox.php doesn't exists then the default one is loaded instead. For a child theme we would use get_stylesheet_directory().

Here's a shorter version:

function fbcommentbox( $theme_template )
{
    return locate_template( 'fbcommentbox.php' );
}

where the locate_template() does all the hard work.

Update

But you mentioned you wanted to override wp_list_comments() instead.

Here's a way to do that by changing the echo argument to false:

add_filter( 'wp_list_comments_args', function( $args )
{
    // Display fbcommentbox.php 
    get_template_part( 'fbcommentbox' );

    // Disable output of wp_list_comments()
    $args['echo'] = 0;

    return $args;
} );

and output instead the fbcommentbox.php template.

  • Looks good. I work for me, but the is an another problem: it removes all the comments.php template content which could be very different in different themes. That's why I was looking for a way to override wp_list_comments() only. – Antonio Dec 16 '15 at 20:35
  • so you want to keep the theme's comments.php template (for the sake of the layout) but still not use the comments in WordPress @Antonio – birgire Dec 16 '15 at 20:46
  • Yes, because comments.php contains border, margins, background colors, etc – Antonio Dec 17 '15 at 7:20

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.