• Resolved fsimmons

    (@fsimmons)


    I have a site built with Beaver Builder. And we have a downloads area on some posts. Those files are attached with ACF. If we restrict the post to “members”, anyone can still download the file as it is outside of the post content in the template.

    And while we could use members_access or other shortcodes, it would be great to just be able to wrap non post content areas in something that follows post role restrictions. Any advice?

    • This topic was modified 6 months, 1 week ago by fsimmons.
    • This topic was modified 6 months, 1 week ago by fsimmons.
Viewing 6 replies - 1 through 6 (of 6 total)
  • Plugin Support Omar ElHawary

    (@omarelhawary)

    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,

    Thread Starter fsimmons

    (@fsimmons)

    Interesting… so like…

    if ( current_user_can( 'read' ) ) {

    Would that align with page-level role restriction?

    Thread Starter fsimmons

    (@fsimmons)

    That seems to function like logged_in. If I set post to “member”, a “non-member” logged in account can is still read -> 1.

    Plugin Support Omar ElHawary

    (@omarelhawary)

    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,

    Thread Starter fsimmons

    (@fsimmons)

    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 6 months, 1 week ago by fsimmons.
    • This reply was modified 6 months, 1 week ago by fsimmons.
    • This reply was modified 6 months, 1 week ago by fsimmons.
    • This reply was modified 6 months, 1 week ago by fsimmons.
    Plugin Support Omar ElHawary

    (@omarelhawary)

    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!

Viewing 6 replies - 1 through 6 (of 6 total)
  • The topic ‘Shortcode that inherits role restriction’ is closed to new replies.