signup_another_blog()
概述
signup_another_blog() 函数用于为已登录用户显示一个注册新站点的表单。它处理站点名称、标题和错误参数,并输出相关表单内容。
关键要点
- 函数接受三个参数:$blogname(新站点名称)、$blog_title(新站点标题)和 $errors(WP_Error 对象或字符串,默认为空字符串)。
- 使用 signup_another_blog_init 过滤器允许开发者修改默认的站点注册变量。
- 函数会检查当前用户是否已有站点,并显示现有站点列表,同时输出隐藏字段和表单。
- 涉及多个相关函数和 Hook,如 get_network()、show_blog_form() 和 do_action('signup_hidden_fields')。
代码示例
function signup_another_blog( $blogname = '', $blog_title = '', $errors = '' ) {
$current_user = wp_get_current_user();
if ( ! is_wp_error( $errors ) ) {
$errors = new WP_Error();
}
$signup_defaults = array(
'blogname' => $blogname,
'blog_title' => $blog_title,
'errors' => $errors,
);
$filtered_results = apply_filters( 'signup_another_blog_init', $signup_defaults );
$blogname = $filtered_results['blogname'];
$blog_title = $filtered_results['blog_title'];
$errors = $filtered_results['errors'];
// 输出表单标题和错误信息
echo '' . sprintf( __( 'Get another %s site in seconds' ), get_network()->site_name ) . '';
if ( $errors->has_errors() ) {
echo '' . __( 'There was a problem, please correct the form below and try again.' ) . '';
}
}注意事项
- 该函数自 WordPress MU 3.0.0 版本引入,主要用于多站点环境。
- 错误处理通过 WP_Error 对象进行,需确保正确传递或初始化。
- 表单输出包括隐藏字段,可通过 signup_hidden_fields 动作钩子进行自定义。
Shows a form for returning users to sign up for another site.
Parameters
Source
function signup_another_blog( $blogname = '', $blog_title = '', $errors = '' ) {
$current_user = wp_get_current_user();
if ( ! is_wp_error( $errors ) ) {
$errors = new WP_Error();
}
$signup_defaults = array(
'blogname' => $blogname,
'blog_title' => $blog_title,
'errors' => $errors,
);
/**
* Filters the default site sign-up variables.
*
* @since 3.0.0
*
* @param array $signup_defaults {
* An array of default site sign-up variables.
*
* @type string $blogname The site blogname.
* @type string $blog_title The site title.
* @type WP_Error $errors A WP_Error object possibly containing 'blogname' or 'blog_title' errors.
* }
*/
$filtered_results = apply_filters( 'signup_another_blog_init', $signup_defaults );
$blogname = $filtered_results['blogname'];
$blog_title = $filtered_results['blog_title'];
$errors = $filtered_results['errors'];
/* translators: %s: Network title. */
echo '<h2>' . sprintf( __( 'Get <em>another</em> %s site in seconds' ), get_network()->site_name ) . '</h2>';
if ( $errors->has_errors() ) {
echo '<p>' . __( 'There was a problem, please correct the form below and try again.' ) . '</p>';
}
?>
<p>
add another site to your account</strong>. There is no limit to the number of sites you can have, so create to your heart’s content, but write responsibly!' ),
$current_user->display_name
);
?>
</p>
ID );
if ( ! empty( $blogs ) ) {
?>
<p></p>
<ul>
userblog_id );
echo '<li><a href="' . esc_url( $home_url ) . '">' . $home_url . '</a></li>';
}
?>
</ul>
<p></p>
<form id="setupform" method="post" action="wp-signup.php">
<input type="hidden" name="stage" value="gimmeanotherblog" />
<p class="submit"><input type="submit" name="submit" class="submit" value="<?php esc_attr_e( 'Create Site' ); ?>" /></p>
</form>
</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#L339">View on Trac</a> <a href="https://github.com/WordPress/wordpress-develop/blob/6.9.4/src/wp-signup.php#L339-L422">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_another_blog_init/"><span class="hook-func">apply_filters</span>( ‘signup_another_blog_init’, <nobr><span class="arg-type">array</span> <span class="arg-name">$signup_defaults</span></nobr> )</a></dt><dd><p>Filters the default site sign-up variables.</p>
</dd><dt class="wp-block-wporg-code-reference-title has-normal-font-size"><a href="https://developer.wordpress.org/reference/hooks/signup_hidden_fields/"><span class="hook-func">do_action</span>( ‘signup_hidden_fields’, <nobr><span class="arg-type">string</span> <span class="arg-name">$context</span></nobr> )</a></dt><dd><p>Fires when hidden sign-up form fields output when creating another site or user.</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.
show_blog_form()wp-signup.php
Generates and displays the Sign-up and Create Site forms.
esc_attr_e()wp-includes/l10n.php
Displays translated text that has been escaped for safe use in an attribute.
wp_get_current_user()wp-includes/pluggable.php
Retrieves the current user object.
get_home_url()wp-includes/link-template.php
Retrieves the URL for a given site where the front end is accessible.
get_blogs_of_user()wp-includes/user.php
Gets the sites a user belongs to.
__()wp-includes/l10n.php
Retrieves the translation of $text.
_e()wp-includes/l10n.php
Displays translated text.
esc_url()wp-includes/formatting.php
Checks and cleans a URL.
apply_filters()wp-includes/plugin.php
Calls the callback functions that have been added to a filter hook.
do_action()wp-includes/plugin.php
Calls the callback functions that have been added to an action hook.
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 |
|---|---|
validate_another_blog_signup()wp-signup.php |
Validates a new site sign-up for an existing user. |
Changelog
| Version | Description |
|---|---|
| MU (3.0.0) | Introduced. |