Hi @fsimmons,
Thanks for reaching out Members Support Team!
You should use at current_user_can function from WordPress and put that in PHP templates where the ACF gets output.
Regards,
Interesting… so like…
if ( current_user_can( 'read' ) ) {
Would that align with page-level role restriction?
That seems to function like logged_in. If I set post to “member”, a “non-member” logged in account can is still read -> 1.
Hi @fsimmons,
You can protect pages/posts with Members as this screenshot. You can select a Role(s) that can see it. So, you can use some PHP in your templates to also check for the same role(s):
if(current_user_can('subscriber')) {
// ACF output goes in here
}
If you want to check for multiple roles
if(current_user_can('subscriber') || current_user_can('contributor') {
// ACF output goes in here
}
You can read more in this thread.
Regards,
Yeah, no. I ended up with this which uses the plugin’s function members_can_current_user_view_post.
/**
* Shortcode to conditionally display content based on Members plugin permissions.
*
* @param array $atts Shortcode attributes.
* @param string|null $content Content inside the shortcode.
* @return string Content if the user has permission, otherwise an empty string.
*/
function conditional_members_content_shortcode( $atts, $content = null ) {
// Get the current post ID.
$post_id = get_the_ID();
// Check if the current user can view the post.
if ( members_can_current_user_view_post( $post_id ) ) {
// Return the content if the user has permission.
return do_shortcode( $content );
} else {
// Optionally, return nothing or a message for users who don't have permission.
return '';
}
}
// Register the shortcode
add_shortcode( 'conditional_members_content', 'conditional_members_content_shortcode' );
And then I wrap my download box with [conditional_members_content][/conditional_members_content]. And it knows whatever role can see the content.
But thanks anyways, brotendo!
-
This reply was modified 2 years ago by
fsimmons.
-
This reply was modified 2 years ago by
fsimmons.
-
This reply was modified 2 years ago by
fsimmons.
-
This reply was modified 2 years ago by
fsimmons.
Great to hear everything is running smoothly! If you ever have questions, feel free to open a ticket. Your insights are invaluable to us, and we appreciate it if you ever want to share.
Keep having a fantastic day!