• Pat Garner

    (@wordpressorgpatgarnercom)


    For those times when you need finer access control granularity, when you need to hide/show a section of a page based upon the current user’s group, here is my solution. NOTE: I am not a PHP developer, just a casual user, so there are no doubt improvements that can be made to this.

    I put this in functions.php:

    /*
        Allow user group shortcode.
    
        Usage: [allow_user_group groupname="UAM_Group_Name|etcetera"]<HTML MARKUP>[/allow_user_group]
    */
    add_shortcode('allow_user_group', 'allow_user_group_func');
    function allow_user_group_func($atts, $content = '')
    {
        $result = ''; //  <-- user does not have permission
        extract(shortcode_atts(array('groupname' => ''), $atts));
        if(!empty($groupname))
        {
            global $wpdb;
            $entries = $wpdb->get_results
            (
                $wpdb->prepare
                (
                    "SELECT groupname FROM {$wpdb->prefix}uam_accessgroups uamag, {$wpdb->prefix}uam_accessgroup_to_object uamagto WHERE uamagto.group_id=uamag.id and uamagto.object_id=%d",
                    wp_get_current_user()->ID
                )
            );
            $parts = explode("|", $groupname);
            foreach($parts as $part)
            {
                foreach($entries as $entry)
                {
                    if(trim($entry->groupname) == trim($part))
                    {
                        $result = $content; //  <-- user has permission
                        break;
                    }
                }
            }
        }
        return $result;
    }

    and I put the following into the page itself:

    [allow_user_group groupname="Data_Top_Secret|Biatches_Only|etcetera"]
        You are authorized, biatch!
    [/allow_user_group]

    https://www.ads-software.com/plugins/user-access-manager/

Viewing 5 replies - 1 through 5 (of 5 total)
  • I used it, but I can’t read it as admin of the site, ?may I change something?

    Thread Starter Pat Garner

    (@wordpressorgpatgarnercom)

    You need to leave a code example. I have no idea what you are talking about “it.”

    Hello, thank you for your shortcode.I have same issue.
    UAM can’t add admin user to any group. If I use this code, admin user can’t see THE CONTENT, because admin is not belong to GROUP_NAME.

    [allow_user_group groupname="GROUP_NAME"]
    THE CONTENT
    [/allow_user_group]

    Do you have any idea how to show admin THE CONTENT ?

    Thanks.

    Thread Starter Pat Garner

    (@wordpressorgpatgarnercom)

    Ohhhh, now I understand miyanke’s post. Y’all’s problems are that you can’t add admin user to a user group. Well you can’t use my shortcode for role, only for user groups. Instead you must use a shortcode that works with roles.

    Google is your friend.

    https://blog.haraldkraft.de/2014/01/wordpress-shortcode-for-user-role-visibility/

    Than you for quick reply, Pat.
    And thank you for your infromation.

    Sorry for my double post.
    Thanks again!

Viewing 5 replies - 1 through 5 (of 5 total)
  • The topic ‘UAM 'allow_user_group' shortcode’ is closed to new replies.