Age Gate Shortcode

Below, you’ll find a detailed guide on how to add the Age Gate Shortcode to your WordPress website, including its parameters, examples, and PHP function code. Additionally, we’ll assist you with common issues that might cause the Age Gate Plugin shortcode not to show or not to work correctly.

Before starting, here is an overview of the Age Gate Plugin and the shortcodes it provides:

Plugin Icon
Age Gate

"Age Gate is a WordPress plugin designed to restrict content access to users of a certain age. Ideal for age-sensitive content, it ensures compliance with age-related legal requirements."

★★★★☆ (58) Active Installs: 30000+ Tested with: 6.1.4 PHP Version: 7.4
Included Shortcodes:
  • [age-gate]

Age Gate [age-gate] Shortcode

The Age-Gate shortcode is a flexible tool used to restrict content based on user age. The shortcode: [age-gate] uses default settings but allows for customization. The shortcode checks the user’s age against the specified ‘age’ attribute. If the user’s age meets or exceeds the specified age, or if the user is a bot, the content within the shortcode is displayed. If the page isn’t a single post, the shortcode returns an empty string. The ‘poster’ attribute allows for an image to be displayed if content is restricted. The shortcode also supports JS method, which encodes restricted content and options into base64 format. It also handles errors and counts the number of shortcode uses.

Shortcode: [age-gate]

Parameters

Here is a list of all possible null shortcode parameters and attributes:

  • age – Specifies the default age for the age-gate plugin
  • title – Defines the label for the buttons on the age-gate
  • type – Determines the type of input for the age-gate
  • poster – Sets an image to be displayed on the age-gate
  • force – A boolean value to force age verification

Examples and Usage

Basic example – This shortcode displays the age-gate with default settings.

[age-gate /]

Advanced examples

Using the shortcode to display the age-gate by referencing a specific age. The age-gate will then require the user to be at least of the specified age to access the content.

[age-gate age=21 /]

Adding a title to the age-gate button. This shortcode will display the age-gate with a custom title on the button.

[age-gate title="Confirm your age" /]

Using the shortcode to display the age-gate with a specific input type. The age-gate will display the input type as specified in the shortcode.

[age-gate type="dropdown" /]

Using the shortcode to force the age-gate regardless of the global settings. This shortcode will display the age-gate even if the global settings are set to not display it.

[age-gate force=true /]

Using the shortcode to display a poster while the age-gate is loading. The shortcode will display the specified poster while the age-gate is loading.

[age-gate poster="https://example.com/poster.jpg" /]

PHP Function Code

In case you have difficulties debugging what causing issues with [null] shortcode, check below the related PHP functions code.

Shortcode line:

add_shortcode($this->shortcode, [$this, 'registerShortcode']);

Shortcode PHP function:

function registerShortcode($atts, $content, $tag)
    {
        $settings = Settings::getInstance();
        self::$count = self::$count + 1;

        $atts = shortcode_atts(
            [
                'age' => $settings->defaultAge,
                'title' => $settings->labelButtons,
                'type' => $settings->inputType,
                'poster' => false,
                'force' => false,
            ],
            $atts,
            'age-gate'
        );

        if ($this->status($settings, $atts) === true || $settings->method === 'standard' && Crawler::isBot()) {
            return $content;
        }

        if (!is_single()) {
            return '';
        }

        $shortcodeSettings = clone($settings);
        $shortcodeSettings->set('inputType', $atts['type']);
        $shortcodeSettings->set('labelButtons', $atts['title']);

        if ($atts['poster'] ?? false) {
            if (is_numeric($atts['poster'])) {
                $shortcodeSettings->set('poster', wp_get_attachment_url($atts['poster']));
            } else {
                $shortcodeSettings->set('poster', $atts['poster']);
            }
        }

        $shortcodeContent = new Content(null, 'shortcode', $shortcodeSettings);
        $shortcodeContent->setAge($atts['age']);

        $view = $this->view($shortcodeSettings, $shortcodeContent);

        if ($settings->method === 'js') {
            wp_enqueue_script('age-gate-shortcode');
            $view->addData([
                'restrictedContent' => base64_encode($content),
                'options' => base64_encode(json_encode(array_merge($atts, ['cookieName' => $settings->getCookieName(), 'cookieDomain' => Cookie::getDomain()]))),
            ]);
        } else {
            $view->addData(['e' => StandardController::$errors, 'c' => self::$count]);
        }
        $markup = $view->compile('shortcode/shortcode-' . $settings->method);

        // remove the attribute
        return $markup;
    }

Code file location:

age-gate/age-gate/vendor/asylum/common/src/Shortcode.php

Conclusion

Now that you’ve learned how to embed the Age Gate Plugin shortcode, understood the parameters, and seen code examples, it’s easy to use and debug any issue that might cause it to ‘not work’. If you still have difficulties with it, don’t hesitate to leave a comment below.

Comments

Leave a Reply

Your email address will not be published. Required fields are marked *