Creating Member-Only Content in WordPress: A Definitive Guide

Home » Snippets » Creating Member-Only Content in WordPress: A Definitive Guide
0

Created with:

Visibility: 

public

Creator: WPTurbo Team

Customize with WPTurbo AI
X

Add Snippet To Project

New Project
Add To Existing Project

Have you ever wanted to create exclusive content on your WordPress website just for your members? Or perhaps you’re considering starting a membership site and aren’t sure how to manage content availability. Either way, restricting content to members only is a highly sought-after feature for many website owners. This article will guide you step-by-step on how to create and manage member-only content, ensuring your valuable information is accessible only to those who you permit access to.

					function wpturbo_member_only_content( $content ) {
    if ( is_user_logged_in() ) {
        return $content;
    } else {
        return __('You must be logged in to view this content.', 'wpturbo');
    }
}
add_filter( 'the_content', 'wpturbo_member_only_content' );
				

The provided code snippet functions as a mechanism to restrict content to members only, meaning content will only be displayed if the user is logged in. If not, the user will see a custom message prompting them to log in.

Starting from the top, we first define a function wpturbo_member_only_content(). This function takes an argument $content.

function wpturbo_member_only_content( $content ) {

$content refers to the actual content of the page that WordPress would normally display. This function acts as a filter for $content.

Inside the function, we have an ifelse construct.

if ( is_user_logged_in() ) {
    return $content;
} else {
    return __('You must be logged in to view this content.', 'wpturbo');
}

The is_user_logged_in() is a built-in WordPress function that checks whether the current visitor is logged in. If they are, the function will return true, if not, it will return false.

If the user is logged in, meaning is_user_logged_in() returns true, we go to the if clause where $content is returned. In other words, WordPress will display the page’s actual content.

If the user is not logged in (is_user_logged_in() returns false), we go to the else clause. Here, instead of returning the content of the page, we return a custom message: 'You must be logged in to view this content.'.

The ‘__()’ function is a built-in WordPress function used for localization. It takes two arguments, the string to be translated and the text domain as an identifier.

Lastly, we apply a filter to the content with add_filter( 'the_content', 'wpturbo_member_only_content' );.

Here, 'the_content' is a WordPress filter hook applied to the content of a post being viewed, allowing us to modify the post’s content. Using this hook with our function 'wpturbo_member_only_content' applies our restrictions based on the member’s login status every time a post content is requested.

Register an account to save your snippets or go Pro to get more features.