Viewing 3 replies - 1 through 3 (of 3 total)
  • @marvelcreative

    i guess no, “wpcf7_before_send_mail” hook wasn’t changed recently.

    Please be sure to use the cf7 post id here
    if ( $cf7->id() == 1234 ) {
    (you can find the cf7 post id while editing the form in the url …?page=wpcf7&post=1234&action=edit)

    and add subscribers_only: true to your form advanced settings

    (tested and works)

    Thread Starter marvelcreative

    (@marvelcreative)

    Thanks Erik!
    It was the “subscribers_only: true” that was my problem. I hadn’t realised that I needed to add that (as my form was already on a page only accessible to logged in users), and without that snippet of code anything to do with logged in users / wp_get_current_user in a function does not work ??

    All sorted now – thanks ??

    Thread Starter marvelcreative

    (@marvelcreative)

    Here is my working code in case it helps anyone else.
    It checks for the form ID, and also tests for the result of a user input of a checkbox field on my form. It assigns a different user role depending on whether they ‘accepted’ or ‘declined’ their place using the form.

    function wpcf7_before_send_mail_function($contact_form, $abort, $submission) {
    
        $form_id = $contact_form->id();
    	$output= $_POST['decision'];
        
        if ( is_user_logged_in() && $form_id === 2368 && $output == 'Accept') {
            $user = wp_get_current_user();
            $user->set_role('enrolment_deposit_paid');
        }
    	else if ( is_user_logged_in() && $form_id === 2368 && $output == 'Decline') {
            $user = wp_get_current_user();
            $user->set_role('declined_a_place');
    	}
    
    }
    add_action( 'wpcf7_before_send_mail', 'wpcf7_before_send_mail_function', 10, 3 );

    ** and add subscribers_only: true to your form advanced settings **

Viewing 3 replies - 1 through 3 (of 3 total)
  • The topic ‘Change user role on submission of form’ is closed to new replies.