Create a Password Reset Form With custom_action

I am trying to use the custom_action hook to generate and send the password reset link to the user.
My setup so far is:

  • created a form with a email field and a hidden field
  • added hidden field on the Email content - {{field_id}}
  • Used add_action(‘bricks/form/custom_action’, ‘form_reset_password_custom_action’, 10, 1);
  • Inside the form_reset_password_custom_action() function I generate the link with the user’s email
  • Add the reset link to the hidden field, as such
    $fields = $form->get_fields();
    $fields['form-field-hdpbqn'] = $reset_pass_link;

Full code below.

The issue is that the reset link is not showing on the email.

I have tested and can confirm that:

  • The hidden field when added a value manually works and can be seen in the email
  • The reset link is being generated successfully
  • And it is being added as the hidden field value.
function form_reset_password_custom_action($form)
{

  $fields = $form->get_fields();

  if ($fields['formId'] !== 'ulqmtn') {
    return;
  }


  $user_email = $fields['form-field-04da93'];
  $user = get_user_by('email', $user_email);
  $reset_key = get_password_reset_key($user);

  $user_login = $user->user_login;
  $reset_pass_link = '<a href="' . network_site_url("wp-login.php?action=rp&key=$reset_key&login=" . rawurlencode($user_login), 'login') . '">Reset Password</a>';

  $fields['form-field-hdpbqn'] = $reset_pass_link;

}
add_action('bricks/form/custom_action', 'form_reset_password_custom_action', 10, 1);

Here are some screenshots of the form setup on the builder


Screenshot 2023-08-10 at 13.32.09

1 Like

Hey Stefano,

both actions (the email and the custom action) get the “original” submission values passed. So you can’t adjust the submission values in the custom action to have them available in the email action.

Instead you should remove the default email action completely and add some code to send the email in your custom action (see example 2 in the academy).

In your case it should look something like this:

<?php

function form_reset_password_custom_action( $form ) {
    $fields = $form->get_fields();

    if ( $fields['formId'] !== 'ulqmtn' ) {
        return;
    }

    $user_email = $fields['form-field-04da93'];
    $user = get_user_by( 'email', $user_email );
    $reset_key = get_password_reset_key( $user );

    $user_login = $user->user_login;
    $reset_pass_link = '<a href="' . network_site_url( "wp-login.php?action=rp&key=$reset_key&login=" . rawurlencode( $user_login ), 'login' ) . '">Reset Password</a>';

    // Prepare email
    $to = $user_email;
    $from = 'Your From Name <yourfromemail@example.com>';
    $subject = 'Password Reset';

    $message = 'Here is your password reset link:<br><br>' . $reset_pass_link;

    $headers[] = "From: $from";
    $headers[] = 'Content-Type: text/html; charset=UTF-8';

    $result = wp_mail( $to, $subject, $message, $headers );
}
add_action( 'bricks/form/custom_action', 'form_reset_password_custom_action' );

Let me know if that helps.

Best,

André