WordPress Hacks for Members Only Website

By default wordrepss is blogging tool. Nevertheless, you can truly customize to make it however you want. Many of people use wordpress to create members only website. You can only always use a plugin, but if you only needed a little modification, why install a plugin when you can do it with few lines of code. Here are some simple yet effective hacks for creating a member only site.

Content for Members only Without Plugin

If there are certain content on your site you wanted to show only to the registered member, you can do so by use the following code in your theme. Whatever goes between is_user_logged_in will be seen by only members.

[php]<?php if ( is_user_logged_in() ) { ?>
// Content for Logged in user
<?php } else {?>
// Content for everyone else
<?php }?>

Show WordPress Login Form Anywhere and Customize It

If you want include wordpress login form anywhere in your theme, which is mentioned in this post you can do so by using following function:

<?php wp_login_form(); ?>

However, it does not give you option to customize the login form, because it just directly pulls default wordpress login form. What if you want show something like this?

Here is code to do that. Paste the following code wherever you want the form to show up.

// WordPress Login Form
<?php if (!(current_user_can(‘level_0’))){ ?>
<h3>Member Login</h3>
<form action="<?php echo get_option(‘home’); ?>/wp-login.php" method="post">
<table width=’100%’ cellspacing="0" cellpadding="0">
<label><?php _e( ‘Username’ ) ?></label>
<input type="text" name="log" class="input" value="<?php echo attribute_escape(stripslashes($user_login)); ?>" />
<label><?php _e( ‘Password’ ) ?></label>
<input type="password" name="pwd" class="input" value="" />
<td colspan="2"><?php do_action(‘login_form’); ?>
<a href="<?php echo site_url(‘wp-login.php?action=lostpassword’, ‘login’) ?>" title="<?php _e(‘Password Lost and Found’) ?>"><?php _e(‘Forgot Password?’) ?></a>
<input type="submit" name="submit" value="Log In" class="button" />
<input type="hidden" name="redirect_to" value="http://<?php echo $_SERVER[‘SERVER_NAME’] . $_SERVER[‘REQUEST_URI’] ?>" />
<?php } else { ?>
<!– When User logged in Show the follwing info –>
<?php get_currentuserinfo();?>
Welcome Back &nbsp<?php echo($current_user->user_login . " ");?><br />
<a href="<?php echo wp_logout_url(urlencode($_SERVER[‘REQUEST_URI’])); ?>">Logout</a><br />
<a href="<?php echo get_admin_url(); ?>">Admin?</a><!– If you are admin goes to admin dashboard –>
<?php }?>

You can add a CSS div tag to style it. The advantage of using this login form you can almost create a mini dashboard for members by using get_currentuserinfo() function and show any information about the user.

Custom WordPress Menu for Members

Like content you can create custom wordpress  (3.0 only) menu for members.  Let’s register the menu first. Go to your theme function file (functions.php) . Use the following code to register the menus.

register_nav_menus( array(
‘primary’ => __( ‘Primary Navigation’, ‘twentyten’ ),
‘visitor’ => __( ‘Visitor Navigation’, ‘twentyten’ ),
‘footer’ => __( ‘Footer Navigation’, ‘twentyten’ ),

) );

Now,  put following code where you want your menu to show up, most probably  inside the header.php file.

if ( is_user_logged_in() ) {
wp_nav_menu( array( ‘container_class’ => ‘menu-header’, ‘theme_location’ => ‘primary’ ) );
} else {
wp_nav_menu( array( ‘container_class’ => ‘menu-header’, ‘theme_location’ => ‘visitor’ ) );

Now you need to actually create the menu on wordpress backhand . The “Menus” setting should be under Appearance tab. Create a menu with the name main and visitor. On both menu add whatever you want to show. For example: if you want a page to show up only when user looged, add it in the main menu . Look at the following image.

Here is live example of how this works.

You can also create a dynamic footer menu using the same method.

  1. Function Reference/wp nav menu

That is it for now. We will be back with more wordpress hacks. Do not forget subscribe.


Kawsar Ali is a web designer who also likes blogging. In his spare time he walks around with a Nikon D5100 hunting for good textures.

  • http://www.hargaemas.my lochoe

    how to make the menu auto refresh, after user login, the default visitor menu still appear?

  • Marlon Minatra

    Useful information as per usual, ty. I certainly hope this kind of thing gets more attention.

  • Web 2.0 Design

    Interesting Post. I really enjoy this post.. Thanks for sharing this…

Pin It on Pinterest

Share This