admin管理员组

文章数量:1125714

For example. User "Mary" should be able to view to the private page "marys page" after logging in, "Tom" should be able to view the private page "toms page", etc

I created new users and am using the "user role editor" plugin to assign them to their own role. Each user will be able to see their own private page. I am using "peters login redirect" to redirect each user to their own private page.

My problem is that I cannot find a plugin that allows me to allow read access to the private page based on a users role. The only plugin I can find that does this is Wordpress Access Control but this plugin has been abandoned for many years.

Is there a plugin that does something similar or is there a simpler solution to this problem? Thanks

For example. User "Mary" should be able to view to the private page "marys page" after logging in, "Tom" should be able to view the private page "toms page", etc

I created new users and am using the "user role editor" plugin to assign them to their own role. Each user will be able to see their own private page. I am using "peters login redirect" to redirect each user to their own private page.

My problem is that I cannot find a plugin that allows me to allow read access to the private page based on a users role. The only plugin I can find that does this is Wordpress Access Control but this plugin has been abandoned for many years.

Is there a plugin that does something similar or is there a simpler solution to this problem? Thanks

Share Improve this question asked Oct 18, 2018 at 0:26 Nirmal MuljiNirmal Mulji 111 silver badge2 bronze badges
Add a comment  | 

2 Answers 2

Reset to default 1

You can achieve the functionality of showing private pages based on a user's role in WordPress without relying on a third-party plugin by using WordPress's built-in capabilities and custom code. Here's a step-by-step approach to implement this:

  1. Create Private Pages:
    • Create private pages for each user, such as "Mary's Page" and "Tom's Page." Make sure these pages are set to "Private" in the page visibility settings.
  2. Assign Roles:
    • You've already assigned roles using the "User Role Editor" plugin, which is great. Ensure that each user has their respective role.
  3. Customize Your Theme's functions.php:
    • You can add custom code to your theme's functions.php file to control access based on the user's role. Open your theme's functions.php file and add the following code:
function custom_private_page_access() {
    if (is_user_logged_in()) {
        $current_user = wp_get_current_user();
        $user_roles = $current_user->roles;

        // Define an array of allowed roles for each page
        $allowed_roles = array(
            'mary' => 'mary_role',
            'tom'  => 'tom_role',
            // Add more users and their roles here
        );

        // Get the current page's ID
        $current_page_id = get_the_ID();

        // Check if the user's role allows access to this page
        if (array_key_exists($current_user->user_login, $allowed_roles) &&
            in_array($allowed_roles[$current_user->user_login], $user_roles)) {
            // User has the required role, allow access to the page
            return;
        } else {
            // Redirect unauthorized users to a specific page (e.g., the homepage)
            wp_redirect(home_url());
            exit();
        }
    }
}
add_action('template_redirect', 'custom_private_page_access');
  1. Customize Role Names:
    • In the code above, replace 'mary', 'tom', 'mary_role', 'tom_role', and any other names with the actual usernames and roles you've set up in your WordPress installation.
  2. Test Your Pages:
    • Now, when Mary logs in, she will only be able to access "Mary's Page" if her role is correctly set to 'mary_role'. The same applies to Tom and other users.
  3. Redirect Unauthorized Users:
    • Unauthorized users will be redirected to the homepage or any other page you specify in the wp_redirect function.

This custom code checks the user's role and allows or denies access to private pages based on their assigned role. You can extend this code to accommodate more users and roles as needed. Remember to back up your theme files before making any changes, and be cautious while editing functions.php.

This solution doesn't rely on abandoned plugins and offers a more tailored approach to your specific needs.

You need either a membership plugin or a user management plugin, you can try these: https://wordpress.org/plugins/restrict-content/

https://wordpress.org/plugins/user-specific-content/

https://www.paidmembershipspro.com

This also can be handled pretty easily with a conditional PHP snippet.

本文标签: pluginsHow to show private pages based on a user39s role