If you’ve ever felt overwhelmed by the idea of building a membership site, check this out. I tried a minimal solution relying solely on WordPress core features, and it worked much better than I expected. Ka-chow!
Below is a basic membership setup with free registration, a login form, and protected content—delivered as a lightweight plugin. With just a few steps, WordPress makes it easy to get everything running smoothly.
Keep in mind this isn’t a comprehensive solution for membership sites. There are no settings pages or automation—everything is done manually. That said, it’s a solid foundation you can build on to suit your workflow.
Basic Membership plugin
Below is the Basic Membership plugin file structure:
/basic-membership/
├── plugin.php
The plugin.php file should consist of the following code, which I’ll break down into individual blocks for clarity and deeper understanding:
The plugin header is pretty straightforward:
<?php
/**
* Plugin Name: Basic Membership
* Description: Create a basic membership site with a custom user role and protected content.
* Version: 1.0
* Author: Brian Gardner
* Author URI: https://briangardner.com/
*/
Create site member user role
The function below creates a site_member user role:
/**
* Create site member user role.
*/
add_action( 'init', function() {
add_role(
'site_member',
__( 'Member' ),
[
'read' => true,
'level_0' => true,
]
);
});
‘read’ => true This allows the user to read content on the site. It’s the most basic capability, required for logging and accessing dashboard.
‘level_0’ => true This is a legacy capability from older versions of WordPress, loosely representing the lowest level of user access. It’s typically granted to Subscribers or any user role that can log in but doesn’t have editing capabilities.
Redirect site members after log in
The code below redirects users with the site_member role away from the WordPress admin and sends them to the site homepage after login.
/**
* Redirect site members to site home after log in.
*/
function redirect_member_from_admin() {
if (is_admin() && !defined('DOING_AJAX') && is_user_logged_in()) {
$user = wp_get_current_user();
if (in_array( 'site_member', (array) $user->roles)) {
wp_redirect(home_url());
exit;
}
}
}
add_action( 'init', 'redirect_member_from_admin' );
Create site member shortcode
This code creates a [site_member] shortcode that restricts content to logged-in users with the site_member role. If they qualify, the content is shown; otherwise, a message says they must be a member to view it.
/**
* Create site member shortcode.
*/
add_shortcode( 'site_member', function( $atts, $content = null ) {
if ( is_user_logged_in() && current_user_can( 'site_member' ) ) {
return do_shortcode( $content );
}
return '<p>You must be a member to view this content.</p>';
} );
An example user of the site_member shortcode:
[site_member]
This is content for members only.
[/site_member]
Create non member shortcode
This code creates a [non_member] shortcode that shows content only to users who aren’t logged in or don’t have the site_member role. If they qualify, the content is displayed; otherwise, nothing is shown.
/**
* Create non member shortcode.
*/
add_shortcode( 'non_member', function( $atts, $content = null ) {
if ( ! ( is_user_logged_in() && current_user_can( 'site_member' ) ) ) {
return do_shortcode( $content );
}
return '';
} );
An example user of the non_member shortcode:
[non_member]
This is content for non-members only.
[/non_member]
Basic Membership plugin
Below is the full code for the Basic Membership plugin:
<?php
/**
* Plugin Name: Basic Membership
* Description: Create a basic membership site with a custom user role and protected content.
* Version: 1.0
* Author: Brian Gardner
* Author URI: https://briangardner.com/
*/
/**
* Create site member user role.
*/
add_action( 'init', function() {
add_role(
'site_member',
__( 'Member' ),
[
'read' => true,
'level_0' => true,
]
);
});
/**
* Redirect site members to site home after log in.
*/
function redirect_member_from_admin() {
if (is_admin() && !defined('DOING_AJAX') && is_user_logged_in()) {
$user = wp_get_current_user();
if (in_array( 'site_member', (array) $user->roles)) {
wp_redirect(home_url());
exit;
}
}
}
add_action( 'init', 'redirect_member_from_admin' );
/**
* Create site member shortcode.
*/
add_shortcode( 'site_member', function( $atts, $content = null ) {
if ( is_user_logged_in() && current_user_can( 'site_member' ) ) {
return do_shortcode( $content );
}
return '<p>You must be a member to view this content.</p>';
} );
/**
* Create non member shortcode.
*/
add_shortcode( 'non_member', function( $atts, $content = null ) {
if ( ! ( is_user_logged_in() && current_user_can( 'site_member' ) ) ) {
return do_shortcode( $content );
}
return '';
} );