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
#1258567

Shane
Supporter

Languages: English (English )

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

Hi Mike,

I modified the shortcode a bit and it should be working now.

The new usage example is.

[wp_get_user_role username=' [wpv-search-term param='church']']

Finally here is the updated code.


// 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' );

Please let me know if this helps now.

Thanks,
Shane

#1258879

It LOOK to be working now, thanks!! I'll let you know if I find something this is what I was looking for. Thanks much!!