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)
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
Thank you ! This really saved me
Super! Worked perfectly. Thanks for the script James 🙂