WPDiscuz
embed anywhere shortcode

In this will guide, we're going to show you how to create a short code for the wpDiscuz, allowing you to custom position comments anywhere on your single post or custom post type. The wpDiscuz is a great tool for comment management on high traffic WordPress websites, and it's the one that we use on this blog. If you want to add it to a custom location on your WordPress website, creating a short code for wpDiscuz, and then placing that shortcode anywhere on the page is a great way to do that.

To create a shortcode for wpDiscuz, it's no different than creating a short code for the standard WordPress comments feature. The reason behind this is due to the fact that the wpDiscuz simply replaces the standard WordPress comments PHP with its own. Therefore, anywhere a normal WordPress comments section shows up on the site, the wpDiscuz will replace it with its own design and functionality.

So, to create a short code for wpDiscuz, all you need to do is paste in the following code, which is a direct copy of the code used to create a custom WordPress comments shortcode.

This code (source) should be added to your functions.PHP on your child theme, or by a code manager like Advanced Scripts (recommended method). It should be loaded in on “plugins loaded”.

[wpse_comments_template]

/**     
 * 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 wpDiscuz comments section anywhere on your website, simply paste in the following short code, navigate to the front end, and see the comments section in the new position.


[wpse_comments_template]

TESTED WORKING JAN 2021 (Oxygen Builder, Elementor)

 


Stack Overflow

 

f you are running the [wpse_comments_template] shortcode before this part in your theme:

<?php
    if ( comments_open() || '0' != get_comments_number() )
        comments_template( '', true );
?>

then it's attempting to use:


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',         '__return_false'             );
        add_filter( 'get_comments_number',   '__return_zero'              );
        return ob_get_clean();
    }
    return '';
}, 10, 2 );

but this might interfere with comment related stuff that comes later, for example in the sidebar.

So it would be more accurate to use:

/**     
 * 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;
}

and you wouldn't have to remove any code from your theme.

I tested this on the Twenty Fifteen theme and it seemed to work as expected.

 

Scroll to Top