1

I was already able to have the comments form show in the post content area with this code, which allows me to use the [wpsites_comment_form] shortcode:

add_shortcode( 'wpsites_comment_form', 'wpsites_comment_form_shortcode' );
function wpsites_comment_form_shortcode() {
    ob_start();
    comment_form();
    $cform = ob_get_contents();
    ob_end_clean();
    return $cform;
} 

What I need is to remove the comments form from the bottom of the post (default display). When I remove it through the admin panel in Posts - Edit - Comments - Don't Allow, it is also removed from the location where it displayed through the shortcode function. So how can I make it display only where the shortcode is placed?

1

Version #1

The following seems to work for the Twenty Fifteen theme:

/**
 * Display the comment form via shortcode on singular pages
 * Remove the default comment form.
 * Hide the unwanted "Comments are closed" message with CSS.
 *
 * @see http://wordpress.stackexchange.com/a/177289/26350
 */

add_shortcode( 'wpse_comment_form', function( $atts = array(), $content = '' )
{
    if( is_singular() && post_type_supports( get_post_type(), 'comments' ) )
    {
        ob_start();
        comment_form();
        print(  '<style>.no-comments { display: none; }</style>' );
        add_filter( 'comments_open', '__return_false' );
        return ob_get_clean();
    }
    return '';
}, 10, 2 );

where we only allow the shortcode to display the comment form on singular posts that support comments.

Here we use CSS to hide the unwanted Comments are closed message.

Version #2

Here's another approach without CSS:

/**
 * Display the comment form via shortcode on singular pages.
 * Remove the default comment form.
 * Hide the unwanted "Comments are closed" message through filters.
 *
 * @see http://wordpress.stackexchange.com/a/177289/26350
 */

add_shortcode( 'wpse_comment_form', function( $atts = array(), $content = '' )
{
    if( is_singular() && post_type_supports( get_post_type(), 'comments' ) )
    {
        ob_start();
        comment_form();
        add_filter( 'comment_form_defaults', 'wpse_comment_form_defaults' );
        return ob_get_clean();
    }           
    return '';
}, 10, 2 );

function wpse_comment_form_defaults( $defaults )
{
    add_filter( 'comments_open', 'wpse_comments_open' );
    remove_filter( current_filter(), __FUNCTION__ );
    return $defaults;
}

function wpse_comments_open( $open )
{
    remove_filter( current_filter(), __FUNCTION__ );
    return false;
}
  • Version #1 tested and it works with the theme I'm using. great! – rodrigo nomada Feb 11 '15 at 10:56
  • Glad to hear it worked for you @rodrigonomada – birgire Feb 11 '15 at 17:50
  • If I want to bring also de list of comments (comments_template) to be displayed above the form, I would need another shortcode function for it? Is it a similar approach? – rodrigo nomada Feb 18 '15 at 17:45
  • I found something here, but am not sure I know how to apply it: lists.automattic.com/pipermail/wp-hackers/2011-April/… – rodrigo nomada Feb 18 '15 at 17:51
  • I tried to mimic birgire's "Version #1" shared above in the answer in order to display the list of comments above the comment_form. It worked partially. It displays the comments in the default location (bottom) also. I used this: add_shortcode( 'wpse_comments_template', function( $atts = array(), $content = '' ) { if( is_singular() && post_type_supports( get_post_type(), 'comments' ) ) { ob_start(); comments_template(); return ob_get_clean(); } return ''; }, 10, 2 ); – rodrigo nomada Feb 18 '15 at 18:12

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.