display_setup_form()
概述
display_setup_form() 函数用于在 WordPress 安装过程中显示设置表单,处理用户输入如站点标题、用户名和邮箱,并包含错误处理和隐私设置选项。
关键要点
- 函数接受一个可选参数 $error,用于显示错误信息。
- 检查数据库中是否存在用户表,以确定安装状态。
- 处理 POST 数据,包括 weblog_title、user_name、admin_email 和 blog_public。
- 输出 HTML 表单,包含站点标题、用户名、密码、邮箱和隐私设置等字段。
- 使用 WordPress 核心函数如 wpdb::get_var()、wp_unslash() 和 submit_button() 等。
- 包含 Hook do_action('blog_privacy_selector') 以支持旧版隐私选项。
代码示例
function display_setup_form( $error = null ) {
global $wpdb;
$user_table = ( $wpdb->get_var( $wpdb->prepare( 'SHOW TABLES LIKE %s', $wpdb->esc_like( $wpdb->users ) ) ) !== null );
// Ensure that sites appear in search engines by default.
$blog_public = 1;
if ( isset( $_POST['weblog_title'] ) ) {
$blog_public = isset( $_POST['blog_public'] ) ? (int) $_POST['blog_public'] : $blog_public;
}
$weblog_title = isset( $_POST['weblog_title'] ) ? trim( wp_unslash( $_POST['weblog_title'] ) ) : '';
$user_name = isset( $_POST['user_name'] ) ? trim( wp_unslash( $_POST['user_name'] ) ) : '';
$admin_email = isset( $_POST['admin_email'] ) ? trim( wp_unslash( $_POST['admin_email'] ) ) : '';
if ( ! is_null( $error ) ) {
// 输出错误信息 HTML
}
// 输出表单 HTML,包括输入字段和提交按钮
}注意事项
- 函数自 WordPress 2.8.0 版本引入,是安装程序的核心部分。
- 依赖全局变量 $wpdb 进行数据库操作,确保在调用前已初始化。
- 表单使用 POST 方法提交数据,需在安装流程中正确处理。
- 隐私设置默认启用(blog_public=1),但用户可通过表单修改。
- 相关函数如 sanitize_user() 和 wp_generate_password() 用于数据验证和安全生成。
Displays installer setup form.
Parameters
$errorstring|nulloptional-
Default:
null
Source
function display_setup_form( $error = null ) {
global $wpdb;
$user_table = ( $wpdb->get_var( $wpdb->prepare( 'SHOW TABLES LIKE %s', $wpdb->esc_like( $wpdb->users ) ) ) !== null );
// Ensure that sites appear in search engines by default.
$blog_public = 1;
if ( isset( $_POST['weblog_title'] ) ) {
$blog_public = isset( $_POST['blog_public'] ) ? (int) $_POST['blog_public'] : $blog_public;
}
$weblog_title = isset( $_POST['weblog_title'] ) ? trim( wp_unslash( $_POST['weblog_title'] ) ) : '';
$user_name = isset( $_POST['user_name'] ) ? trim( wp_unslash( $_POST['user_name'] ) ) : '';
$admin_email = isset( $_POST['admin_email'] ) ? trim( wp_unslash( $_POST['admin_email'] ) ) : '';
if ( ! is_null( $error ) ) {
?>
<h1></h1>
<p class="message"></p>
<form id="setup" method="post" action="install.php?step=2" novalidate="novalidate">
<table class="form-table" role="presentation">
<tr>
<th scope="row"><label for="weblog_title"></label></th>
<td><input name="weblog_title" type="text" id="weblog_title" size="25" value="<?php echo esc_attr( $weblog_title ); ?>" /></td>
</tr>
<tr>
<th scope="row"><label for="user_login"></label></th>
<td>
';
} else {
?>
<input name="user_name" type="text" id="user_login" size="25" aria-describedby="user-name-desc" value="<?php echo esc_attr( sanitize_user( $user_name, true ) ); ?>" />
<p id="user-name-desc"></p>
</td>
</tr>
<tr class="form-field form-required user-pass1-wrap">
<th scope="row">
<label for="pass1">
</label>
</th>
<td>
<div class="wp-pwd">
<div class="password-input-wrapper">
<input type="password" name="admin_password" id="pass1" class="regular-text" autocomplete="new-password" spellcheck="false" data-reveal="1" data-pw="<?php echo esc_attr( $initial_password ); ?>" aria-describedby="pass-strength-result admin-password-desc" />
<div id="pass-strength-result" aria-live="polite"></div>
</div>
<button type="button" class="button wp-hide-pw hide-if-no-js" data-start-masked="<?php echo (int) isset( $_POST['admin_password'] ); ?>" data-toggle="0" aria-label="<?php esc_attr_e( 'Hide password' ); ?>">
<span class="dashicons dashicons-hidden"></span>
<span class="text"></span>
</button>
</div>
<p id="admin-password-desc"><span class="description important hide-if-no-js">
<strong></strong>
</span></p>
</td>
</tr>
<tr class="form-field form-required user-pass2-wrap hide-if-js">
<th scope="row">
<label for="pass2">
<span class="description"></span>
</label>
</th>
<td>
<input type="password" name="admin_password2" id="pass2" autocomplete="new-password" spellcheck="false" />
</td>
</tr>
<tr class="pw-weak">
<th scope="row"></th>
<td>
<label>
<input type="checkbox" name="pw_weak" class="pw-checkbox" />
</label>
</td>
</tr>
<tr>
<th scope="row"><label for="admin_email"></label></th>
<td><input name="admin_email" type="email" id="admin_email" size="25" aria-describedby="admin-email-desc" value="<?php echo esc_attr( $admin_email ); ?>" />
<p id="admin-email-desc"></p></td>
</tr>
<tr>
<th scope="row"></th>
<td>
<fieldset>
<legend class="screen-reader-text"><span></span></legend>
<input id="blog-public" type="radio" name="blog_public" value="1" <?php checked( 1, $blog_public ); ?> />
<label for="blog-public"></label><br />
<input id="blog-norobots" type="radio" name="blog_public" aria-describedby="public-desc" value="0" <?php checked( 0, $blog_public ); ?> />
<label for="blog-norobots"></label>
<p id="public-desc" class="description"></p>
<label for="blog_public"><input name="blog_public" type="checkbox" id="blog_public" aria-describedby="privacy-desc" value="0" <?php checked( 0, $blog_public ); ?> />
</label>
<p id="privacy-desc" class="description"></p>
</fieldset>
</td>
</tr>
</table>
<p class="step"> 'submit' ) ); ?></p>
<input type="hidden" name="language" value="<?php echo isset( $_REQUEST['language'] ) ? esc_attr( $_REQUEST['language'] ) : ''; ?>" />
</form>
</pre><p class="wporg-dot-link-list"><a href="https://developer.wordpress.org/reference/files/wp-admin/install.php/">View all references</a> <a href="https://core.trac.wordpress.org/browser/tags/6.9.4/src/wp-admin/install.php#L90">View on Trac</a> <a href="https://github.com/WordPress/wordpress-develop/blob/6.9.4/src/wp-admin/install.php#L90-L213">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/blog_privacy_selector/"><span class="hook-func">do_action</span>( ‘blog_privacy_selector’ )</a></dt><dd><p>Enables the legacy ‘Site visibility’ privacy options.</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/classes/wpdb/esc_like/">wpdb::esc_like()</a><code>wp-includes/class-wpdb.php
First half of escaping for LIKE special characters % and _ before preparing for SQL.
submit_button()wp-admin/includes/template.php
Echoes a submit button, with provided text and appropriate class(es).
_ex()wp-includes/l10n.php
Displays translated string with gettext context.
esc_attr_e()wp-includes/l10n.php
Displays translated text that has been escaped for safe use in an attribute.
sanitize_user()wp-includes/formatting.php
Sanitizes a username, stripping out unsafe characters.
wp_generate_password()wp-includes/pluggable.php
Generates a random password drawn from the defined set of characters.
checked()wp-includes/general-template.php
Outputs the HTML checked attribute.
has_action()wp-includes/plugin.php
Checks if any action has been registered for a hook.
_e()wp-includes/l10n.php
Displays translated text.
__()wp-includes/l10n.php
Retrieves the translation of $text.
wp_unslash()wp-includes/formatting.php
Removes slashes from a string or recursively removes slashes from strings within an array.
esc_attr()wp-includes/formatting.php
Escaping for HTML attributes.
do_action()wp-includes/plugin.php
Calls the callback functions that have been added to an action hook.
wpdb::get_var()wp-includes/class-wpdb.php
Retrieves one value from the database.
wpdb::prepare()wp-includes/class-wpdb.php
Prepares a SQL query for safe execution.
Changelog
| Version | Description |
|---|---|
| 2.8.0 | Introduced. |