Blog

DIY WordPress Membership

This month is Slocum Studio’s look at membership on the WordPress framework.

We recently featured a video where we talk exclusively about the benefits of implementing membership plugins in a WordPress-powered website.

But what if you are a developer or designer who wants to implement membership on a client’s website?

How about your own website or blog?

Here’s what to do:

Setup and Example

All custom page templates are added on the server side of your WordPress website.

Within the active theme folder, create a new file (e.g. page-members-only.php) for the custom page template. Place the following code code at the start of your new custom page template after opening PHP (<!–?php):

[php] /*
Template Name: Members Only
*/
[/php]

http://codex.wordpress.org/Pages#Creating_Your_Own_Page_Templates

Now, for some practical applications of the custom page template.

Content for the Logged-in User

This particular example checks to see if the user is logged in and if they aren’t re-directs the user to the login page.

[php] <?php
/*
Template Name: Members Only
*/

// Check to see if the current user is logged in
// This has to be done before output so we can re-direct the user

if ( !is_user_logged_in( ) ) { // User is not logged in, re-direct them to the login screen
wp_redirect( wp_login_url( ) );
exit;
}

get_header( ); ?>

<div id="primary" class="site-content">
<div id="content" role="main">

<?php while ( have_posts( ) ) : the_post( ); ?>
<?php get_template_part( ‘content’, ‘page’ ); ?>
<?php comments_template( ”, true ); ?>
<?php endwhile; // end of the loop. ?>

</div><!– #content –>
</div><!– #primary –>

<?php get_sidebar( ); ?>
<?php get_footer( ); ?>
[/php]

http://codex.wordpress.org/Function_Reference/is_user_logged_in

http://codex.wordpress.org/Function_Reference/wp_redirect

http://codex.wordpress.org/Function_Reference/wp_login_url

Check the User Role (Admin, Author, Reader, etc.)

This example first checks to see if the user is logged in, and if they are, then determines if the user is an administrator. If the user is not an administrator, they are re-directed back to the home page. If the user is an administrator, the content is displayed.

If the user is not logged in, they are re-directed to the login screen.

[php] <?php
/*
Template Name: Members Only
*/

// Check to see if the current user is logged in
// This has to be done before output so we can re-direct the user
if ( is_user_logged_in( ) )
if ( !current_user_can( ‘manage_options’ ) ) { // If the current user is not an administratior
wp_redirect( home_url( ) );
exit;
}
else {
wp_redirect( wp_login_url( ) );
exit;
}

get_header( ); ?>

<div id="primary" class="site-content">
<div id="content" role="main">

<?php while ( have_posts( ) ) : the_post( ); ?>
<?php get_template_part( ‘content’, ‘page’ ); ?>
<?php comments_template( ”, true ); ?>
<?php endwhile; // end of the loop. ?>

</div><!– #content –>
</div><!– #primary –>

<?php get_sidebar( ); ?>
<?php get_footer( ); ?>
[/php]

http://codex.wordpress.org/Function_Reference/current_user_can

http://codex.wordpress.org/Roles_and_Capabilities

http://codex.wordpress.org/Function_Reference/home_url

Display Login Form

This example will display a WordPress login form if the current user is not logged in.

[php] <?php
/*
Template Name: Members Only
*/

get_header( ); ?>

<div id="primary" class="site-content">
<div id="content" role="main">
<?php
// Check to see if the current user is logged in
if ( is_user_logged_in( ) ) {
while ( have_posts( ) ) : the_post( );
?>

<?php
endwhile; // end of the loop.
}
// User is not logged in (display message and WordPress login form)
else {
?>
<p>Please login to view this content.</p>
<?php wp_login_form( ); ?>
<?php
}
?>

</div><!– #content –>
</div><!– #primary –>

<?php get_sidebar( ); ?>
<?php get_footer( ); ?>
[/php]

http://codex.wordpress.org/Function_Reference/wp_login_form

Additional Tips

For additional help on how to make your subscription-based website fly, let us know in the comments below.  We will be discussing these subscription plugins quite a lot in the coming month and will have more help as we go.

If you want read those pages and see those videos as they come around, please subscribe.

Leave a Reply

Your email address will not be published.