开发者

How do I hide the WordPress comments form?

I have a plugin that enables Facebook commenting on WordPress. Users have requested a feature that will hide the default WordPress comments form on posts where Facebook commenting is enabled.

How do I go about hiding the WP comments form on the fly? I know I can comment out comment_form(); from the comments.php template, but I'd like to be able to hide/unhide it开发者_StackOverflow at the click on a button.

Does it have something to do with the comments_template filter?

The plugin's homepage is http://grahamswan.com/facebook-comments


Wrap the form in a div and use this simple piece of jquery to show/hide the div.

<div class="comments">
<?php comment_form(); ?>
</div>

<a class="comment_switch">Show / Hide Comments</a>

.hidden{display:none;}

$("comment_switch").click(function () {
     $("comments").toggleClass("hidden")");
   });


I have also such need arise. I have replaced the comment form with the appropriate message. Here's the hook code, you can change the condition as per your need.

/**
 * Restrict WordPress comment posting to Admin, Authors and HW+ active users
 */

function fun_comment_form_defaults( $defaults ) {
    if ( comments_open() && is_user_logged_in() && ( is_admin() || current_user_can( 'edit_others_posts', get_the_ID() ) || wc_memberships_is_user_active_member( get_current_user_id(), 'hw' ) ) ) {
        return $defaults;
    } else {
        $defaults['comment_field'] = wp_kses_post( sprintf( '<p>You should require an active <a href="%s">HW+ membership</a> to post a comment.</p>', esc_url( get_site_url() . '/membership/' ) ) );
        $defaults['submit_button'] = '';
        $defaults['submit_field']  = '';
        $defaults['fields']              = '';
        return $defaults;
    }
}
add_filter( 'comment_form_defaults', 'fun_comment_form_defaults' );
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜