spivurno
9/16/2014 - 4:29 AM

Gravity Perks // GP Limit Choices // Hide Form if Choices Exhausted

Gravity Perks // GP Limit Choices // Hide Form if Choices Exhausted

/**
 * Gravity Perks // GP Limit Choices // Hide Form if Choices Exhausted
 * http://gravitywiz.com/documentation/gp-limit-choices/
 *
 * Hide the form and display a message when the limit has been reached for a Limit-enabled field. This example will hide the form
 * if *any* choice is exhausted so it is best used if there is only one field with limits enabled.
 */
add_filter( 'gform_pre_render', 'gw_disable_form_if_choices_exhausted', 12 );
function gw_disable_form_if_choices_exhausted( $form ) {

    $message = 'This event is closed.';
    $has_choiceless_field = isset( gp_limit_choices()->choiceless[$form['id']] );

    if( $has_choiceless_field ) {
        add_filter( 'gform_get_form_filter_' . $form['id'], create_function( '', 'return \'' . $message . '\';' ) );
    }

    return $form;
}