Use WordPress Login

WordPress makes it pretty easy to log in and out of WordPress in a theme. Basically with two functions

<?php
$redirect = 'wp-admin/post-new.php';
if ( !is_user_logged_in() ) {
    $link = '<a href="' . esc_url( wp_login_url( $redirect ) ) . '">' . __('Login') . '</a>';
} else {
    $link = '<a href="' . esc_url( wp_logout_url( $redirect ) ) . '">' . __('Logout') . '</a>';
}
echo $link; // or echo apply_filters('loginout', $link);
?>

But I'm not going into details - in this article I would like to go a little further and show how, for example, the login form gets into the theme. Thus adjustments to the design and integration to your own needs can be implemented very easily.

In the first example I want to show you the classic login form so its not too much code. The following syntax shows a simple login form in your theme. I have not made any further adjustments and used the default login form of WordPress.

<form action="<?php echo wp_login_url(get_permalink()); ?>" method="post">
	<label for="log">
		<input type="text" name="log" id="log" value="<?php echo wp_specialchars(stripslashes($user_login), 1) ?>" size="22" /> User
	</label><br />
	<label for="pwd">
		<input type="password" name="pwd" id="pwd" size="22" /> Password
	</label><br />
	<input type="submit" name="submit" value="Send" class="button" />
	<label for="rememberme">
		<input name="rememberme" id="rememberme" type="checkbox" checked="checked" value="forever" /> Remember me
	</label>
</form>
<?php } ?>

This is not always sufficient and depends on the users who are logged, and whether one can even register on the website, I wrote a little example which pays attention to various aspects.
loginform

The following syntax is the view of logged user ( if ($ user_ID) ) , which is a completely different to new users, they get the form only if they are allowed to register (get_option('users_can_register')) , depending on the settings in the backend of WordPress.
Similarly, users with a User_Level greater than 1 (if ( $user_level >= 1 )) get one link more to write an article. Alternatively, you can also use the function current_user_can() and therefore explicitly asked for the user role.

<?php
global $user_ID, $user_identity, $user_level;
if ( $user_ID ) { ?>
	
	<h3><?php _e( 'Control panel', FB_BASIS_TEXTDOMAIN ); ?></h3>
	<ul>
		<li><?php _e( 'Identified as', FB_BASIS_TEXTDOMAIN ); ?> <strong><?php echo $user_identity ?></strong>.
			<ul>
				<li><a href="<?php bloginfo('url') ?>/wp-admin/"><?php _e( 'Dashboard', FB_BASIS_TEXTDOMAIN ); ?></a></li>
	
				<?php if ( $user_level >= 1 ) { ?>
					<li><a href="<?php bloginfo('url') ?>/wp-admin/post-new.php"><?php _e( 'Write an article', FB_BASIS_TEXTDOMAIN ); ?></a></li>
				<?php } ?>
	
				<li><a href="<?php bloginfo('url') ?>/wp-admin/profile.php"><?php _e( 'Profile', FB_BASIS_TEXTDOMAIN ); ?></a></li>
				<li><a href="<?php esc_url( wp_logout_url( urlencode($_SERVER['REQUEST_URI']) ) ); ?>"><?php _e( 'Exit', FB_BASIS_TEXTDOMAIN ); ?></a></li>
			</ul>
		</li>
	</ul>

<?php } elseif ( get_option('users_can_register') ) { ?>

		<h3><?php _e( 'Identification', FB_BASIS_TEXTDOMAIN ); ?></h3>
		<ul>
			<li>
				<form action="<?php echo wp_login_url(get_permalink()); ?>" method="post">
					<p>
						<label for="log">
							<?php _e( 'User', FB_BASIS_TEXTDOMAIN ); ?>
							<input type="text" name="log" id="log" value="<?php echo wp_specialchars(stripslashes($user_login), 1) ?>" size="22" />
						</label><br />
						<label for="pwd">
							<?php _e( 'Password', FB_BASIS_TEXTDOMAIN ); ?>
							<input type="password" name="pwd" id="pwd" size="22" />
						</label><br />
						<input type="submit" name="submit" value="<?php _e( 'Send', FB_BASIS_TEXTDOMAIN ); ?>" class="button" />
						<label for="rememberme">
							<input name="rememberme" id="rememberme" type="checkbox" checked="checked" value="forever" /><?php _e( 'Remember me', FB_BASIS_TEXTDOMAIN ); ?>
						</label><br />
						<input type="hidden" name="redirect_to" value="<?php echo $_SERVER['REQUEST_URI']; ?>"/>
					</p>
				</form>
			</li>
			<li>
				<a href="<?php echo site_url('wp-login.php?action=register', 'login'); ?>"><?php _e( 'Register', FB_BASIS_TEXTDOMAIN ); ?></a>
			</li>
			<li>
				<a href="<?php echo site_url('wp-login.php?action=lostpassword', 'login');	 ?>"><?php _e( 'Recover password', FB_BASIS_TEXTDOMAIN ); ?></a>
			</li>
		</ul>

<?php } ?>

If user level is insufficient, you may also extend WordPress, either via code or using a Plugin.
With the help of a few lines of PHP you can assign a role quickly.

function my_new_object() {
	global $wp_roles;
		
	$wp_roles->add_cap('administrator', 'MyObject');
}

// start an activation theme
if ( is_admin() && isset($_GET['activated'] ) && $pagenow == 'themes.php' )
	my_new_object();

The object can simply be requested via standard.
if ( current_user_can('MyObject') )

Alternatively you can also create new roles: add_role('New_Role', 'New name of the role');. The function also allows the simultaneous use of rights, therefore a third optional parameter is available, which expects several values in an array. Otherwise, the objects can be add via add_cap().

This is only a small approach, many opportunities arise - have fun with the benefits of these possibilities.

Comments are closed.

10 comments

  1. FreewareMatter

    I don't understand why you don't use the function is_user_logged_in() to check user is logged in or not (to show login form). You write it in the beginning of article, but don't use is anymore in the body.

  2. sriganesh

    thanks for sharing, but liitle detail for like me , new ppl plz :)

  3. Frank

    @FreewareMatter: i check with is_user_logged_in() only for register users. When users login in WordPress, then she become a other panel to use in frontend.

  4. Design Informer

    Just ran into this site today from the Six Revisions article and I love it. Your design is awesome and the content is even better.

    Needless to say, just added you guys to my favorites and will definitely be visiting often. Keep up the great work!

  5. FreewareMatter

    @Frank: I know that. But why don't you use it instead of $user_ID?

    We can replace:

    if ( $user_ID ) {} else {}

    with

    if ( is_user_logged_in() ) {} else {}

    I mean is_user_logged_in() is the same as $user_ID.

  6. Frank

    @FreewareMatter: You must set global this variable and its bether you use a function for compatibility in feature and secure.

  7. Frank

    @Design Informer: I love your comment, thanks!

  8. Colin

    Couple of questions for you..
    1> If the user makes an error on input to the login form he/she is redirected to the "real" login form. How would I detect the error and return to the in-page form instead?

    2> How can I get the registration form onto a page?

    What I would ultimately like to do is have private pages that display the login form if the viewer is not logged in, with a link to a reg form if they haven't yet subscribed. Once the viewer is reg'ed and logged in the page's content is made visible. All of this is done in one page using jquery. I have the basic mechanism working already, but the login errors and reg form are holding me back...

  9. Tamil Social Network

    How do I integrate 1 wordpress ID with another wordpress blog. I wanted to use particular blogs id to log in with other wordpress powered blogs..

One pingback

  1. wordpress login plugin | WORDPRESS