redirect - Specific Content on pages based on user

admin2025-01-08  5

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?

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?

Share Improve this question asked Dec 21, 2016 at 0:57 JohnJohn 11 bronze badge
Add a comment  | 

1 Answer 1

Reset to default 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.

转载请注明原文地址:http://conceptsofalgorithm.com/Algorithm/1736269876a1386.html

最新回复(0)