钩子文档

register_form

💡 云策文档标注

概述

register_form 是一个 WordPress 动作钩子,在用户注册表单的“Email”字段后触发,主要用于自定义注册表单的字段添加或修改。

关键要点

  • register_form 钩子在注册表单的“Email”字段后执行,允许开发者添加自定义字段或修改表单内容。
  • 自定义注册时,需结合 registration_errors 钩子进行验证,并使用 register_post 钩子保存额外数据。
  • 在 WordPress 多站点(MS)环境中,应使用 signup_header 动作来重定向用户,而非 register_form。
  • 添加的字段不会自动保存,需要手动设置验证规则和处理保存逻辑。

代码示例

add_action( 'register_form', 'wporg_myplugin_add_registration_fields' );

function wporg_myplugin_add_registration_fields() {
    $user_extra = ( isset( $_POST['user_extra'] ) ) ? $_POST['user_extra'] : '';
    ?>
    <p>
        <label for="user_extra">Extra Field<br>
        <input type="text" name="user_extra" id="user_extra" class="input" value="<?php echo esc_attr( $user_extra ); ?>" size="25" />
        </label>
    </p>
    <?php
}

注意事项

修改或翻译注册表单时,需谨慎处理代码块,避免意外更改注册页面的其他部分。


📄 原文内容

Fires following the ‘Email’ field in the user registration form.

More Information

  • Use in conjunction with ‘registration_errors‘ (for validation) and ‘register_post‘ (save extra data) when customizing registration.
  • WordPress MS Note: For WordPress MS (Multi-Site), use the ‘signup_header‘ action to redirect users away from the signup.

Source

do_action( 'register_form' );

Changelog

Version Description
2.1.0 Introduced.

User Contributed Notes

  1. Skip to note 3 content

    This example demonstrates how to add a new field to the registration form. Keep in mind that this won’t be saved automatically. You will still need to set up validation rules and manually handle saving of the additional form fields.

    add_action( 'register_form', 'wporg_myplugin_add_registration_fields' );
    
    function wporg_myplugin_add_registration_fields() {
    
        // Get and set any values already sent
        $user_extra = ( isset( $_POST['user_extra'] ) ) ? $_POST['user_extra'] : '';
        ?>
    
        <p>
            <label for="user_extra"><br />
            <input type="text" name="user_extra" id="user_extra" class="input" value="<?php echo esc_attr( stripslashes( $user_extra ) ); ?>" size="25" /></label>
        </p>
    
        </pre>
    				</div><!-- .comment-content -->
    
    					<section id='feedback-3447' class='wporg-has-embedded-code feedback hide-if-js' data-comment-count='0'>
    </section><!-- .feedback -->
    <footer class='feedback-links wporg-dot-link-list' >
    <a role="button" class="feedback-login" href="https://login.wordpress.org/?redirect_to=https%3A%2F%2Fdeveloper.wordpress.org%2Freference%2Fhooks%2Fregister_form%2F%3Freplytocom%3D3447%23feedback-editor-3447" rel="nofollow">Log in to add feedback</a></footer>
    </article><!-- .comment-body -->
    </li>
    			<li id="comment-3448" data-comment-id="3448" class="comment byuser comment-author-tecdoc odd alt thread-odd thread-alt depth-1">
    			<article id="div-comment-3448" class="comment-body">
    
    							<a href="#comment-content-3448" class="screen-reader-text">Skip to note 4 content</a>
    				<header class="comment-meta">
    					<div class="comment-author vcard">
    						<span class="comment-author-attribution">
    						<a href="https://profiles.wordpress.org/tecdoc/" rel="external nofollow" class="url">Tom Carney</a>						</span>
    						<a class="comment-date" href="https://developer.wordpress.org/reference/hooks/register_form/#comment-3448">
    							<time datetime="2019-11-03T18:04:49+00:00">
    							6 years ago							</time>
    						</a>
    
    																													</div>
    					<div class="user-note-voting" data-nonce="a2d24d9d69" data-can-vote="false"><a class="user-note-voting-up" title="You must log in to vote on the helpfulness of this note" data-id="3448" data-vote="up" href="https://login.wordpress.org?redirect_to=https%3A%2F%2Fdeveloper.wordpress.org%2Freference%2Fhooks%2Fregister_form%2F%23comment-3448"><span class="screen-reader-text">You must log in to vote on the helpfulness of this note</span></a><span class="user-note-voting-count " title=""><span class="screen-reader-text">Vote results for this note: </span>0</span><a class="user-note-voting-down" title="You must log in to vote on the helpfulness of this note" data-id="3448" data-vote="down" href="https://login.wordpress.org?redirect_to=https%3A%2F%2Fdeveloper.wordpress.org%2Freference%2Fhooks%2Fregister_form%2F%23comment-3448"><span class="screen-reader-text">You must log in to vote on the helpfulness of this note</span></a></div>				</header>
    				<!-- .comment-metadata -->
    			
    				<div class="wporg-has-embedded-code comment-content" id="comment-content-3448">
    				<p>To modify or translate the registration form , page or fieldnames, you can use the following function:</p>
    <pre class="wp-block-code"><code lang="php" class="language-php line-numbers">function my_translate() {
    
       $your_content = ob_get_contents();
       $your_content = preg_replace( '/<label for="user_login">(.*?)<br/', 'Usernumia: ', $content );
       $your_content = preg_replace( '/<label for="user_email">(.*?)<br/', 'Email Sior:', $content );
    
       ob_get_clean();
       echo $your_content;
    }
    add_action( 'register_form', 'my_translate' );

    Choose the codes/blocks for translation accurately, otherwise the above function may change other parts of the registration page.