How To Make A WordPress Comments Shortcode (No Plugin)

Published: 7 months ago

This tutorial will show you how to create a custom shortcode for your WordPress Commons. With it, you can place them in custom positions, simply by in putting the shortcode. This is helpful if you use a page builder and want to add your WordPress comments anywhere on the page, or want to place your comments in a different section than your theme normally allows.

There are several plugin that allow you to create shortcodes for WordPress comments, but this example will use a simple code snippet. This makes it so you don't need to install yet another plugin, which will probably slow down your WordPress website.

Simply paste the following code (source) snippet into your functions.php, or by using Advanced Scripts:

/**     
 * Display the comment template with the [wpse_comments_template] 
 * shortcode on singular pages. 
 *
 * @see http://stackoverflow.com/a/28644134/2078474
 */
 add_shortcode( 'wpse_comments_template', function( $atts = array(), $content = '' )
 {
    if( is_singular() && post_type_supports( get_post_type(), 'comments' ) )
    {
        ob_start();
        comments_template();
        add_filter( 'comments_open',       'wpse_comments_open'   );
        add_filter( 'get_comments_number', 'wpse_comments_number' );
        return ob_get_clean();
    }
    return '';
}, 10, 2 );

function wpse_comments_open( $open )
{
    remove_filter( current_filter(), __FUNCTION__ );
    return false;
}

function wpse_comments_number( $open )
{
    remove_filter( current_filter(), __FUNCTION__ );
    return 0;
}

Now, if you want to place your WordPress comments section anywhere on your website, simply paste in the following shortcode, navigate to the front end, and see the comments section in the new position.

[wpse_comments_template] 
Join Our Newsletter
Here's what you get: A once a week newsletter that contains original content, aggregated posts, WordPress deals, and anything else we think you'd like. We don't spam or sell your email address. Unsub at any time.
Subscribe
Notify of
guest
0 Comments
Inline Feedbacks
View all comments
IsoGroup- Web Dev/Design, WordPress and More

Affiliate Disclosure: Some links on this website are affiliate links. We make commissions for purchases made through those links.

We only recommend items/tools that we've personally used and like.

Full Disclosure | FTC Statement

Join Our Newsletter
Over 4,000 Design Lovers and Problem Solvers.

Author

A relationship driven website creation company.

Connect

© ISOTROPIC, LLC 2016-2021
linkedin facebook pinterest youtube rss twitter instagram facebook-blank rss-blank linkedin-blank pinterest youtube twitter instagram