Skip to content

Instantly share code, notes, and snippets.

@Alimir
Last active March 10, 2023 12:24
Show Gist options
  • Save Alimir/cb0d7d00571d93c604b291e4bea72793 to your computer and use it in GitHub Desktop.
Save Alimir/cb0d7d00571d93c604b291e4bea72793 to your computer and use it in GitHub Desktop.
Add new password field inside wp ulike edit account & signup forms
<?php
/**
* Add new fields to account form
*
* @param string $type
* @param array $args
* @return void
*/
function wp_ulike_pro_customize_account_form( $type, $args ){
if( $type === 'profile' ){
?>
<div class="ulp-flex-col-xl-12 ulp-flex-col-md-12 ulp-flex-col-xs-12">
<div class="ulp-floating">
<input id="ulp-new-password" type="password" class="ulp-floating-input" name="newpassword" type="text" placeholder="Set new password" required/>
<label for="ulp-new-password" class="ulp-floating-label" data-content="Password">
<span class="ulp-hidden-visually">Password</span>
</label>
</div>
</div>
<div class="ulp-flex-col-xl-12 ulp-flex-col-md-12 ulp-flex-col-xs-12">
<div class="ulp-floating">
<input id="ulp-new-re-password" type="password" class="ulp-floating-input" name="re-newpassword" type="text" placeholder="Re-type password" required/>
<label for="ulp-new-re-password" class="ulp-floating-label" data-content="Re-Password">
<span class="ulp-hidden-visually">Re-Password</span>
</label>
</div>
</div>
<?php
}
if( $type === 'signup' ){
?>
<div class="ulp-flex-col-xl-12 ulp-flex-col-md-12 ulp-flex-col-xs-12">
<div class="ulp-floating">
<input id="ulp-new-re-password" type="password" class="ulp-floating-input" name="re-newpassword" type="text" placeholder="Re-type password" required/>
<label for="ulp-new-re-password" class="ulp-floating-label" data-content="Re-Password">
<span class="ulp-hidden-visually">Re-Password</span>
</label>
</div>
</div>
<?php
}
}
add_action( 'wp_ulike_pro_forms_before_submit', 'wp_ulike_pro_customize_account_form', 10, 2 );
/**
* Update data
*
* @param array $args
* @return void
*/
function wp_ulike_pro_customize_account_form_process($args){
if( ! empty( $_POST['newpassword'] ) && is_user_logged_in() ){
if ( $_POST['newpassword'] !== $_POST['re-newpassword'] ) {
throw new \Exception( 'Password not match' );
}
$current_user = wp_get_current_user();
reset_password( $current_user, $_POST['newpassword'] );
$creds = array(
'user_login' => $current_user->user_login,
'user_password' => $_POST['newpassword'],
'remember' => false
);
$user = wp_signon( $creds );
if ( is_wp_error( $user ) ) {
throw new \Exception( 'Error occured' );
}
wp_set_current_user( $user->ID );
}
}
add_action( 'wp_ulike_pro_after_profile_process', 'wp_ulike_pro_customize_account_form_process', 20, 1 );
/**
* process re-password field
*
* @param array $args
* @return void
*/
function wp_ulike_pro_customize_signup_form_process($args){
if( empty( $args->data['password'] ) ){
throw new \Exception( 'Please enter Password filed' );
}
if( empty( $_POST['re-newpassword'] ) ){
throw new \Exception( 'Please enter Re-Password filed' );
}
if ( $args->data['password'] !== $_POST['re-newpassword'] ) {
throw new \Exception( 'Password not match' );
}
}
add_action( 'wp_ulike_pro_before_signup_process', 'wp_ulike_pro_customize_signup_form_process', 20, 1 );
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment