函数文档

wp_register()

💡 云策文档标注

概述

wp_register() 函数用于显示注册或管理链接,根据用户登录状态和注册设置动态输出。它支持自定义前后文本,并可选择直接输出或返回链接字符串。

关键要点

  • 功能:显示注册链接(未登录且注册启用时)或管理链接(已登录时)。
  • 参数:$before(链接前文本,默认空)、$after(链接后文本,默认空)、$display(是否直接输出,默认 true)。
  • 返回值:$display 为 true 时返回 void,为 false 时返回链接字符串。
  • 条件:注册链接仅在“设置 > 常规 > 会员资格:任何人都可以注册”启用时显示。
  • Hook:提供 apply_filters('register', $link) 用于过滤链接 HTML。

代码示例

function wp_register( $before = '', $after = '', $display = true ) {
    if ( ! is_user_logged_in() ) {
        if ( get_option( 'users_can_register' ) ) {
            $link = $before . '<a href="' . esc_url( wp_registration_url() ) . '">' . __( 'Register' ) . '</a>' . $after;
        } else {
            $link = '';
        }
    } elseif ( current_user_can( 'read' ) ) {
        $link = $before . '<a href="' . admin_url() . '">' . __( 'Site Admin' ) . '</a>' . $after;
    } else {
        $link = '';
    }
    $link = apply_filters( 'register', $link );
    if ( $display ) {
        echo $link;
    } else {
        return $link;
    }
}

注意事项

  • 默认用法:wp_register() 以列表格式显示链接,需结合 HTML 结构使用。
  • 无前后文本示例:调用 wp_register('', '', true) 可输出纯链接,无额外文本包裹。

📄 原文内容

Displays the Registration or Admin link.

Description

Display a link which allows the user to navigate to the registration page if not logged in and registration is enabled or to the dashboard if logged in.

Parameters

$beforestringrequired
Text to output before the link. Default <li>.
$afterstringrequired
Text to output after the link. Default </li>.
$displaybooloptional
Default to echo and not return the link.

Default:true

Return

void|string Void if $display argument is true, registration or admin link if $display is false.

More Information

The “Register” link is not offered if the Administration > Settings > General > Membership: Anyone can register box is not checked.

Source

function wp_register( $before = '<li>', $after = '</li>', $display = true ) {
	if ( ! is_user_logged_in() ) {
		if ( get_option( 'users_can_register' ) ) {
			$link = $before . '<a href="' . esc_url( wp_registration_url() ) . '">' . __( 'Register' ) . '</a>' . $after;
		} else {
			$link = '';
		}
	} elseif ( current_user_can( 'read' ) ) {
		$link = $before . '<a href="' . admin_url() . '">' . __( 'Site Admin' ) . '</a>' . $after;
	} else {
		$link = '';
	}

	/**
	 * Filters the HTML link to the Registration or Admin page.
	 *
	 * Users are sent to the admin page if logged-in, or the registration page
	 * if enabled and logged-out.
	 *
	 * @since 1.5.0
	 *
	 * @param string $link The HTML code for the link to the Registration or Admin page.
	 */
	$link = apply_filters( 'register', $link );

	if ( $display ) {
		echo $link;
	} else {
		return $link;
	}
}

Hooks

apply_filters( ‘register’, string $link )

Filters the HTML link to the Registration or Admin page.

Changelog

Version Description
1.5.0 Introduced.

User Contributed Notes

  1. Skip to note 4 content

    Display Without Text Before or After
    The following code example displays the “Register” or “Site Admin” link with no text in before or after parameters.

    When not logged in the following HTML is produced:

    <a href="<a href="http://www.example.com/wp-login.php?action=register">Register</a&gt">http://www.example.com/wp-login.php?action=register">Register</a&gt<>;

    When logged in the following HTML is produced:

    <a href="<a href="http://www.example.com/wp-admin/">Site" rel="nofollow ugc">http://www.example.com/wp-admin/">Site</a> Admin</a>