Hi, I want to control access at a block level to specific user roles. For example, when I have a social share block at the end of a blog, I want it to be available only if the user is logged on to the site; if the user is not logged on to the site the social share should be visible but must prompt the user to logon when they click the social share button. The problem that I am unable to get around is to define user role access at a block level. Please let me know how should I go about doing it.
I looked at your documentation, but could not find answer that addresses my ask; also, I am working on a local server and so cannot share a link. FYI. I am using Kadence theme.
Looking forward to your input.
Any pointers? Please update.
Hello,
You can try with Toolset conditional block, setup the condition as "Current user ID" not equal to empty, see my screenshot logged-in.JPG.
Then display other blocks as you want,
More help:
https://toolset.com/course-lesson/using-toolset-conditional-block/
Thanks for sharing the block details. I tried using the conditional block, but could not mimic what I am looking for. Here are the conditions that I tried out:
Current user is not equal to Empty.
NOT (
Current user is not equal to role Subscriber.
)
Current user is not equal to role Subscriber.
The outcome of the condition is either the button is visible or not visible to a user of a particular role. This is not what I want.
Here is the behavior I want: The button should be visible to all users. However, if the user is not a Subscriber, a log on window must be displayed with the button is clicked. In other words, the button is visible to all users, but to access the functionality of the button, the user must be logged on to the site.
How can I get this condition to work?
Hi,
Thanks for the pointer. I tried the following conditions:
Current user is not equal to Empty.
Current user is not equal to role Subscriber.
NOT (
Current user is not equal to role Subscriber.
)
However, in all the three cases, the button on which the condition is applied is either displayed or hidden. This is not what I want. Here is what I want: I want the button to be displayed always. When a user whose role is not a Subscriber clicks the button, the user must be prompted to log on to the site before proceeding to use the button. In other words, the button is visible to non logged on users, but can be used only by a logged on user.
What condition settings must I use to achieve it? Please suggest.
Stating the requirement a bit differently: I want users to view the site content (course/blog). The site is set up somewhat similar to toolset courses; however, when the user clicks Start the course or Read more, the content should be accessible only to registered members. The idea is when people browse the site and find a blog interesting, they need to register/pay for the complete content.
In short, on click of the button (Start or Read more) the user must get a login/register page if they have not logged on. (I am not using Woocommerce; using Ultimate Membership to manage membership and subscription.)
Please suggest.
Hi,
Thank you for sharing these further details.
Luo will be on vacation this week, so I'll be following up on this ticket.
What you're planning to achieve is possible, using the same conditional blocks that Luo referred to.
You'll need to divide your blog post's content in the content template, into two parts:
Part 1: the content that is shown to the guests/visitors who are not logged in:
You'll add a first conditional block with the condition "Current user ID" is equal to empty.
( which would mean that the current visitor is not logged-in )
Within this, conditional block, you'll add:
- the partial or introductory content
- message that you'll need to be a member to view the full content
- the link to the registration and/or login page
Part 2: the content that is shown to the users/visitors who are logged in:
You'll add a second conditional block with the condition "Current user ID" is not equal to empty.
( which would mean that the current visitor is logged-in )
Within this block, you can add the full actual content that should be visible to only the logged-in users.
Note: For more complex cases where you need to show or hide the specific content, based on the user's role, you can use the "Current user role" evaluation.
( example screenshot attached )
I hope this helps and please let me know if you need any further assistance around this.
regards,
Waqar
Thanks Waqar for the details. Unfortunately, what you have suggested does not solve the condition that I am looking to achieve. What you have suggested is to split the content into one that is visible to the non logged in user and another part to logged in user. In the blog archive page, I want the read more link to the blog to be ALWAYS visible. When the user clicks the read more button, here is the expected behavior:
* If the user is not logged in, it should redirect user to the login page
* If the user the logged in, the blog should be displayed.
Going by the suggestions that I received so far, it looks like this is not achievable by Toolset access.
Thanks for writing back.
Your observation is correct and Toolset Access doesn't offer a feature to automatically redirect the visitor to the login page.
To implement redirection like this, you can use some custom code.
For example, suppose that I have 3 custom post types with slugs "shops", "products" and "books" and I'd like to add a redirection so that if a non-logged in visitor tried to visit any of these single post pages, he/she is automatically redirected to the login page with ID "123".
The custom code, in this case, would look like this:
function custom_login_redirect() {
if ( (!is_user_logged_in()) && (is_singular('shops, products, books')) ) {
wp_redirect( get_permalink('123') );
exit;
}
}
add_action('get_header', 'custom_login_redirect');
Please replace the ID "123" with the actual page ID of your login page and the comma-separated list of slugs "shops, products, books", with the slugs of your actual custom post types.
The above code snippet can be included through either Toolset's custom code feature ( ref: https://toolset.com/documentation/adding-custom-code/using-toolset-to-add-custom-code/ ) or through the active theme's "functions.php" file.
Note: The custom code examples from our forum are shared to get you started in the right direction. You're welcome to adjust them as needed and for more personalized customization assistance, you can consider hiring a professional from our list of recommended contractors:
https://toolset.com/contractors/
Thanks Waqar for the snippet of code. Although it did not solve the problem in its totality, I understand what needs to be done to get there. Marking the issue as resolved. Thank you!