Adding TOS to Membership2 Registration.

The good news is... I know I am on the right path. The bad news... The following error occurs with the following added to functions.php

Call to undefined method MS_Controller_Frontend::add() i

// START ADD TOS TO MEMBERSHIP FORM

add_filter('ms_controller_frontend_register_user_before','membership_tos_validate');

function membership_tos_validate($error) {
if(isset($_POST['tos_agree']) ) {
return $error; //pass
}
else
{
$error->add('notos', 'You must agree to the TOS');
}
}

add_action('signup_user_init','membership_tos_field');

function membership_tos_field($error) { ?>
<div class="form-element">
<label class="control-label" for="user_email">I Agree to the TOS</label>
<div class="element">
<input type="checkbox" class="checkbox" name="tos_agree" value="1">
</div>
</div>
<?php
}

// END Add TOS to Membership Form

  • James

    I am now working with this... No error, but not checking the box still allows registration.

    // START ADD TOS TO MEMBERSHIP FORM

    add_filter('registration_errors','membership_tos_validate');

    function membership_tos_validate($error) {
    if(isset($_POST['tos_agree']) ) {
    return $error; //pass
    }
    else
    {
    $error->add('notos', 'You must agree to the TOS');
    }
    }

    add_action('register_form','membership_tos_field');

    function membership_tos_field($error) { ?>
    <div class="form-element">
    <label class="control-label" for="user_email">I Agree to the TOS</label>
    <div class="element">
    <input type="checkbox" class="checkbox" name="tos_agree" value="1">
    </div>
    </div>
    <?php
    }

    // END Add TOS to Membership Form

    /* Stop Adding Functions Below this Line */
    ?>

  • Sajid

    Hi James! Hope you are doing good :slight_smile:

    I am glad you found a way to add TOS in membership form and yes you are on right direction :slight_smile:

    For the error, I am flagging SLS (Second Level Support), they are our mystery solvers and will be find a workaround for you. Please bear in mind their response may be slower than normal support, because they work on advanced threads, so please be patient.

    You can also see Developer Docs for more details about actions and filters hooks from Membership2 > Helps > Advanced Settings > Developer Docs.

    Take care and have a nice weekend :slight_smile:

    Cheers, Sajid

  • James

    I have gotten here now and it is the closest yet (maybe...).

    // START ADD TOS TO MEMBERSHIP FORM

    add_action('register_form','membership_tos_field');

    function membership_tos_field($error) { ?>
    <div class="ms-form-element ms-form-element-tos">
    <span class="wpmui-wrapper wpmui-input-wrapper ">
    <label for"tos_agree" class="wpmui-field-label wpmui-label-tos_agree">I Agree to the TOS</label>
    <div class="element">
    <input type="checkbox" class="checkbox ms-validation-error" name="tos_agree" id="tos_agree" checked>
    </div>
    <label id="password2-error" class="ms-validation-error" for="tos_agree">"This field is required."
    </label>
    </span>
    </div>
    <?php
    }

    function tos_agree_registration_errors( $errors ) {
    if ( empty( $_POST['tos_agree'] ) )
    $errors->add( 'empty_tos_agree', 'ERROR: Please enter your first name.' );
    return $errors;
    }
    add_filter( 'registration_errors', 'tos_agree_registration_errors' );

    // END Add TOS to Membership Form

  • Hoang Ngo

    Hi @James,

    I hope you are well today.
    I think you can try this hook ms_view_shortcode_registeruser_data, something like this

    add_filter( 'ms_view_shortcode_registeruser_data', 'validate_tos_submitted', 10, 2 );
    function validate_tos_submitted( $data, $controller ) {
    	if ( ! isset( $_POST['tos_agree'] ) ) {
    		if ( is_wp_error( $data['error'] ) ) {
    			$data['error']->add( 'ms_register_tos_not_check', __( "You must agree to the TOS", MS_TEXT_DOMAIN ) );
    		} else {
    			$data['error'] = new WP_Error( 'ms_register_tos_not_check', __( "You must agree to the TOS", MS_TEXT_DOMAIN ) );
    		}
    	}
    
    	return $data;
    }

    I still not test it, but the idea is if MS native validation can't find any errors, it will just by pass the hook registration_errors, so the above code, is use to tell the MS validation, we will need to validate the TOS field, and if users don't check it when register, we jump to error.
    And then, for displaying the error, you can use the hook registration_errors like this

    add_action( 'registration_errors', 'show_tos_error_if_have' );
    function show_tos_error_if_have( WP_Error $error ) {
    	$error_message = $error->get_error_code( 'ms_register_tos_not_check' );
    	if ( $error_message ) {
    		echo $error_message;
    	}
    }

    Hope this will help you :slight_smile:
    If you have any issues please don't hesitate to let us know so we can assist.

    Best regards,
    Hoang