You could just copy the form off the wp-login.php page, however it’s not very well marked up, plus you will probably want to fit it into a smaller area on the front end of your site, maybe in your sidebar or at the top right of your web site.
The Login Form
The first job is to put the login form where you want it to go. The code for this is below:
- <form id="loginform" action="< ?php echo get_option('home'); ?>/wp-login.php" method="post">
- <fieldset><legend>Login</legend>
- <div>
- <label for="log">Username
- <input type="text" name="log" id="log" size="20" /></label>
- </div>
- <div>
- <label for="pwd">Password
- <input type="password" name="pwd" id="pwd" size="20" /></label>
- </div>
- <div>
- <label><input name="rememberme" type="checkbox" id="rememberme" value="forever" /> Remember Me</label>
- </div>
- <div>
- <input type="submit" name="wp-submit" id="wp-submit" value="Log In" />
- <input type="hidden" name="redirect_to" value="< ?php echo get_option('home'); ?/>/wp-admin/" />
- </div>
- </fieldset>
- </form>
The above code will allow you to log into your admin area from anywhere on the site.
If you want to allow people to log in but return to the page they logged in from, then you’ll need to change the redirect value on line 16 to be
<input type="hidden" name="redirect_to" value="<?php echo urlencode(get_permalink()); ?>" />
Logged In Users
Of course we don’t need the form displaying if the person is logged in, so we need to add a bit of additional PHP to check if the user is logged in. If they aren’t then we display the form, if they are then we can either display nothing, or display a link to the admin and perhaps a logout link.
To check if the user is logged in we use the is_user_logged_in() conditional, eg.
- < ?php if (!is_user_logged_in()) : ?>
- <form id="loginform" action="< ?php echo get_option('home'); ?>/wp-login.php" method="post">
- <fieldset><legend>Login</legend>
- <div>
- <label for="log">Username
- <input type="text" name="log" id="log" size="20" /></label>
- </div>
- <div>
- <label for="pwd">Password
- <input type="password" name="pwd" id="pwd" size="20" /></label>
- </div>
- <div>
- <label><input name="rememberme" type="checkbox" id="rememberme" value="forever" /> Remember Me</label>
- </div>
- <div>
- <input type="submit" name="wp-submit" id="wp-submit" value="Log In" />
- <input type="hidden" name="redirect_to" value="< ?php echo urlencode(get_permalink()); ?/>" />
- </div>
- </fieldset>
- </form>
- < ?php else : ?>
- <ul>
- <li><a href="< ?php echo get_option('home'); ?>/wp-admin/">WP Admin</a></li>
- <li><a href="< ?php echo wp_logout_url(get_permalink()); ?>">Logout</a></li>
- </ul>
- < ?php endif; ?>
This is quite a simple addition but a very useful one for any site that has more than just the admin logging in.
0 comments:
Post a Comment