Making a user and adding a role... but NOT failing if the user already exists.

This seems SO basic. I have two actions for a newsletter signup: make user, and add role. The issue is, I also have Surecart running, which will make users if they buy anything from the store. So, if someone has bought something on the store, signing up for the newsletter will fail because the user already exists.

I want the form to check if a user already exists, and if they don’t, make the user and give them the “newsletter” role. If the user already exists, DON’T FAIL, just add the “newsletter” role.

So I’ve spent a week and a half trying to get this functionality, and I’m deep into PHP programing, but hitting a wall trying to get the form field data into the snippet (I’m able to import the correct array for the form, but have no idea how to actually see what’s inside so I can match up the variables.) After a full day of blindly trying everything I can think of, and getting the feeling that I must be the only person in the history of the interned to ever try to accomplish this task…

I’m taking a step back and just asking simply: Is there an easy way to just skip making a new user and continue on with the rest of the form’s submission actions if the user already exists?

Hi,

thanks for the detailed description, and sorry for the wall you hit. Short answer first: no, the User Registration action has no “skip if the user already exists” option. The action validates username and email up front, and if either one already exists it stops the whole submission with an error, so none of the other actions run.

So for your case (create the user only if needed, always add the newsletter role) the clean way is to drop the Registration action and do both steps in the Custom action. The Custom action fires the hook bricksforge/pro_forms/custom_action and hands you the form object, which is exactly the piece you were missing. Here is a complete snippet:

    add_action('bricksforge/pro_forms/custom_action', function ($form) {

        // Optional: only run for this form (Pro Forms element ID)
        if ($form->get_form_id() !== 'abc123') {
            return;
        }

        // "email" is the Custom ID you gave the email field
        $email = $form->get_form_field_by_id('{{email}}');

        if (!is_email($email)) {
            return;
        }

        $user_id = email_exists($email);

        if (!$user_id) {
            $user_id = wp_insert_user([
                'user_login' => $email,
                'user_email' => $email,
                'user_pass'  => wp_generate_password(),
                'role'       => 'newsletter',
            ]);

            if (is_wp_error($user_id)) {
                return;
            }
        }

        $user = new WP_User($user_id);
        $user->add_role('newsletter');
    });

Two things that should save you the guessing:

  1. To see what is inside the submission, log the raw fields once: error_log(print_r($form->get_fields(), true)); and look at wp-content/debug.log (WP_DEBUG_LOG has to be on). The keys are form-field-, so a field with the Custom ID “email” arrives as form-field-email.

  2. You do not need to touch that array directly. $form->get_form_field_by_id(‘{{email}}’) resolves the same {{field_id}} syntax you already use in the email action and gives you the plain value. Same for any other field, e.g. {{first_name}}.

With this setup an existing SureCart customer just gets the extra role and the rest of your actions (email, redirect and so on) continue as normal.

Best,
Daniele