Search code examples
phpwordpresscontact-form-7

Contact Form 7 (CF7) radio button to change User Role on WordPress


I have a form setup for users to fill in after they register on my WordPress site. Ive setup the form using Contact Form 7, and have a radio button called radio-766 that has two options: subscriber and customer.

I want the user to pick one of these two options, then when they submit the form, it will change their user role.

Below is what I have so far... I've grabbed snippets from online and tried to create my own, but it isn't working. Any ideas on how I can get this to work?

function tm_cf7_roles_posted_data( $posted_data ) {

    // Stop if user is not logged in.
    if ( ! is_user_logged_in() )
        return;

    ob_start();

    $role = sanitize_key( $posted_data['radio-766'] );
    if ( ! in_array( $role, array( 'subscriber', 'customer' ) ) )
        return;

    $user = new WP_User( get_current_user_id() );
    $index = key( $user->roles );
    $user_role = $user->roles[ $index ];


    $output = ob_get_contents();
    ob_end_clean();
    return $output;
} 
add_action( 'wpcf7_posted_data', 'tm_cf7_roles_posted_data' );

Should I be including the form name or ID anywhere? Can't find info on this

Any help is so appreciated!

EDIT

I feel like there is nothing connecting this function to the CF7 form named "After LinkedIn", so I found this snippet of code, just not sure how to integrate and get working

if (!isset($cfdata->posted_data) && class_exists('WPCF7_Submission')) {
        // Contact Form 7 version 3.9 removed $cfdata->posted_data and now
        // we have to retrieve it from an API
        $submission = WPCF7_Submission::get_instance();
        if ($submission) {
            $formdata = $submission->get_posted_data();
        }
    } elseif (isset($cfdata->posted_data)) {
        // For pre-3.9 versions of Contact Form 7
        $formdata = $cfdata->posted_data;
    } else {
        // We can't retrieve the form data
        return $cfdata;
    }
      // Check this is the user registration form
    if ( $cfdata->title() == 'After LinkedIn') {



Solution

  • As per the Contact form 7 plugin author is_user_logged_in() will work on below to cases in form submission:

    1. subscribers_only: true set in the additional setting in form. OR
    2. Set WPCF7_VERIFY_NONCE to true using add_filter( 'wpcf7_verify_nonce', '__return_true' );

    For more information click here.

    Also, to change the user role you can do the following:

    add_action( 'wpcf7_posted_data', 'tm_cf7_roles_posted_data' );
    function tm_cf7_roles_posted_data( $posted_data ) {
        $submission = WPCF7_Submission::get_instance();
        $wpcf7      = WPCF7_ContactForm::get_current();
    
        $formID = $wpcf7->id();
        if ( $submission ) {
            if( $formID == "YOUR-FORM-ID" ) {
                if ( is_user_logged_in() ) {
                    $role = sanitize_key( $posted_data['radio-766'][0] );
                    if ( ! in_array( $role, array( 'subscriber', 'customer' ) ) )
                        return;
    
                    // Getting the WP_User object
                    $user = wp_get_current_user();
                    // The user exists
                    if( $user && $user->exists() ) {
                        // Check if user already has that role or not
                        if ( !in_array( $role, (array) $user->roles ) ) {
                            // Remove all the previous roles from the user and add this one
                            // This will also reset all the caps and set them for the new role
                            $user->set_role( $role );
                        }
                    }
                }
            }
        }
    }
    

    If you only want to add a new role to the user and retain the existing role then instead of set_role use below:

    // Add a new role to the user while retaining the previous ones
    $user->add_role( $role );