Skip to content

Instantly share code, notes, and snippets.

@deivamagalhaes
Last active April 16, 2020 17:26
Show Gist options
  • Save deivamagalhaes/50bcf08e1280282337634da90d6c2fd4 to your computer and use it in GitHub Desktop.
Save deivamagalhaes/50bcf08e1280282337634da90d6c2fd4 to your computer and use it in GitHub Desktop.
Code snippet to hide the comments and the comment form for all non-members
<?php
add_filter( 'comments_array', function ( $comments_flat, $post_id ) {
$post = get_post( $post_id );
// checks if the current post has any restricted content
if ( false !== strpos( $post->post_content, '[wcm_restrict' ) ) {
if ( ! wc_memberships_is_user_active_member()
|| ( get_current_user_id() && ! wc_memberships_user_can( get_current_user_id(), 'view', array( 'post' => $post_id ) ) ) ) {
$comments_flat = [];
}
}
return $comments_flat;
}, 10, 2 );
add_filter( 'comments_open', function ( $open, $post_id ) {
$post = get_post( $post_id );
// checks if the current post has any restricted content
if ( false !== strpos( $post->post_content, '[wcm_restrict' ) ) {
$open = ( wc_memberships_is_user_active_member()
&& ( get_current_user_id() && wc_memberships_user_can( get_current_user_id(), 'view', array( 'post' => $post_id ) ) ) );
}
return $open;
}, 10, 2 );
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment