钩子文档

login_form

💡 云策文档标注

概述

login_form 是一个 WordPress 动作钩子,在登录表单的“密码”字段后触发,用于自定义内置登录表单。通常与 login_head 钩子结合使用以实现验证功能。

关键要点

  • login_form 钩子在登录表单的密码字段后执行,允许开发者添加自定义字段或内容。
  • 此钩子自 WordPress 2.1.0 版本引入,源调用为 do_action( 'login_form' )。
  • 使用 add_action( 'login_form', 'callback_function' ) 来挂载自定义函数,但需手动处理额外字段的验证和保存。

代码示例

add_action( 'login_form', 'myplugin_add_login_fields' );

function myplugin_add_login_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 ‘Password’ field in the login form.

More Information

It can be used to customize the built-in WordPress login form. Use in conjunction with ‘login_head‘ (for validation).

Source

do_action( 'login_form' );

Changelog

Version Description
2.1.0 Introduced.

User Contributed Notes

  1. Skip to note 4 content

    Example migrated from Codex:

    This example demonstrates how to add a new field to the login 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( 'login_form', 'myplugin_add_login_fields' );
    
    function myplugin_add_login_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-4426' 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%2Flogin_form%2F%3Freplytocom%3D4426%23feedback-editor-4426" rel="nofollow">Log in to add feedback</a></footer>
    </article><!-- .comment-body -->
    </li>
    			<li id="comment-4427" data-comment-id="4427" class="comment byuser comment-author-stevenlinx even thread-even depth-1">
    			<article id="div-comment-4427" class="comment-body">
    
    							<a href="#comment-content-4427" class="screen-reader-text">Skip to note 5 content</a>
    				<header class="comment-meta">
    					<div class="comment-author vcard">
    						<span class="comment-author-attribution">
    						<a href="https://profiles.wordpress.org/stevenlinx/" rel="external nofollow" class="url">Steven Lin</a>						</span>
    						<a class="comment-date" href="https://developer.wordpress.org/reference/hooks/login_form/#comment-4427">
    							<time datetime="2020-10-22T03:04:05+00:00">
    							5 years ago							</time>
    						</a>
    
    																													</div>
    					<div class="user-note-voting" data-nonce="42811be6e6" 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="4427" data-vote="up" href="https://login.wordpress.org?redirect_to=https%3A%2F%2Fdeveloper.wordpress.org%2Freference%2Fhooks%2Flogin_form%2F%23comment-4427"><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="4427" data-vote="down" href="https://login.wordpress.org?redirect_to=https%3A%2F%2Fdeveloper.wordpress.org%2Freference%2Fhooks%2Flogin_form%2F%23comment-4427"><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-4427">
    				<p>Example migrated from Codex:</p>
    <p>The following example demonstrates how to translate the login form, page, fields or labels. Choose the codes/blocks for translation accurately, otherwise the above function may change other parts of the login page.</p>
    <pre class="wp-block-code"><code lang="php" class="language-php line-numbers">function my_translatorr2() {
    
      $your_content = ob_get_contents();
      $your_content = preg_replace( '/<label for="user_login">(.*?)<br/', 'Usernumia: ', $your_content );
      $your_content = preg_replace( '/<label for="user_pass">(.*?)<br/', 'Passwiert:', $your_content );
    
      ob_get_clean();
      echo $your_content;
    }
    add_action( 'login_form', 'my_translatorr2' );