Post Pic

Add a login form on your WordPress Theme

Are you using WordPress as a cms or as a community site? If yes, it can be a cool idea to display a login form in your blog sidebar or on a specific page. Here’s a simple code to do it.

Nothing hard at all. Simply paste the following code where you'd like to display your login form. (For example, on your blog sidebar, or on a page template)

<<?php if (!(current_user_can('level_0'))){ ?>
<h2>Login</h2>
<form action="<?php echo get_option('home'); ?>/wp-login.php" method="post">
<input type="text" name="log" id="log" value="<?php echo wp_specialchars(stripslashes($user_login), 1) ?>" size="20" />
<input type="password" name="pwd" id="pwd" size="20" />
<input type="submit" name="submit" value="Send" class="button" />
    <p>
       <label for="rememberme"><input name="rememberme" id="rememberme" type="checkbox" checked="checked" value="forever" /> Remember me</label>
       <input type="hidden" name="redirect_to" value="<?php echo $_SERVER['REQUEST_URI']; ?>" />
    </p>
</form>
<a href="<?php echo get_option('home'); ?>/wp-login.php?action=lostpassword">Recover password</a>
<?php } else { ?>
<h2>Logout</h2>
<a href="<?php echo wp_logout_url(urlencode($_SERVER['REQUEST_URI'])); ?>">logout</a><br />
<a href="http://XXX/wp-admin/">admin</a>
<?php }?>

Once saved, your theme will display a login form. Definitely easier and quicker for login!

Related Posts

Related Posts

No related posts.

17 Responses

Oct 04 2011 04:35

I used this code but I think there is some error…

I can see the login box in my sidebar but the login field is not empty but show this line:

how can you fix this?
Thanks!

Oct 31 2011 23:40

You can see that there is an extra “<" at the beginning of this code. Remove it and all should be fine.

Nov 03 2011 02:44

@luca: I think there is an extra < in the beginning of the code. I haven't tried the script but it should start <?php not <<?php

Nov 20 2011 15:12

hey :)
that’s probably a very wimpy question,
but would somebody please tell me how to create the function that users have to use a login&password to read my wordpress blog?
thx!

Feb 04 2012 04:55

hey, the logging in part works fine, but when i click the log out button, it tries to redirect me somewhere and shows this “[an error occurred while processing this directive]”

actually, i’ve tried logging out a dozen times on 1 time it completed the process……any ideas?……this is what it puts in the url when i get the error: /%2Fcms%2F

Feb 23 2012 11:18

@bob :

i think that href value of url from “a” html tag maybe error or something else

you can change the href value from:

to something like this:

Reference :
http://codex.wordpress.org/Function_Reference/wp_logout_url

Apr 30 2012 11:09

How can I create a Login with oauth active? I usually have it on standard login page, but what about doing that into frontend? Thanks :-)

May 14 2012 21:59

My way to do this would be to install the ‘Shortcode Exec PHP’ plugin

using that plugin create a new short code [login] with the following:

wp_login_form( $args );

now when ever you use the short code [login] any where on your wordpress site the default login box appears.

May 26 2012 13:17

Thanks for the tip Stevie – exactly what I’d been searching high and low for….

Jun 29 2012 20:27

Great tutorial. I love it. Thank you.

Jul 24 2012 00:14

How do I make the log-in buttons function correctly?… When I click on them now, nothing happens.

Sep 11 2012 20:56

FYI, I was able to make it work when I changed

$_SERVER['REQUEST_URI']

to

$GLOBALS['HTTP_SERVER_VARS']['REQUEST_URI']

Oct 01 2012 14:10

Could you explain me how can i put into login forms messages (Example: “insert pass”).
when put the cursor into form the message disappear.
(Like this form “Your name”, “Your email” and “your site”)

Jan 23 2013 10:53

What about the (!(current_user_can(‘level_0′)) function, this is undefined …… Please let me know, and i am new to wordpress

Feb 13 2013 15:37

I really need this plugin but it seems not working.. :( (

custom page output below:

Recover password

Logout
admin

Mar 05 2013 12:25

The code provided works perfectly in localhost, but when I upload it to Live Server it doesn’t work ! I have updated permalinks. But after pressing the Send button it reloads the same page without logging in.

May 16 2013 03:56

You can also just insert the following function into your page or post:
<?php wp_login_form(); ?>

Trackbacks:

Leave a Comment

* Name, Email, Comment are Required

WP Theme of the week

Sponsored Likebox