mermberfix logo x
The MemberFix Team’s got you covered.
Membership plugins

How to add the SAME comments on multiple posts or pages

same comments

[et_pb_section fb_built=”1″ _builder_version=”3.19.6″ custom_margin=”0px||” custom_padding=”0px||”][et_pb_row _builder_version=”3.0.48″ background_size=”initial” background_position=”top_left” background_repeat=”repeat”][et_pb_column type=”4_4″ _builder_version=”3.0.47″ parallax=”off” parallax_method=”on”][et_pb_text _builder_version=”3.19.4″]

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.

[/et_pb_text][/et_pb_column][/et_pb_row][/et_pb_section]

You may also enjoy...

WordPress based membership sites have certain requirements, and make use of certain applications that, in my experience, makes most of the popular hosting providers a poor choice.
Second series of posts called “Best Practices For Selling Your Digital Products”
Learn how to add a mandatory multi-step Terms of Service to the Memberpress checkout form using the Contact Form 7 plugin…
Learn how to integrate Invision Power System to Wordpress via SSO.
Subscribe
Notify of
guest
1 Comment
Oldest
Newest Most Voted
Inline Feedbacks
View all comments
Aditya Tri Hartanto
October 15, 2022 12:50 pm

Dude.. Its not working. You code break my sites -__-

Screenshot_111.png