Restrict User Access Shortcodes

Below, you’ll find a detailed guide on how to add the Restrict User Access Shortcodes to your WordPress website, including their parameters, examples, and PHP function code. Additionally, we’ll assist you with common issues that might cause the Restrict User Access Plugin shortcodes not to show or not to work correctly.

Before starting, here is an overview of the Restrict User Access Plugin and the shortcodes it provides:

Plugin Icon
Restrict User Access – Membership & Content Protection

"Restrict User Access – Membership & Content Protection is a powerful WordPress plugin. It lets you control member access to your content and create exclusive areas on your website."

★★★★✩ (89) Active Installs: 20000+ Tested with: 6.2.3 PHP Version: 5.6
Included Shortcodes:
  • [restrict]
  • [rua-user-levels]

Restrict User Access [restrict] Shortcode

The Restrict User Access shortcode allows you to control user access to content based on specified roles or levels. It checks if a user has global access, if not, it validates their role or level. It also supports negation and content dripping based on the user’s membership start date. If the user lacks access, it replaces the content with a specified page’s content.

Shortcode: [restrict]

Parameters

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

  • role – Identifies the user role to restrict access
  • level – Denotes the user access level for restriction
  • page – Specifies the page ID to be displayed when access is restricted
  • drip_days – Sets the number of days before content becomes accessible

Examples and Usage

Basic example – Restrict content to a specific user level

[restrict level="premium"] Your premium content goes here [/restrict]

Advanced examples

Restrict content to multiple user levels. The content will be visible to users who have either the “premium” or “gold” level.

[restrict level="premium,gold"] Your premium or gold content goes here [/restrict]

Restrict content to a specific user role. In this example, the content will be visible to users with the “editor” role.

[restrict role="editor"] Content for editors goes here [/restrict]

Restrict content and replace it with the content of a specific page if the user does not have access. In this example, if the user does not have the “premium” level, they will see the content of the page with the ID of 5.

[restrict level="premium" page="5"] Your premium content goes here [/restrict]

Restrict content to a specific user level and drip the content after a certain number of days. In this example, the content will be visible to users with the “premium” level, but only after they have been a member for 7 days.

[restrict level="premium" drip_days="7"] Your premium content goes here [/restrict]

PHP Function Code

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

Shortcode line:

add_shortcode('restrict', [$this,'shortcode_restrict']);

Shortcode PHP function:

function shortcode_restrict($atts, $content = null)
    {
        $user = rua_get_user();
        if ($user->has_global_access()) {
            return do_shortcode($content);
        }

        $a = shortcode_atts([
            'role'      => '',
            'level'     => '',
            'page'      => 0,
            'drip_days' => 0
        ], $atts, 'restrict');

        $has_access = false;

        if ($a['level'] !== '') {
            $has_negation = strpos($a['level'], '!') !== false;
            $user_levels = array_flip($user->get_level_ids());
            if (!empty($user_levels) || $has_negation) {
                $level_names = explode(',', str_replace(' ', '', $a['level']));
                $not_found = 0;
                foreach ($level_names as $level_name) {
                    $level = $this->get_level_by_name(ltrim($level_name, '!'));
                    if (!$level) {
                        $not_found++;
                        continue;
                    }
                    //if level param is negated, give access only if user does not have it
                    if ($level->post_name != $level_name) {
                        $has_access = !isset($user_levels[$level->ID]);
                    } elseif (isset($user_levels[$level->ID])) {
                        $drip = (int)$a['drip_days'];
                        if ($drip > 0 && $user->has_level($level->ID)) {
                            //@todo if extended level drips content, use start date
                            //of level user is member of
                            $start = $user->level_memberships()->get($level->ID)->get_start();
                            if ($start > 0) {
                                $drip_time = strtotime('+' . $drip . ' days 00:00', $start);
                                $should_drip = apply_filters(
                                    'rua/auth/content-drip',
                                    time() <= $drip_time,
                                    $user,
                                    $level->ID
                                );
                                if ($should_drip) {
                                    continue;
                                }
                            }
                        }
                        $has_access = true;
                    }
                    if ($has_access) {
                        break;
                    }
                }
                //if levels do not exist, make content visible
                if (!$has_access && $not_found && $not_found === count($level_names)) {
                    $has_access = true;
                }
            }
        } elseif ($a['role'] !== '') {
            $user_roles = array_flip(wp_get_current_user()->roles);
            if (!empty($user_roles)) {
                $roles = explode(',', str_replace(' ', '', $a['role']));
                foreach ($roles as $role_name) {
                    $role = ltrim($role_name, '!');
                    $not = $role != $role_name;
                    //when role is negated, give access if user does not have it
                    //otherwise give access only if user has it
                    if ($not xor isset($user_roles[$role])) {
                        $has_access = true;
                        break;
                    }
                }
            }
        }

        /**
         * @var bool $has_access
         * @var RUA_User_Interface $user
         * @var array $a
         */
        $has_access = apply_filters('rua/shortcode/restrict', $has_access, $user, $a);

        if (!$has_access) {
            $content = '';

            // Only apply the page content if it exists
            $page = $a['page'] ? get_post($a['page']) : null;
            if ($page) {
                setup_postdata($page);
                $content = get_the_content();
                wp_reset_postdata();
            }
        }

        return do_shortcode($content);
    }

Code file location:

restrict-user-access/restrict-user-access/level.php

Restrict User Access [rua-user-levels] Shortcode

The Restrict User Access shortcode ‘rua-user-levels’ is designed to fetch and display the access levels of a specific user. This shortcode takes a user’s ID as an argument, retrieves the user’s access levels, and returns them as a comma-separated list. The levels are fetched from the RUA_App instance.

Shortcode: [rua-user-levels]

Parameters

Here is a list of all possible rua-user-levels shortcode parameters and attributes:

  • id – It is the unique identifier of a user.

Examples and Usage

Basic example – A basic usage of the ‘rua-user-levels’ shortcode would be to pass the user ID as a parameter to retrieve the user’s access levels.

[rua-user-levels id=2 /]

Advanced example – In the context of the ‘rua-user-levels’ shortcode, an advanced usage might involve passing multiple user IDs to retrieve their respective access levels. However, please note that the current function does not support multiple IDs. Therefore, you would have to modify the function to accept and process an array of IDs.

[rua-user-levels id="2,3,4" /]

Remember, the shortcode will return a comma-separated list of user access levels based on the given ID or IDs. If the user or users do not have any access levels, it will return an empty string.

PHP Function Code

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

Shortcode line:

add_shortcode('rua-user-levels', [$this,'shortcode_user_levels']);

Shortcode PHP function:

function shortcode_user_levels($atts)
    {
        $a = shortcode_atts([
            'id' => null
        ], $atts, 'rua-user-level');

        $user = rua_get_user($a['id']);

        $levels = RUA_App::instance()->get_levels();
        $level_names = [];
        foreach ($user->get_level_ids() as $id) {
            if (isset($levels[$id])) {
                $level_names[] = $levels[$id]->post_title;
            }
        }

        return implode(', ', $level_names);
    }

Code file location:

restrict-user-access/restrict-user-access/level.php

Conclusion

Now that you’ve learned how to embed the Restrict User Access Plugin shortcodes, 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 *