Income.tube
  • YouTube Channel Income Calculator
  • YouTube Trending Video Analyzer
  • YouTube Video Idea Generator
  • Tools And Resources
No Result
View All Result
  • Login
  • Register
Income.tube
No Result
View All Result
Income.tube
No Result
View All Result
restrict content by user

Restrict content to subscribers only WordPress WITHOUT plugin

Steve Gilruth by Steve Gilruth
in Online Business Ideas
0
A A
Share on FacebookShare on Twitter

I asked ChatGPT “how do I restrict some page and post content to subscribers only WordPress WITHOUT a plugin”

Response:

Restricting access to certain page and post content for subscribers only in WordPress without using a plugin can be done using a custom function. You can add the function to your theme’s functions.php file. Here’s a step-by-step guide to help you achieve this:

  1. Access your WordPress files: You can access your WordPress files through the file manager in your hosting control panel or by using an FTP client.
  2. Locate the functions.php file: Go to the /wp-content/themes/your-theme/ directory and locate the functions.php file. (Replace “your-theme” with the name of your active theme.)
  3. Edit the functions.php file: Open the functions.php file in a text editor and add the following code:
function restrict_content_to_subscribers_only($content) {
    if (is_single() || is_page()) {
        if (is_user_logged_in()) {
            $user = wp_get_current_user();
            if (in_array('subscriber', (array) $user->roles)) {
                // The user has the "subscriber" role
                return $content;
            } else {
                return '<p>This content is restricted to subscribers only. Please <a href="' . wp_login_url() . '">log in</a> or <a href="' . wp_registration_url() . '">register</a> to view this content.</p>';
            }
        } else {
            return '<p>This content is restricted to subscribers only. Please <a href="' . wp_login_url() . '">log in</a> or <a href="' . wp_registration_url() . '">register</a> to view this content.</p>';
        }
    }
    return $content;
}
add_filter('the_content', 'restrict_content_to_subscribers_only');

This code defines a function called restrict_content_to_subscribers_only that checks if the current user is logged in and has the “subscriber” role. If they do, the content is displayed; otherwise, a message is displayed asking the user to log in or register.

  1. Save the changes: Save the functions.php file and upload it back to your server if you’re using an FTP client.

Now, when you visit any post or page on your WordPress site, the content will be restricted to logged-in subscribers only. Non-subscribers and logged-out users will see the login/register message.

Please note that editing your theme’s files may have unintended consequences. Always create a backup of your files before making any changes, and consider using a child theme to avoid losing your changes when the parent theme is updated.

Thanks, please modify to use a shortcode beginning of restricted content and end of content to only restrict certain sections of a post or page to people not logged in as a subscriber

To restrict certain sections of a post or page using a shortcode, you can follow these steps:

  1. Open the functions.php file of your theme (as explained in the previous answer).
  2. Add the following code to your functions.php file:
function restrict_content_to_subscribers_only_shortcode($atts, $content = null) {
    if (is_user_logged_in()) {
        $user = wp_get_current_user();
        if (in_array('subscriber', (array) $user->roles)) {
            // The user has the "subscriber" role
            return do_shortcode($content);
        } else {
            return '<p>This content is restricted to subscribers only. Please <a href="' . wp_login_url() . '">log in</a> or <a href="' . wp_registration_url() . '">register</a> to view this content.</p>';
        }
    } else {
        return '<p>This content is restricted to subscribers only. Please <a href="' . wp_login_url() . '">log in</a> or <a href="' . wp_registration_url() . '">register</a> to view this content.</p>';
    }
}
add_shortcode('restricted', 'restrict_content_to_subscribers_only_shortcode');

This code defines a shortcode named ‘restricted’, which you can use to wrap the content you want to restrict. The shortcode checks if the current user is logged in and has the “subscriber” role. If they do, the content is displayed; otherwise, a message is displayed asking the user to log in or register.

  1. Save the changes: Save the functions.php file and upload it back to your server if you’re using an FTP client.

Now, you can use the

Exclusive Content 🚀 Unlock Exclusive Access! The content/tools below are available to logged-in users only. To view this content, please Login or Register using the links in the menu above to gain instant access now!



This is more public content visible to everyone.

The restricted content will only be visible to logged-in subscribers, while other content will be visible to all users.

amazing thank you

You’re welcome! I’m glad I could help. If you have any more questions or need further assistance, please don’t hesitate to ask. Good luck with your WordPress project!

Sorry, please add access to all WordPress user roles, not just subscribers

No problem! To grant access to the restricted content for all logged-in WordPress user roles, you just need to modify the code slightly. Replace the previous code in your functions.php file with the following code:

function restrict_content_to_logged_in_users_shortcode($atts, $content = null) {
    if (is_user_logged_in()) {
        // The user is logged in
        return do_shortcode($content);
    } else {
        return '<p>This content is restricted to logged-in users. Please <a href="' . wp_login_url() . '">log in</a> or <a href="' . wp_registration_url() . '">register</a> to view this content.</p>';
    }
}
add_shortcode('restricted', 'restrict_content_to_logged_in_users_shortcode');

Now, the

Exclusive Content 🚀 Unlock Exclusive Access! The content/tools below are available to logged-in users only. To view this content, please Login or Register using the links in the menu above to gain instant access now!

shortcode will allow all logged-in users to view the content, regardless of their role. The content will be restricted only for logged-out users.

Remember to save your changes in the functions.php file and upload it back to your server if necessary.

Brilliant thank you!

You’re welcome! I’m glad the solution works for you. If you have any more questions or need further assistance, please feel free to ask. Good luck with your WordPress project!

ShareTweetSharePin

Related Posts

ClickBank YouTube Shorts
Affiliate Marketing

The Ultimate ClickBank YouTube Shorts Strategy

Argil-AI
Online Business Ideas

Why Argil.ai is the Ultimate AI Tool for Creating Viral Shorts

Affiliate Programs for Shorts
Affiliate Marketing

The Best Affiliate Programs for Shorts to Boost Your Income 🚀

Engaging YouTube Video Ideas
Online Business Ideas

Discover Engaging YouTube Video Ideas

Leave a Reply Cancel reply

Your email address will not be published. Required fields are marked *

  • About
  • Contact
  • Disclaimer – Disclosure
  • Tools And Resources
  • Terms & Privacy

© 2025 Income.tube

Welcome Back!

Sign In with Google
OR

Login to your account below

Forgotten Password? Sign Up

Create New Account!

Sign Up with Google
OR

Fill the forms below to register

All fields are required. Log In

Retrieve your password

Please enter your username or email address to reset your password.

Log In

Add New Playlist

No Result
View All Result
  • YouTube Channel Income Calculator
  • YouTube Trending Video Analyzer
  • YouTube Video Idea Generator
  • Tools And Resources

© 2025 Income.tube