Skip Navigation

[Resolved] Conditional statement for usermeta

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

Problem:

Perform a conditional action based on the user role based on the user name that was provided in the URL.

Solution:

In order to get this done I would recommend the use of a custom shortcode.

To get the user role from the user name in the url you can use the shortcode below.

// Add Shortcode
function wp_get_user_role( $atts ) {
  
    // Attributes
    $atts = shortcode_atts(
        array(
            'username' => '',
        ),
        $atts
    );
  
    $user_meta=get_user_by('login',$atts['username']);
      
    $user_roles=$user_meta->roles;
    return $user_roles[0];
  
}
add_shortcode( 'wp_get_user_role', 'wp_get_user_role' );

The shortcode is used like this
[wp_get_user_role username=' [wpv-search-term param='church']']

Where you can use the [wpv-search-term param='url-param'] to get the username from the param.

From there you can add this shortcode to the conditional statement.

This support ticket is created 5 years, 7 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.

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 16 replies, has 2 voices.

Last updated by mikeH-3 5 years, 7 months ago.

Assisted by: Shane.

Author
Posts
#1253377

I am using this view filter to show only posts from this specific author:
hidden link

How do I setup a conditional inside this view that says basically:

If the church in this url "yoursite/page-with-this-view/?church=lakeview" is an 'editor' then show "This is an editor account" in the page

'Editor' is the <b>user role</b> that the account 'lakeview' is assigned to.

This message would show to people not logged in.

Thanks much!!

#1253511

Shane
Supporter

Languages: English (English )

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

Hi Mike,

So you pretty much want to display a message based on the user role correct?

If not then what would the conditional be based on.

Please let me know
Thanks,
Shane

#1253605

Based on the user role of the username that is at the end of the url for a given view. So I want the user role of the account with username lakeview when someone goes to hidden link

It is pulling that username using that filter I sent screenshot of.

And this would display publicly. This doesn't apply to being logged in.

#1254377

Shane
Supporter

Languages: English (English )

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

Hi Mike,

So if a logged in user got to hidden link you want them to get this message.

"If the church in this url "yoursite/page-with-this-view/?church=lakeview" is an 'editor' then show "This is an editor account" in the page"

However for logged out users you want the display to remain ?

I'm not sure i understand clearly

Please let me know.
Thanks,
Shane

#1254483

No, I am explaining to you what I want in plain English terms instead of code terms. Ignore that that sentence was in a code block.

Basically every admin will create a unique username in wordpress. Then their specific page is rendered using the author filter and looks like something like "yoursite.com/?church=lakeview" where lakeview is the username of that account. So right now all the posts written by lakeview account show up on this page.

So what I need now is the conditional statement that would see the username in the url and figure out what user role it is assigned to. If it is an 'editor' for instance, it woulds say "this page is an editor page".

This condition would run NOT logged into the site, and the public should see it.

Does that help?

#1254503

Shane
Supporter

Languages: English (English )

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

Hi Mike,

Thank you for the clarity.

This is actually quite simple then simple to achieve then. You will need to write some custom code to get the role of the user that the view loop will display.

Add this custom shortcode to your Toolset custom code option in Toolset-> Settings -> Custom Code.

// Add Shortcode
function wp_get_user_role( $atts ) {

	// Attributes
	$atts = shortcode_atts(
		array(
			'id' => '',
		),
		$atts
	);

	$user_meta=get_userdata($atts['id']);
	
	$user_roles=$user_meta->roles;
	return $user_roles[0];

}
add_shortcode( 'wp_get_user_role', 'wp_get_user_role' );

Please ensure that it is enabled.
Then you are going to write the name of the shortcode i.e 'wp_get_user_role' in the view 3rd party shortcode argument in Toolset -> Settings -> Frontend Content.

Finally the usage would be.


[wpv-conditional if="( '[wp_get_user_role id=' [wpv-user field='ID']']' eq 'subscriber' )"] This is a subscriber [/wpv-conditional]

Please let me know if this helps.
Thanks,
Shane

#1254569

Thanks for all this! I see the message when logged in but not logged out, which is the main purpose. That's what I am stuck on. Let me know what needs to be done.

Thanks!

#1254583

Shane
Supporter

Languages: English (English )

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

Hi Mike,

Could you let me know what you are seeing when you log out ?

Also is your user view displaying anything to the logged out users?

Please let me know.
Thanks,
Shane

#1254585

When I am at, for example, hidden link and logged in, it shows me the text "This is a subscriber". When I am logged out, on same page, it shows nothing.

Yes the view is showing the 1st post to the public.

#1254627

I don't know if this helps but for displaying usermeta for someones account through those author urls, I use something like this:

[wpv-conditional if="( '[types usermeta='rss-feed' user_name='[wpv-search-term param=church]' output='raw'][/types]' ne '' )"]
This is our rss feed
[/wpv-conditional]

This does work. Essentially it shows whatever this user entered in their 'rss-feed' usermeta field, and it shows it whether logged in or not. ANd it's always attached to the username at the end of the url, like hidden link

But I don't know how to make it work for something like issue I have now with matching user roles.

#1255173

Shane
Supporter

Languages: English (English )

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

Hi Mike,

Could you provide me with access so that I can check this out in a little more detail for you ?

The private fields have been enabled for your next response.

Thanks,
Shane

#1257401

Shane
Supporter

Languages: English (English )

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

Hi Mike,

I'm a little confused now.

Your view is listing Posts, i was under the impression that it will be listing users.

If I am to understand correctly now, this view is listing out the posts of the authors that are mentioned in the URL.

if i'm not correct then right now im not sure what exactly you want to achieve.

Thanks,
Shane

#1257405

No it is listing only the posts by the username specified in the url. Does that help? It's not a user view, its a post view.

#1257439

Shane
Supporter

Languages: English (English )

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

Hi Mike,

Now i'm following a bit better, Then you wanna display something conditional based on that user's role that was provided in the url correct?

I may need to modify the custom shortcode that i've written .

Thanks,
Shane

#1257651

Correct... username is in the url (at the end of it) and I want to show or hide things to the public based on that accounts username role.

example: if site.com/?church=lakeview is in user role 'free' then don't show this shortcode [give]

This is a toolset post view I am showing, not a toolset user view.