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

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

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?

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?

Share Improve this question asked Feb 5, 2015 at 20:59 rodrigo nomadarodrigo nomada 451 gold badge1 silver badge8 bronze badges
Add a comment  | 

1 Answer 1

Reset to default 4

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

发布者:admin,转转请注明出处:http://www.yc00.com/questions/1745441660a4627857.html

相关推荐

发表回复

评论列表(0条)

  • 暂无评论

联系我们

400-800-8888

在线咨询: QQ交谈

邮件:admin@example.com

工作时间:周一至周五,9:30-18:30,节假日休息

关注微信