wp_loginout()获取一个退出链接

目录

描述

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.

wp_loginout() 描述

用法

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

wp_loginout() 用法

参数

$redirect

(string) (可选) URL to redirect to on login/logout.

默认值: None

$echo

(boolean) (可选) If true, echos the link, if false, returns the link as a string.

默认值: true

wp_loginout() 参数

示例

Basic Example

/* ----------------------------------
 * wordpress之魂 © http://wphun.com
 * ---------------------------------- */

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

/* ----------------------------------
 * wordpress之魂 © http://wphun.com
 * ---------------------------------- */

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.

/* ----------------------------------
 * wordpress之魂 © http://wphun.com
 * ---------------------------------- */

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

wp_loginout() 示例

注意

wp_loginout() 注意

历史

添加于 版本: 1.5

The $redirect parameter is added in Version 2.8.

wp_loginout() 历史

源文件

wp_loginout() 函数的代码位于 wp-includes/general-template.php.

/* ----------------------------------
 * wordpress之魂 © http://wphun.com
 * ---------------------------------- */
/**
 * Display the Log In/Out link.
 *
 * Displays a link, which allows users to navigate to the Log In page to log in
 * or log out depending on whether they are currently logged in.
 *
 * @since 1.5.0
 *
 * @param string $redirect Optional path to redirect to on login/logout.
 * @param bool   $echo     Default to echo and not return the link.
 * @return string|void String when retrieving.
 */
function wp_loginout($redirect = '', $echo = true) {
	if ( ! is_user_logged_in() )
		$link = '' . __('Log in') . '';
	else
		$link = '' . __('Log out') . '';

	if ( $echo ) {
		/**
		 * Filter the HTML output for the Log In/Log Out link.
		 *
		 * @since 1.5.0
		 *
		 * @param string $link The HTML link content.
		 */
		echo apply_filters( 'loginout', $link );
	} else {
		/** This filter is documented in wp-includes/general-template.php */
		return apply_filters( 'loginout', $link );
	}
}

wp_loginout() 源文件

相关