Skip Navigation

[Resolved] Adding specific users to Access shortcode

This thread is resolved. Here is a description of the problem and solution.

Problem:

The issue here is that the user wanted to restrict specific users using our Access shortcode.

Solution:

Unfortunately this isn't possible with our Access shortcode as it only works based on the user role.

However I was able to come up with a little solution/workaround for you with this custom shortcode.
Add the following to your functions.php file

// Add Shortcode
function restrict_user( $atts , $content = null ) {
    // Attributes
    $atts = shortcode_atts(
        array(
            'user_id' => '',
        ),
        $atts
    );
    $current_user = get_current_user_id();
    $restricted_ids=explode(",",$atts['user_id']);
    if(in_array($current_user,$restricted_ids)){
        $content = "";
    }
    return "<p>".$content."</p>";
}
add_shortcode( 'restrict_users', 'restrict_user' );

So how you'll use this is like this [restrict_users user_id="32,4"]My Content to Restrict[/restrict_users]

This support ticket is created 6 years, 9 months ago. There's a good chance that you are reading advice that it now obsolete.

This is the technical support forum for Toolset - a suite of plugins for developing WordPress sites without writing PHP.

Everyone can read this forum, but only Toolset clients can post in it. Toolset support works 6 days per week, 19 hours per day.

No supporters are available to work today on Toolset forum. Feel free to create tickets and we will handle it as soon as we are online. Thank you for your understanding.

Sun Mon Tue Wed Thu Fri Sat
- 9:00 – 12:00 9:00 – 12:00 9:00 – 12:00 9:00 – 12:00 9:00 – 12:00 -
- 13:00 – 18:00 13:00 – 18:00 13:00 – 18:00 14:00 – 18:00 13:00 – 18:00 -

Supporter timezone: America/Jamaica (GMT-05:00)

This topic contains 3 replies, has 2 voices.

Last updated by Shane 6 years, 9 months ago.

Assisted by: Shane.

Author
Posts
#617811

Hi

1. I have created a user role called Role A and assigned it to some users. I have then created a shortcode that only allows users with Role A to view like so...

[toolset_access role="Role A" operator="allow"]This content is only viewable to users assigned Role A[/toolset_access]

2. Next I created a user role called Role B and assigned it to some other users. I then created a short code that only allows users with Role B to view like so...

[toolset_access role="Role B" operator="allow"]This content is only viewable to users assigned Role B[/toolset_access]

My issue occurs because there is a small subset of my users that need to be able to view the content in both. In other words they need to be able to see content that is viewable to BOTH Role A and Role B. Because there is not currently a way that I can assign a user 2 roles. I need to find a way around this.

Is it possible to add specific users to the shortcode. Something like...

[toolset_access role="Role A" user id="99,98,66" operator="allow"]This content is only viewable to users assigned Role A plus 3 individuals who are subscribers[/toolset_access]

Which says only users assigned Role A plus these three users can view this content?

Thanks!

#617938

Shane
Supporter

Languages: English (English )

Timezone: America/Jamaica (GMT-05:00)

Hi Stewart,

Thank you for contacting our support forum.

Unfortunately no this won't be possible to use this shortcode to restrict individual users.

Is it a case where you would like to restrict an entire page or just certain contents within that page ?

Please let me know.
Thanks,
Shane

#617957

Hi

I have a post group called Business Intelligence. There are two pages assigned to this post group. The pages are called Dashboard A and Dashboard B (each page has an embedded dashboard with sensitive info that can ONLY be viewed by people with correct role who are also a member of the BI Post group)

I have a dashboard which has information that ONLY role A can see embedded in a page. Ive restricted the access using Access shortcode as shown in previous ticket

I have another Dashboard which only Role B can see embedded in a different page. Ive restricted the access using Access shortcode as shown in previous ticket. I then have the business intelligence team (a team of 5) who need to be able to see BOTH Dashboards.

#618022

Shane
Supporter

Languages: English (English )

Timezone: America/Jamaica (GMT-05:00)

Hi Stewart,

Thank you i'm able to understand a bit better now but i must apologize that our shortcode is not able to perform this task for you.

However I was able to come up with a little solution/workaround for you with this custom shortcode.
Add the following to your functions.php file

// Add Shortcode
function restrict_user( $atts , $content = null ) {
	// Attributes
	$atts = shortcode_atts(
		array(
			'user_id' => '',
		),
		$atts
	);
	$current_user = get_current_user_id();
	$restricted_ids=explode(",",$atts['user_id']);
	if(in_array($current_user,$restricted_ids)){
	    $content = "";
	}
	return "<p>".$content."</p>";
}
add_shortcode( 'restrict_users', 'restrict_user' );

So how you'll use this is like this [restrict_users user_id="32,4"]My Content to Restrict[/restrict_users]

Please try this and let me know if it helps.
Thanks,
Shane