函数文档

wp_loginout()

💡 云策文档标注

概述

wp_loginout() 函数用于在 WordPress 中显示登录/登出链接,根据用户是否已登录动态切换链接内容。它支持可选的重定向参数和输出控制,适用于主题或插件开发。

关键要点

  • 函数根据 is_user_logged_in() 判断用户状态,生成对应的登录或登出链接。
  • 参数 $redirect 可选,指定登录或登出后的重定向路径;$display 控制是否直接输出链接或返回字符串。
  • 使用 apply_filters('loginout', $link) 钩子允许开发者过滤链接的 HTML 输出。
  • 返回类型为 void 或 string,取决于 $display 参数的值。
  • 相关函数包括 wp_login_url()、wp_logout_url() 和 esc_url() 等,用于辅助处理 URL。

代码示例

// 基本用法:直接输出登录/登出链接
wp_loginout();

// 返回链接字符串,用于自定义输出
$link = wp_loginout('', false);

// 在导航菜单中添加登录/登出链接的示例
add_filter('wp_nav_menu_secondary_items', 'wpdocs_loginout_menu_link');
function wpdocs_loginout_menu_link($menu) {
    $loginout = wp_loginout($_SERVER['REQUEST_URI'], false);
    $menu .= $loginout;
    return $menu;
}

注意事项

  • 确保在主题或插件中正确使用此函数,避免安全风险,如使用 esc_url() 处理重定向 URL。
  • 注意 $display 参数的默认值为 true,意味着默认会直接输出链接,若需进一步处理应设置为 false。
  • 钩子 loginout 可用于自定义链接的 HTML 内容,增强灵活性。

📄 原文内容

Displays the Log In/Out link.

Description

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.

Parameters

$redirectstringoptional
Optional path to redirect to on login/logout.
$displaybooloptional
Default to echo and not return the link.

Default:true

Return

void|string Void if $display argument is true, log in/out link if $display is false.

Source

function wp_loginout( $redirect = '', $display = true ) {
	if ( ! is_user_logged_in() ) {
		$link = '<a href="' . esc_url( wp_login_url( $redirect ) ) . '">' . __( 'Log in' ) . '</a>';
	} else {
		$link = '<a href="' . esc_url( wp_logout_url( $redirect ) ) . '">' . __( 'Log out' ) . '</a>';
	}

	if ( $display ) {
		/**
		 * Filters 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 );
	}
}

Hooks

apply_filters( ‘loginout’, string $link )

Filters the HTML output for the Log In/Log Out link.

Changelog

Version Description
1.5.0 Introduced.

User Contributed Notes

  1. Skip to note 4 content

    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','wpdocs_loginout_menu_link' );
    
    /**
     * Append Login In/Out link to menu with a redirect to this page
     */
    function wpdocs_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>';