Desizn Tech logo
  • Home
  • Freebies
  • Showcase
  • Tutorial
  • Photography

WordPress Hacks for Members Only Website

Posted February 21, 2011 & filed under Tutorial, Wordpress

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 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">
     <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 }?>

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.


  • Function Reference/wp login form
  • Function Reference/get currentuserinfo
  • Add a login form on your WordPress Theme

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.

<?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' ) );
};?>

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.

More about Kawsar Ali

About the author

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

Tags: members, menu, navigation, wordpress hack, wp_login


Related posts:
  • Remove WordPress Meta
  • WordPress Hacks and Tweaks to Visually Empower Your Website
  • WordPress Custom Fields and Hacks for Bloggers
  • WordPress and jQuery: Troubleshooting, Implementing Sidebar Tabs and More
  • M&M: A Free Modern, Minimalist and Smart WordPress Theme

Comments

Due to a high number of spam comments on site we have disabled comment system at desizntech.info. In a few days comments will be available again after we delete spam.

Cheap Tablet PC

Stay Connected

rss twitter facebook google pinterest
Get free updates in your inbox!

Subscribe via rss

Our Supporters

  • Cloud Storage
  • Dedicated Server
  • Modern Furniture
  • Online Business Printing
  • Online Backup
  • Logo Design
  • Wow Gold
  • Web Hosting UK
  • Secure and protect your business with SiteLock.
  • Festklänningar
  • Blogroll

    AnimHut
    Designmodo – Web Design
    Design Superhero
    Inspireyourway
    iBrandStudio
    OurTuts
    Skyje
    TextureQualityPro
    Stock Images
    Vector Pack
    365icon
    TexTuts
    • About
    • Contact
    • Contribute
    • Privacy Policy

    Copyright © 2008-2013 Desizn Tech

    Theme by Kawsar Ali.