How to display the comment_form with a shortcode while removing it from its default position?

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

Leave a Comment