Forum Replies Created
-
AuthorPosts
-
January 27, 2020 at 5:10 pm in reply to: Custom Post Types does not seem to be able to be protected? #19685
joshua
ParticipantI know this post is wicked old, but in case anyone is trying to solve this, here is what I did:
I am using a custom theme, with a custom post type (registered in my functions.php, not a plugin). I am using Advanced Custom Fields (ACF) to generate the content for the posts page. Like others, I’ve been trying to solve having ACF fields be hidden. It has to be in a wp-loop! There are many ways to achieve this, I opted to do this with a Custom Post Type (CPT).
By default, Simple Membership Plugin did not protect the CPT. So I needed to wrap the page content in
if(SwpmMemberUtils::is_member_logged_in())This only partially helped, as it checks to see if the user is logged in, but it does not protect based on membership levels, etc. So I needed the then check the account permissions, using
global $post; $access_ctrl = SwpmAccessControl::get_instance(); if ($access_ctrl->can_i_read_post($post)){The complete block of code on my single-customers.php page looks like:
<?php if(SwpmMemberUtils::is_member_logged_in()) { global $post; $access_ctrl = SwpmAccessControl::get_instance(); if ($access_ctrl->can_i_read_post($post)){ //This user can read the specified post //Add your custom code that does something ?> <?php while ( have_posts() ) : the_post(); ?> <h1 class="moldeB pull-left portal-name">CUSTOMER PORTAL — <?php the_field( 'client_name' ); ?> </h1> <?php $client_logo = get_field( 'client_logo' ); ?> <?php if ( $client_logo ) { ?> <div class="spacer-20"></div> <img src="<?php echo $client_logo['url']; ?>" alt="<?php echo $client_logo['alt']; ?>" class="pull-right customer-logo" /> <?php } ?> <div class="clearfix"></div> <div class="spacer-50"></div> <?php the_field( 'client_content' ); ?> <?php //get_template_part( 'template-parts/content', get_post_type() ); endwhile; // End of the loop. ?> <?php } else { ?> <p>You do not have access to this page.</p> <?php } ?> <?php } else { ?> <p>You are not logged in. Please login <a href="/customer-portal">here.</a></p> <?php } ?>I hope this helps someone else searching!
-
AuthorPosts