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:
- 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.
- 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.)
- 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.
- 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:
- Open the functions.php file of your theme (as explained in the previous answer).
- 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.
- 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!