Hook for post-WordPress/Woocommerce registration to check form data & add function

What I have:

  • A log in page created by WooCommerce
  • A check box added to it by MC4WP (Mailchimp for WordPress)
  • Roles managed by Members plugin

What I am trying to do:
When a user registers, if they have selected the check box, I would like to assign them an additional role. (NOT update their role from the default woocommerce Customer to something else; trying to add a role in addition to that).

I have assigned multiple roles with another form successfully, but without the added step of the checkbox or woocommerce’s form.

I’ve tried hooking user_register, profile_update, even woocommerce_created_customer, but none so far have worked.

I’m not sure where my issue is, but this I have found:
When including the checkbox name as taken from source code, I get a 500 error after form submission. User is registered (with customer role only).
If I put in a junk name there, no error.

I’m not sure what I’ve misunderstood (or just completely missed).
Any help/thoughts are appreciated!

//add_action( 'user_register', 'check_the_form_box_add_role');
//add_action('woocommerce_created_customer', 'check_the_form_box_add_role');


add_action('profile_update', 'check_the_form_box_add_role');

function check_the_form_box_add_role( $user_id ) {

    if ( isset($_POST['_mc4wp_subscribe_wp-registration-form']) ) {
            $user_id = wp_insert_user( $user_id );
            $role_to_add = 'thisistherole;

            $user_id->add_role($role_to_add);
        }


}

The form from source code:

<form method="post" class="register">
<p class="woocommerce-FormRow woocommerce-FormRow--wide form-row form-row-wide">
<label for="reg_email">Email address <span class="required">*</span></label>
<input type="email" class="woocommerce-Input woocommerce-Input--text input-text" name="email" id="reg_email" value=""/>
</p>

<div style="left: -999em; position: absolute;"><label for="trap">Anti-spam</label><input type="text" name="email_2" id="trap" tabindex="-1"/></div>
 <input type="hidden" name="_mc4wp_subscribe_wp-registration-form" value="0"/><p class="mc4wp-checkbox mc4wp-checkbox-wp-registration-form"><label><input type="checkbox" name="_mc4wp_subscribe_wp-registration-form" value="1" checked="checked"/><span>This is text for checkbox!</span></label></p> 
<p class="woocomerce-FormRow form-row">
<input type="hidden" id="_wpnonce" name="_wpnonce" value="994ca0c618"/><input type="hidden" name="_wp_http_referer" value="/my-account/"/> <input type="submit" class="woocommerce-Button button" name="register" value="Register"/>
</p>
</form>

I thought about hooking registration_errors, but this runs before the user is created. Previously, I would create user as Customer and use something like

$user_id = wp_insert_user( $user_id );
$role_to_add = 'thisistherole;
$user_id->add_role($role_to_add);

to add the additional role.

Read more here: Hook for post-Wordpress/Woocommerce registration to check form data & add function

Leave a Reply

Your email address will not be published. Required fields are marked *