Overview

In this article I’ll show you how to add the same comments and comment form display on multiple posts or pages on your WordPress site.

 

Creating shortcodes

This entire process is pretty simple and revolves around using some custom shortcodes. To do this, you’ll need to add piece of code to your theme’s functions.php file. But it’s not a good idea to edit your functions.php file directly so it’s better to use a plugin.

We use the “My Custom Functions” plugin, which I recommend, not just for people that are not very techy but even for experienced developers. I often use it because it is safe and quick way to add custom code to your theme.

How to install and use My Custom Functions

  1. Navigate to Plugins => Add New.
  2. Search for “My Custom Functions”.
  3. Look for My Custom Function plugin, click install and when finished click Activate.
  4. Navigate to Settings => PHP Inserter.

Adding code

  1. Inside functions.php file OR PHP Inserter, add this code:

    // Shortcode: [display_comments]
    function display_comments_function($atts = [], $content = null, $tag = ''){
    $atts = array_change_key_case((array)$atts, CASE_LOWER);
    $comments_atts = shortcode_atts([
    'post_id' => '',
    ], $atts, $tag);
    ob_start();
    echo '<div id="commentsbox">';
    echo '<h2>Comments:</h2>';
    echo '<ol class="commentlist">';
    //Gather comments for a specific post or page
    $comments = get_comments(array(
    'post_id' => $comments_atts['post_id'],
    'status' => 'approve' //Change this to the type of comments to be displayed
    ));

    //Display the list of comments
    wp_list_comments(array(
    'per_page' => 10, //Allow comment pagination
    'reverse_top_level' => false //Show the oldest comments at the top of the list
    ), $comments);
    echo '</ol>';
    echo '</div>';
    $ReturnString = ob_get_contents();
    ob_end_clean();
    return $ReturnString;
    }
    add_shortcode( 'display_comments', 'display_comments_function' );

    // Shortcode: [display_comments_form]
    function display_comments_form_function($atts = [], $content = null, $tag = ''){
    $atts = array_change_key_case((array)$atts, CASE_LOWER);
    $comments_atts = shortcode_atts([
    'post_id' => '',
    ], $atts, $tag);
    ob_start();
    comment_form($default, $comments_atts['post_id']);
    $ReturnString = ob_get_contents();
    ob_end_clean();
    return $ReturnString;
    }
    add_shortcode( 'display_comments_form', 'display_comments_form_function' );

    // Shortcode: [redirect_back]
    function redirect_back_shortcode(){
    if(is_single() || is_post()){
    ob_start();
    $location = wp_get_referer();
    echo '<script>';
    echo 'window.location="'.$location.'";';
    echo '</script>';
    echo '<a href="'.$location.'">Go back to see your comment</a>';
    wp_redirect($location);
    exit();
    $ReturnString = ob_get_contents();
    ob_end_clean();
    return $ReturnString;
    }
    }
    add_shortcode( 'redirect_back', 'redirect_back_shortcode' );

    addcode - How to add the SAME comments on multiple posts or pages

  2. Make sure to toggle ON the toggle on top right corner.
  3. Click Save changes.

Using Shortcodes

After we have created the shortcodes, we can use it to display the same Comments List and Comment Form on multiple posts.

Getting the ID

  1. Create a new post or page. This is where we’ll store our comments.
  2. Add this shortcode into the content: [redirect_back]
  3. Publish it.
  4. Copy the post’s ID.
  5. It is located in the URL after ?post=
    Example: https://yourwebsite.com/wp-admin/post.php?post=6727&action=edit
    copyidurl - How to add the SAME comments on multiple posts or pages
  6. Save this ID or write it somewhere, we will use it later.

Using shortcodes on posts/pages

Follow these steps for each post or page where you want these comments displayed:

  1. Go to the Editor of post/page.
    Make sure that comments are disabled and there are no comments on this post/page.
    disabledcomments - How to add the SAME comments on multiple posts or pages
  2. Add these shortcodes wherever you’d like to display them:

    [display_comments post_id='12345']
    [display_comments_form post_id='12345']

    Replace 12345 with the ID we saved earlier.

  3. Save or Publish your post.
  4. That’s it. Now you should see same comments on these posts/pages.

Feel free to leave a comment if you have any troubles or questions.

Share This