isotropic-2022
Share
Blog
Search
Menu

How To Make A WordPress Comments Shortcode (No Plugin)

By James LePage
 on January 7, 2021
Last modified on January 7th, 2022

How To Make A WordPress Comments Shortcode (No Plugin)

By James LePage
 on January 7, 2021
Last modified on January 7th, 2022

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; }
Code language: PHP (php)

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]
Code language: JSON / JSON with Comments (json)
Subscribe & Share
If you liked this content, subscribe for our monthly roundup of WordPress news, website inspiration, exclusive deals and interesting articles.
Unsubscribe at any time. We do not spam and will never sell or share your email.
Subscribe
Notify of
guest
1 Comment
Oldest
Newest Most Voted
Inline Feedbacks
View all comments
G Mike
G Mike
2 months ago

Hello,

TL;DR – I’m using a multisite installation. Question: How do I use your code to fetch comments + comments form from my subsite and display them into my root site.

Details: I am using WordPress Multilingual Multisite where my Root site is in English and subsite is in Spanish.

On the English root site, is it possible to have at the bottom of each post a section that will pull the “comments + comment form” of the Spanish sub-site and display them in the English root site?

Basically, each post on the English root site will have a section where user can submit content in English and another section where user can submit comment in Spanish.
Is it possible? What function, action, snippet do I need to use to accomplish that?

Great post by the way!

— GM

Article By
James LePage
Contributors/Editors
notloggedin
James LePage is the founder of Isotropic Design, a digital agency that builds WordPress websites. I read, write and design constantly!
We're looking for new authors. Explore Isotropic Jobs.
Close
🇺🇸 English
Save posts, access exclusive content, join members-only communities and more.
Dark Mode Toggle
Love web dev? Join the Isotropic weekly newsletter.
cross
linkedin facebook pinterest youtube rss twitter instagram facebook-blank rss-blank linkedin-blank pinterest youtube twitter instagram
0

Your Cart is Empty