Use WordPress Login

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

' . __('Login') . '';
} else {
    $link = '' . __('Logout') . '';
}
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.



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.


	
	

  • .
    • = 1 ) { ?>




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.


Posted

in

by

Comments

10 responses to “Use WordPress Login”

  1. FreewareMatter Avatar

    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 Avatar

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

  3. Frank Avatar

    @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 Avatar

    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 Avatar

    @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 Avatar

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

  7. Frank Avatar

    @Design Informer: I love your comment, thanks!

  8. […] Use WordPress Login – , WordPress, Thus, Basically – WP Engineer […]

  9. Colin Avatar
    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…

  10. Tamil Social Network Avatar

    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..