Custom Reset Password Page
-
So I used this function to create a Shortcode for Reset Password Form. I get the idea of codes from here.
/** Custom Shortcode for WooCommerce Reset Password **/ function wc_custom_lost_password_form( $atts ) { if ( !empty( $_SESSION[ "csx-reset-link-set" ] ) && isset( $_SESSION[ "csx-reset-link-set" ] ) && $_SESSION[ "csx-reset-link-set" ] === "true" ) { // WPCS: input var ok, CSRF ok. unset( $_SESSION[ "csx-reset-link-set" ] ); return wc_get_template( 'myaccount/lost-password-confirmation.php' ); } elseif ( !empty( $_SESSION[ "csx-show-reset-form" ] ) && isset( $_SESSION[ "csx-show-reset-form" ] ) && $_SESSION[ "csx-show-reset-form" ] === "true" ) { /* Test 1 */ echo 'If this message print, it means this condition pass. (1)'; if ( isset( $_COOKIE[ 'wp-resetpass-' . COOKIEHASH ] ) && 0 < strpos( $_COOKIE[ 'wp-resetpass-' . COOKIEHASH ], ':' ) ) { // @codingStandardsIgnoreLine /* Test 2 */ echo 'If this message print, it means this condition pass. (2)'; //Test 2 list( $rp_id, $rp_key ) = array_map( 'wc_clean', explode( ':', wp_unslash( $_COOKIE[ 'wp-resetpass-' . COOKIEHASH ] ), 2 ) ); // @codingStandardsIgnoreLine $userdata = get_userdata( absint( $rp_id ) ); $rp_login = $userdata ? $userdata->user_login : ''; $user = WC_Shortcode_My_Account::check_password_reset_key( $rp_key, $rp_login ); // Reset key / login is correct, display reset password form with hidden key / login values. if ( is_object( $user ) ) { return wc_get_template( 'myaccount/form-reset-password.php', array( 'key' => $rp_key, 'login' => $rp_login, ) ); } } } // Show lost password form by default. return wc_get_template( 'myaccount/form-lost-password.php', array( 'form' => 'lost_password', ) ); } add_shortcode( 'lost_password_form', 'wc_custom_lost_password_form' );
Using Elementor to design the whole page, I’ve used the ShortCode widget and the
[lost_password_form]
shortcode that I have created to display the form.Then another function to redirect the user into the Reset Password page I created.
/** Handling Redirections **/ function csx_redirections() { if ( !empty( $_GET[ 'reset-link-sent' ] ) ) { $_SESSION[ "csx-reset-link-set" ] = "true"; } if ( !empty( $_GET[ 'show-reset-form' ] ) ) { $_SESSION[ "csx-show-reset-form" ] = "true"; } if ( !empty( $_GET[ 'reset-link-sent' ] ) || !empty( $_GET[ 'show-reset-form' ] ) ) { wp_redirect( home_url() . '/password-recovery' ); exit; } } add_action( 'template_redirect', 'csx_redirections' );
As you can see, from the shortcode I’ve created, I was able to display the default form which is
myaccount/form-lost-password.php
. I was also able to display the confirmation message which ismyaccount/lost-password-confirmation.php
. But it failed to display the new password form which ismyaccount/form-reset-password.php
.I enabled the debugging to see which condition is not passing, as you can see from the shortcode I created, there is
echo's
;/* Test 1 */ echo 'If this message print, it means this condition pass. (1)';
and
/* Test 2 */ echo 'If this message print, it means this condition pass. (2)'; //Test 2
The Test 2 is not printing, it means the following condition is failed.
if ( isset( $_COOKIE[ 'wp-resetpass-' . COOKIEHASH ] ) && 0 < strpos( $_COOKIE[ 'wp-resetpass-' . COOKIEHASH ], ':' ) ){ .... }
I’m almost there, what I am missing?
- The topic ‘Custom Reset Password Page’ is closed to new replies.