Pin It

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 }?>
[/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]
<?php wp_login_form(); ?>
[/php]

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.

[php]
// 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">
<tr>
<td>
<label><?php _e( ‘Username’ ) ?></label>
<input type="text" name="log" class="input" value="<?php echo attribute_escape(stripslashes($user_login)); ?>" />
</td>
<td>
<label><?php _e( ‘Password’ ) ?></label>
<input type="password" name="pwd" class="input" value="" />
</td>
</tr>
<tr>
<td colspan="2"><?php do_action(‘login_form’); ?>
</td>
</tr>
<tr>
<td>
<a href="<?php echo site_url(‘wp-login.php?action=lostpassword’, ‘login’) ?>" title="<?php _e(‘Password Lost and Found’) ?>"><?php _e(‘Forgot Password?’) ?></a>
</td>
<td>
<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'] ?>" />
</td>
</tr>
</table>
</form>
<?php } else { ?>
<!– When User logged in Show the follwing info –>
<h2>Logout</h2>
<?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 }?>
[/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.

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

) );
[/php]

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

[php]
<?php
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’ ) );
};?>
[/php]

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.


Author:

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…