Support » Plugin: Age Gate » Age Gate Country select option

  • I’m try to add Country selection to Age Gate by adding Custom Form Fields.

    I want the country selection to redirect to specific URL based on the selection.

    Please help.

Viewing 5 replies - 1 through 5 (of 5 total)
  • Plugin Author Phil

    (@philsbury)

    Hi @neilkonka,

    This is possible, but how to do it depends on if you’re using the standard or JavaScript version.

    Either way you need the dropdown, so you can do this like:

    
    add_filter('post_age_gate_custom_fields', 'ag_country_list', 10, 1);
    
    function ag_country_list($fields)
    {
        $countries = [
            [
                
                'name' => 'United States',
                'redirect' => '/us/my-page'
            ],
            [
                
                'name' => 'United Kingdom',
                'redirect' => '/gb/my-page'
            ],
            
        ];
    
        $fields .= '<label>Country</label>';
        $fields .= '<select id="ag-country" name="country" required>';
        $fields .= '<option value=""> -- Select -- </option>';
        foreach ($countries as $country) {
            $fields .= '<option id="ag-county" value="'. $country['redirect'] .'">'. $country['name'] .'</option>';
        }
        $fields .= '</select>';
        return $fields;
    }
    

    If you are using the standard version, you can add an action to the success hook to send users to where you want:

    
    add_action('age_gate_form_success', function ($a, $b = []) {
        if ($a['country']) {
            wp_redirect($a['country']);
            exit;
        }
    });
    

    Or for the JS version, use this JavaScript:

    
    jQuery(function () {
      jQuery(document).on('agegatepassed', function () {
        if (jQuery('select[name="country"]').val()) {
          window.location.href = jQuery('select[name="country"]').val();
        }
      });
    });
    

    The JS one does have a tiny delay, but still does the job.

    Thanks
    Phil

    neilkonka

    (@neilkonka)

    Thank-you Phil for you response.

    I have added the above standard version to the functions.php file but the loader is continues spinning and nothing is happening.

    I hope I’m adding code correctly.

    Following is my code.

    add_action( 'wp_enqueue_scripts', 'my_enqueue_assets' ); 
    
    function my_enqueue_assets() { 
    
        wp_enqueue_style( 'parent-style', get_template_directory_uri().'/style.css' ); 
    
    } 
    add_filter('pre_age_gate_custom_fields', 'top_custom_country', 10, 2);
    
    function top_custom_country($fields){
    	$fields .= age_gate_error('custom_country_input');
    	$countries = [
            [
                
                'name' => 'Canada',
                'redirect' => '/fr/'
            ],
            [
                
                'name' => 'French',
                'redirect' => '/pt/'
            ],
            
        ];
    	
    	$fields .= age_gate_error('custom_country_input');
    	$fields .= '<div class="top_age_customfield">';
    	$fields .= '<p style="margin-bottom:10px;">Are you allowed to be here?</p>'; 
    	
    	$fields .= '<label>Country</label>';
        $fields .= '<select id="ag-country" name="custom_country_input" required>';
        $fields .= '<option value=""> -- Select -- </option>';
        foreach ($countries as $custom_country_input) {
            $fields .= '<option id="ag-county" value="'. $custom_country_input['redirect'] .'">'. $custom_country_input['name'] .'</option>';
        }
        $fields .= '</select>';
    	$fields .= '</div>'; 
    	return $fields;
    }
    add_filter('age_gate_validation', 'top_validation_rules', 10, 1);
    
    function top_validation_rules($rules){
    	return array_merge($rules, [
    		'custom_country_input' => 'required'
    	]);
    }
    add_filter('age_gate_field_names', 'top_field_names', 10, 1);
    
    function top_field_names($names){
    	return array_merge($names, [
    		'custom_country_input' => "Country"
    	]);
    }
    add_action('age_gate_form_success', function ($a, $b = []) {
        if ($a['custom_country_input']) {
            wp_redirect($a['custom_country_input']);
            exit;
        }
    });
    add_filter('post_age_gate_custom_fields','termandcondition', 10, 1);
    function termandcondition(){
    	return '<p style="font-size: 14px;margin-top: 20px;">By accessing this site, you accept the Terms of Use and Privacy Policy.</p>';
    }
    
    function wpb_image_editor_default_to_gd( $editors ) {
        $gd_editor = 'WP_Image_Editor_GD';
        $editors = array_diff( $editors, array( $gd_editor ) );
        array_unshift( $editors, $gd_editor );
        return $editors;
    }
    add_filter( 'wp_image_editors', 'wpb_image_editor_default_to_gd' );
    

    Thanks
    Neil

    neilkonka

    (@neilkonka)

    And this is the site link http://demo.shreestar.com

    Plugin Author Phil

    (@philsbury)

    Hi @neilkonka,

    If the loader is spinning then you don’t want this bit as you’re using the JavaScript mode:

    
    add_action('age_gate_form_success', function ($a, $b = []) {
        if ($a['custom_country_input']) {
            wp_redirect($a['custom_country_input']);
            exit;
        }
    });
    

    Instead you should add this to one of your Javascript files:

    
    jQuery(function () {
      jQuery(document).on('agegatepassed', function () {
        if (jQuery('select[name="country"]').val()) {
          window.location.href = jQuery('select[name="country"]').val();
        }
      });
    });
    

    Thanks
    Phil

    neilkonka

    (@neilkonka)

    Than-you Phil its working with the Javascript.

    Really appreciate.

    Can we remove the delay?

    • This reply was modified 4 months ago by neilkonka.
Viewing 5 replies - 1 through 5 (of 5 total)
  • You must be logged in to reply to this topic.