WordPress.org

Codex

Interested in functions, hooks, classes, or methods? Check out the new WordPress Code Reference!

Function Reference/wp loginout

Description

Displays a login link, or if a user is logged in, displays a logout link. An optional, redirect argument can be used to redirect the user upon login or logout.

Usage

 <?php wp_loginout$redirect$echo ); ?> 

Parameters

$redirect
(string) (optional) URL to redirect to on login/logout.
Default: None
$echo
(boolean) (optional) If true, echos the link, if false, returns the link as a string.
Default: true

Examples

Basic Example

<p><?php wp_loginout(); ?></p>

Add Log in/out Link To Nav Menu

Simply add this code to your parent or child themes functions.php file to display a log in/out link in the secondary navigation menu of the Twenty Fourteen default theme for WordPress.

add_filter( 'wp_nav_menu_secondary_items','wpsites_loginout_menu_link' );

function wpsites_loginout_menu_link( $menu ) {
    $loginout = wp_loginout($_SERVER['REQUEST_URI'], false );
    $menu .= $loginout;
    return $menu;
}

Other themes like Twenty Thirteen may require you to add a class to the code like this example.

$loginout = '<li class="nav-menu" class="menu-item">' . wp_loginout($_SERVER['REQUEST_URI'], false ) . '</li>';

Notes

Change Log

Since: 1.5

The $redirect parameter is added in Version 2.8.

Source File

wp_loginout() is located in wp-includes/general-template.php.

Related

Login Tags: is_user_logged_in(), wp_login_form(), wp_loginout(), wp_logout(), wp_register()
Login URLs: wp_login_url(), wp_logout_url(), wp_lostpassword_url(), wp_registration_url()

See also index of Function Reference and index of Template Tags.