0

I have a page on my site and would like to show different content on this paged based on who is logged in. If the user isn't logged in, show the login form or if the user is logged in but does not have access, then show a message stating so.

I have tried "User Specific Content" plugin but I can't seem to get that to show a login link or form and redirect that back to the page in question.

Does anyone know how to achieve this?

1 Answer 1

0

You can separate your users with roles, with different capabilities, and you can add custom roles to your theme. Use the capabilities of the users to filter the content you show them with the WP functions current_user_can() and is_user_logged_in(). Here you have an example:

if(is_user_logged_in() && current_user_can( 'read' )){
        // Show something
    }else{
        if(is_user_logged_in()){
            // Show something else
        }else{
            // Redirect to login?
        }
    }

You can also filter access to pages depending of the user itself and not the role. It's too much specific in my opinion, but it could be an option. The function wp_get_current_user() would retrieve you the current user in the page, and you could compare it with your known user. With this option you can also get the exact role of the user and filter them with that, which I think is a better option.

1
  • You're welcome, I'm glad it helped you!
    – dgarceran
    Dec 22, 2016 at 9:08

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.