Skip to content

Commit 5d3901a

Browse files
committed
Feature #37522: Add wp_lostpassword_form() function for embedding the lost password form anywhere
1 parent 4e46f01 commit 5d3901a

2 files changed

Lines changed: 107 additions & 26 deletions

File tree

src/wp-includes/general-template.php

Lines changed: 101 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -651,6 +651,107 @@ function wp_login_form( $args = array() ) {
651651
}
652652
}
653653

654+
/**
655+
* Outputs or returns the lost password form for use anywhere on a WordPress site.
656+
*
657+
* @since 7.1.0
658+
*
659+
* @param array $args {
660+
* Optional. Array of arguments to control the form output. Default empty array.
661+
*
662+
* @type bool $echo Whether to display the form or return the output. Default true.
663+
* @type string $redirect URL to redirect to after submitting the form. Default empty string.
664+
* @type string $form_id ID attribute for the form element. Default 'lostpasswordform'.
665+
* @type string $id_username ID attribute for the username input. Default 'user_login'.
666+
* @type string $id_submit ID attribute for the submit button. Default 'wp-submit'.
667+
* @type string $label_username Label for the username input. Default 'Username or Email Address'.
668+
* @type string $label_submit Label for the submit button. Default 'Get New Password'.
669+
* }
670+
* @return void|string Void if 'echo' argument is true, lost password form HTML if 'echo' is false.
671+
*/
672+
function wp_lostpassword_form( $args = array() ) {
673+
$defaults = array(
674+
'echo' => true,
675+
'redirect' => '',
676+
'form_id' => 'lostpasswordform',
677+
'id_username' => 'user_login',
678+
'id_submit' => 'wp-submit',
679+
'label_username' => __( 'Username or Email Address' ),
680+
'label_submit' => __( 'Get New Password' ),
681+
);
682+
683+
/**
684+
* Filters the default lost password form arguments.
685+
*
686+
* @since 7.1.0
687+
*
688+
* @see wp_lostpassword_form()
689+
*
690+
* @param array $defaults An array of default lost password form arguments.
691+
*/
692+
$args = wp_parse_args( $args, apply_filters( 'lostpassword_form_defaults', $defaults ) );
693+
694+
$user_login = '';
695+
if ( isset( $_POST['user_login'] ) && is_string( $_POST['user_login'] ) ) {
696+
$user_login = wp_unslash( $_POST['user_login'] );
697+
}
698+
699+
/**
700+
* Filters content to display at the top of the lost password form.
701+
*
702+
* The filter evaluates just following the opening form tag element.
703+
*
704+
* @since 7.1.0
705+
*
706+
* @param string $content Content to display. Default empty.
707+
* @param array $args Array of lost password form arguments.
708+
*/
709+
$lostpassword_form_top = apply_filters( 'lostpassword_form_top', '', $args );
710+
711+
/**
712+
* Filters content to display at the bottom of the lost password form.
713+
*
714+
* The filter evaluates just preceding the closing form tag element.
715+
*
716+
* @since 7.1.0
717+
*
718+
* @param string $content Content to display. Default empty.
719+
* @param array $args Array of lost password form arguments.
720+
*/
721+
$lostpassword_form_bottom = apply_filters( 'lostpassword_form_bottom', '', $args );
722+
723+
ob_start();
724+
?>
725+
<form name="<?php echo esc_attr( $args['form_id'] ); ?>" id="<?php echo esc_attr( $args['form_id'] ); ?>" action="<?php echo esc_url( network_site_url( 'wp-login.php?action=lostpassword', 'login_post' ) ); ?>" method="post">
726+
<?php echo $lostpassword_form_top; ?>
727+
<p>
728+
<label for="<?php echo esc_attr( $args['id_username'] ); ?>"><?php echo esc_html( $args['label_username'] ); ?></label>
729+
<input type="text" name="user_login" id="<?php echo esc_attr( $args['id_username'] ); ?>" class="input ltr" value="<?php echo esc_attr( $user_login ); ?>" size="20" autocapitalize="off" autocomplete="username" required="required" />
730+
</p>
731+
<?php
732+
/**
733+
* Fires inside the lost password form tags, before the hidden fields.
734+
*
735+
* @since 2.1.0
736+
*/
737+
do_action( 'lostpassword_form' );
738+
?>
739+
<input type="hidden" name="redirect_to" value="<?php echo esc_attr( $args['redirect'] ); ?>" />
740+
<p class="submit">
741+
<input type="submit" name="wp-submit" id="<?php echo esc_attr( $args['id_submit'] ); ?>" class="button button-primary button-large" value="<?php echo esc_attr( $args['label_submit'] ); ?>" />
742+
</p>
743+
<?php echo $lostpassword_form_bottom; ?>
744+
</form>
745+
<?php
746+
$form = ob_get_clean();
747+
748+
if ( $args['echo'] ) {
749+
echo $form;
750+
} else {
751+
return $form;
752+
}
753+
}
754+
654755
/**
655756
* Returns the URL that allows the user to reset the lost password.
656757
*

src/wp-login.php

Lines changed: 6 additions & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -886,35 +886,15 @@ function wp_login_viewport_meta() {
886886
$errors
887887
);
888888

889-
$user_login = '';
890-
891-
if ( isset( $_POST['user_login'] ) && is_string( $_POST['user_login'] ) ) {
892-
$user_login = wp_unslash( $_POST['user_login'] );
893-
}
889+
wp_lostpassword_form(
890+
array(
891+
'echo' => true,
892+
'redirect' => $redirect_to,
893+
)
894+
);
894895

895896
?>
896897

897-
<form name="lostpasswordform" id="lostpasswordform" action="<?php echo esc_url( network_site_url( 'wp-login.php?action=lostpassword', 'login_post' ) ); ?>" method="post">
898-
<p>
899-
<label for="user_login"><?php _e( 'Username or Email Address' ); ?></label>
900-
<input type="text" name="user_login" id="user_login" class="input ltr" value="<?php echo esc_attr( $user_login ); ?>" size="20" autocapitalize="off" autocomplete="username" required="required" />
901-
</p>
902-
<?php
903-
904-
/**
905-
* Fires inside the lostpassword form tags, before the hidden fields.
906-
*
907-
* @since 2.1.0
908-
*/
909-
do_action( 'lostpassword_form' );
910-
911-
?>
912-
<input type="hidden" name="redirect_to" value="<?php echo esc_attr( $redirect_to ); ?>" />
913-
<p class="submit">
914-
<input type="submit" name="wp-submit" id="wp-submit" class="button button-primary button-large" value="<?php esc_attr_e( 'Get New Password' ); ?>" />
915-
</p>
916-
</form>
917-
918898
<p id="nav">
919899
<a class="wp-login-log-in" href="<?php echo esc_url( wp_login_url() ); ?>"><?php _e( 'Log in' ); ?></a>
920900
<?php

0 commit comments

Comments
 (0)