spivurno
8/24/2013 - 1:30 PM

MOD: Ban/Limit Email Domains for Gravity Form Email Fields: Only Apply to Forms Displayed on Posts of a Specific Category

MOD: Ban/Limit Email Domains for Gravity Form Email Fields: Only Apply to Forms Displayed on Posts of a Specific Category

<?php
/**
* Ban/Limit Email Domains By Post Category
* 
* Requires: http://gravitywiz.com/2012/11/11/banlimit-email-domains-for-gravity-form-email-fields/
* Video:    http://screencast.com/t/ezkG0Hd6    
*/
add_filter( 'gform_pre_validation', 'gw_maybe_apply_email_domain_control' );
function gw_maybe_apply_email_domain_control( $form ) {
    global $post;
    
    if( ! is_object( $post ) )
        return $form;
    
    // UPDATE: "3" should be the category ID on which forms should have the email domain control applied
    $category_id = 3;
    $terms = wp_get_object_terms( $post->ID, 'category' );
    
    foreach( $terms as $term ) {
        if( $term->term_id == $category_id ) {
            gw_apply_email_domain_control();
            break;
        }
    }
    
    return $form;
}

function gw_apply_email_domain_control() {
    
    if( ! class_exists( 'GWEmailDomainControl' ) )
        return;
    
    // UPDATE: Modify these parameters as needed per instructions in the "Requires" link above
    new GWEmailDomainControl( array(
        'domains' => array( 'gmail.com', 'hotmail.com', 'test.com' ),
        'validation_message' => __( 'Oh no! <strong>%s</strong> email accounts are not eligible for this form.' ),
        'mode' => 'ban'
        ));
        
}