• Resolved harlointeractive

    (@harlointeractive)


    Age gate works great to gate pages but when someone uses search and a gated product is one of the search results then the age gate is triggered from the search template which is not desired.

    Test at – https://www.herb-pharm.com/?s=turmeric

    The Hemp products are gated but this should only happen when someone clicks the product detail page, not on the search results page. Is there a fix for this?

    The page I need help with: [log in to see the link]

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

    (@philsbury)

    Hi @harlointeractive,

    You can filter it to not include search. There’s two ways you can do this and which one you choose comes down to your setup.

    Option 1: PHP

    You can add the following snippet to you functions.php (or custom plugin)
    This will work in both PHP (see below for caveat) and Javascript modes of the plugin

    add_filter('age_gate_restricted', function ($restrict, $data) {
        if (isset($data->type) && $data->type === 'search') {
            return false;
        }
    
        return $restrict;
    }, 10, 2);

    Option 2: Javascript
    This is only available in the Javascript version.
    First you need to enable hooks in the Age Gate Advanced settings.
    Then in a javascript file you can add:

    ageGateHooks.addFilter('age_gate_restricted', 'demo', function(restrict, data) {
      if(data.type === 'search') {
        return false;
      }
      return restrict;
    
    });

    Or, if you don’t have access to any JS files, you can add it to the footer of the page:

    add_action('wp_footer', function () {
        ?>
        <script>
    
            ageGateHooks.addFilter('age_gate_restricted', 'demo', function(restrict, data) {
                if(data.type === 'search') {
                    return false;
                }
                return restrict;
    
            });
        </script>
        <?php
    });

    While the pure PHP version will work for JS mode, it does an Ajax request, this is obviously an additional server call you may not want, and could delay age gate showing if there’s lots of queries happening. Essentially, the JS method is probably better if you’re using the JS age gate.

    Thanks
    Phil

    Thread Starter harlointeractive

    (@harlointeractive)

    Awesome, thank you for the feedback!

Viewing 2 replies - 1 through 2 (of 2 total)
  • The topic ‘Age Gate Triggering on Search’ is closed to new replies.