Home / Archive / MemberPress: Exclude Specific Custom Fields from MemberPress Registration Forms
Duplicate Snippet

Embed Snippet on Your Site

MemberPress: Exclude Specific Custom Fields from MemberPress Registration Forms

This code snippet allows you to hide specific custom fields on MemberPress registration forms when a particular URL parameter is present. This approach is particularly useful for corporate accounts or specialized registration flows where you want to customize the registration form for specific types of registrations.

This functionality is triggered only when a specific query parameter (ca in this example) is present in the URL.

To exclude different custom fields, modify the $excluded_fields array on lines 9 and 28 with your custom field keys:

$excluded_fields = array( 'your_custom_field_key1', 'your_custom_field_key2' );

Replace your_custom_field_key1 and your_custom_field_key2 with the actual field keys of the custom fields you want to exclude. The field keys can be found in the MemberPress settings (Dashboard > MemberPress > Settings) under the Fields tab.

If you want to use a different URL parameter, replace ca with your desired parameter name, on lines 12 and 24:

if ( $post->ID > 0 && MeprProduct::is_product_page( $post ) && isset( $_GET['ca'] ) ) {
// Your code here
}

Code Preview
php
<?php
/**
 * MemberPress - Exclude Specific Custom Fields from Signup Forms
 * 
 * This code hides selected custom fields on MemberPress registration forms
 * when a specific URL parameter is present.
 */
add_filter( 'mepr_render_custom_fields', function( $fields ) {
    global $post;
    // List of fields to be excluded
    $excluded_fields = array( 'mepr_company_name', 'mepr_phone', 'mepr_logo' );
    // Check if it's a product page and 'ca' query parameter is present
    if ( $post->ID > 0 && MeprProduct::is_product_page( $post ) && isset( $_GET['ca'] ) ) {
        // Loop through the fields and exclude the specified ones
        foreach ( $fields as $i => $field ) {
            if ( in_array( $field->field_key, $excluded_fields ) ) {
                unset( $fields[$i] );
            }
        }
    }
    return $fields;
} );
add_filter( 'mepr-validate-signup', function( $errors ) {
    // Check if 'ca' query parameter is not present
    if ( !isset( $_GET['ca'] ) ) {
        return $errors;
    }
    // List of fields to be excluded from validation
    $excluded_fields = array( 'mepr_company_name', 'mepr_phone', 'mepr_logo' );
    // Loop through the errors and exclude the specified ones
    foreach ( $errors as $key => $error ) {
        if ( in_array( $key, $excluded_fields ) ) {
            unset( $errors[$key] );
        }
    }
    return $errors;
});

Comments

Add a Comment