show_blog_form()
概述
show_blog_form() 函数用于生成并显示 WordPress 多站点环境中的站点注册和创建表单。它处理站点名称、标题的输入,并集成错误验证和语言选择功能。
关键要点
- 函数接受三个参数:$blogname(站点名称)、$blog_title(站点标题)和 $errors(WP_Error 对象或字符串,用于错误处理)。
- 根据子域名安装配置(is_subdomain_install())动态调整表单字段标签和提示。
- 自动验证并显示与 blogname 和 blog_title 相关的错误消息,增强用户体验。
- 包含语言选择器(wp_dropdown_languages()),允许用户在注册时选择站点语言。
- 集成了多个 WordPress 核心函数和 Hook,如 do_action('signup_blogform') 用于扩展表单行为。
代码示例
function show_blog_form( $blogname = '', $blog_title = '', $errors = '' ) {
if ( ! is_wp_error( $errors ) ) {
$errors = new WP_Error();
}
// 表单生成逻辑...
}注意事项
- 此函数主要用于多站点(MU)环境,自 WordPress 3.0.0 版本引入。
- 错误参数 $errors 应使用 WP_Error 对象以确保正确验证和显示。
- 表单输出依赖于当前网络设置和用户登录状态,需在适当上下文中调用。
Generates and displays the Sign-up and Create Site forms.
Parameters
Source
function show_blog_form( $blogname = '', $blog_title = '', $errors = '' ) {
if ( ! is_wp_error( $errors ) ) {
$errors = new WP_Error();
}
$current_network = get_network();
// Site name.
if ( ! is_subdomain_install() ) {
echo '<label for="blogname">' . __( 'Site Name (subdirectory only):' ) . '</label>';
} else {
echo '<label for="blogname">' . __( 'Site Domain (subdomain only):' ) . '</label>';
}
$errmsg_blogname = $errors->get_error_message( 'blogname' );
$errmsg_blogname_aria = '';
if ( $errmsg_blogname ) {
$errmsg_blogname_aria = 'wp-signup-blogname-error ';
echo '<p class="error" id="wp-signup-blogname-error">' . $errmsg_blogname . '</p>';
}
if ( ! is_subdomain_install() ) {
echo '<div class="wp-signup-blogname"><span class="prefix_address" id="prefix-address">' . $current_network->domain . $current_network->path . '</span><input name="blogname" type="text" id="blogname" value="' . esc_attr( $blogname ) . '" maxlength="60" autocomplete="off" required="required" aria-describedby="' . $errmsg_blogname_aria . 'prefix-address" /></div>';
} else {
$site_domain = preg_replace( '|^www.|', '', $current_network->domain );
echo '<div class="wp-signup-blogname"><input name="blogname" type="text" id="blogname" value="' . esc_attr( $blogname ) . '" maxlength="60" autocomplete="off" required="required" aria-describedby="' . $errmsg_blogname_aria . 'suffix-address" /><span class="suffix_address" id="suffix-address">.' . esc_html( $site_domain ) . '</span></div>';
}
if ( ! is_user_logged_in() ) {
if ( ! is_subdomain_install() ) {
$site = $current_network->domain . $current_network->path . __( 'sitename' );
} else {
$site = __( 'domain' ) . '.' . $site_domain . $current_network->path;
}
printf(
'<p>(<strong>%s</strong>) %s</p>',
/* translators: %s: Site address. */
sprintf( __( 'Your address will be %s.' ), $site ),
__( 'Must be at least 4 characters, letters and numbers only. It cannot be changed, so choose carefully!' )
);
}
// Site Title.
?>
<label for="blog_title"></label>
get_error_message( 'blog_title' );
$errmsg_blog_title_aria = '';
if ( $errmsg_blog_title ) {
$errmsg_blog_title_aria = ' aria-describedby="wp-signup-blog-title-error"';
echo '<p class="error" id="wp-signup-blog-title-error">' . $errmsg_blog_title . '</p>';
}
echo '<input name="blog_title" type="text" id="blog_title" value="' . esc_attr( $blog_title ) . '" required="required" autocomplete="off"' . $errmsg_blog_title_aria . ' />';
?>
<p>
<label for="site-language"></label>
'WPLANG',
'id' => 'site-language',
'selected' => $lang,
'languages' => $languages,
'show_available_translations' => false,
)
);
?>
</p>
<div id="privacy">
<fieldset class="privacy-intro">
<legend>
<span class="label-heading"></span>
</legend>
<p class="wp-signup-radio-buttons">
<span class="wp-signup-radio-button">
<input type="radio" id="blog_public_on" name="blog_public" value="1" <?php echo $blog_public_on_checked; ?> />
<label class="checkbox" for="blog_public_on"></label>
</span>
<span class="wp-signup-radio-button">
<input type="radio" id="blog_public_off" name="blog_public" value="0" <?php echo $blog_public_off_checked; ?> />
<label class="checkbox" for="blog_public_off"></label>
</span>
</p>
</fieldset>
</div>
</pre><p class="wporg-dot-link-list"><a href="https://developer.wordpress.org/reference/files/wp-signup.php/">View all references</a> <a href="https://core.trac.wordpress.org/browser/tags/6.9.4/src/wp-signup.php#L117">View on Trac</a> <a href="https://github.com/WordPress/wordpress-develop/blob/6.9.4/src/wp-signup.php#L117-L244">View on GitHub</a></p></section>
<section class="wp-block-wporg-code-reference-hooks"><h2 id="hooks" class="is-toc-heading wp-block-heading has-heading-5-font-size" tabindex="-1" ><a href="#hooks">Hooks</a></h2> <dl><dt class="wp-block-wporg-code-reference-title has-normal-font-size"><a href="https://developer.wordpress.org/reference/hooks/signup_blogform/"><span class="hook-func">do_action</span>( ‘signup_blogform’, <nobr><span class="arg-type">WP_Error</span> <span class="arg-name">$errors</span></nobr> )</a></dt><dd><p>Fires after the site sign-up form.</p>
</dd></dl></section>
<section class="wp-block-wporg-code-reference-related" data-nosnippet="true"><h2 id="related" class="is-toc-heading wp-block-heading has-heading-5-font-size" tabindex="-1" ><a href="#related">Related</a></h2> <section style="margin-top:var(--wp--preset--spacing--20)" class="wp-block-wporg-code-table" id="uses"><figure class="wp-block-table "><table><thead><tr><th scope="col">Uses</th><th scope="col">Description</th></tr></thead><tbody><tr class=""><td><a href="https://developer.wordpress.org/reference/functions/get_network/">get_network()</a><code>wp-includes/ms-network.php
Retrieves network data given a network ID or network object.
signup_get_available_languages()wp-signup.php
Retrieves languages available during the site/user sign-up process.
wp_dropdown_languages()wp-includes/l10n.php
Displays or returns a Language selector.
is_subdomain_install()wp-includes/ms-load.php
Whether a subdomain configuration is enabled.
__()wp-includes/l10n.php
Retrieves the translation of $text.
_e()wp-includes/l10n.php
Displays translated text.
esc_attr()wp-includes/formatting.php
Escaping for HTML attributes.
esc_html()wp-includes/formatting.php
Escaping for HTML blocks.
is_user_logged_in()wp-includes/pluggable.php
Determines whether the current visitor is a logged in user.
do_action()wp-includes/plugin.php
Calls the callback functions that have been added to an action hook.
get_site_option()wp-includes/option.php
Retrieve an option value for the current network based on name of option.
is_wp_error()wp-includes/load.php
Checks whether the given variable is a WordPress Error.
WP_Error::__construct()wp-includes/class-wp-error.php
Initializes the error.
| Used by | Description |
|---|---|
signup_another_blog()wp-signup.php |
Shows a form for returning users to sign up for another site. |
signup_blog()wp-signup.php |
Shows a form for a user or visitor to sign up for a new site. |
Changelog
| Version | Description |
|---|---|
| MU (3.0.0) | Introduced. |