Hi... i'm trying to write an hack that display the current logged in user role on the sidebar.. there is some function that can do this?
Hi... i'm trying to write an hack that display the current logged in user role on the sidebar.. there is some function that can do this?
anyone?
just the name? Like it display something like: "Welcome user!"?
there is a script for that:
<?php global $user_nickname;
get_currentuserinfo();
if ( $user_nickname<> "" ) { ?>
<?php echo $user_nickname ?>
<?php } else { ?>
Welcome Guest<?php } ?>
No doubt there is a better way then doing it like this but...
function dirty_get_role($id){
$role = explode('"',$wpdb->get_var("SELECT meta_value FROM wp_usermeta WHERE meta_key = 'wp_capabilities' and user_id = '$id'"));
return $role[1];
}
This is a VERY, dirty way of doing it!
For Bossy: No.. sorry for my english, but i need something that return the user role of the user that is logged in, like the Phunky dirty function :D...
I will try his way for the moment...
if someone have another metod to do this is very apreciate...
Thanks
Oké, I upgrade recently to WP 2. So I didn't understand 'Roles'. But now I do!
I tried to put the function directly in my sidbar for a test but don't work... i tryied this:
<?php
global $userdata;
get_currentuserinfo();
$role = explode('"',$wpdb->get_var("SELECT meta_value FROM wp_usermeta WHERE meta_key = 'wp_capabilities' and user_id = '$userdata->ID'"));
echo $role[1];
?>
and work very well... but i use widgets, and when i putted in my function.php (or use phpexec widget), all under the widget in the home page disappear... where i'm wrong?? :(
This is the code i used for functions.php:
function widget_user_role() {
global $userdata;
get_currentuserinfo();
$role = explode('"',$wpdb->get_var("SELECT meta_value FROM wp_usermeta WHERE meta_key = 'wp_capabilities' and user_id = '$userdata->ID'"));
echo $role[1];
}
if ( function_exists('register_sidebar_widget') )
register_sidebar_widget(__('User Role'), 'widget_user_role');
Im not too sure how to help you here, ive not really looked at widgets myself.
Do you get a error at all on the sidebar? or is it just a case of nothing else will display after the widget_user_role()prints out?
Sorry i should have also mentioned above that you would need to global $userdata if you hadnt already got hold of the userid.
I don't get an error... nothing will display after the function call... but if i try the same code outside of a function it worck perfectly.
I know about the $userdata variable but, the function you suggest me don't work if i put it in a template file... i used the code without the function (as I mentioned above) and the role is shown correctly.
in practice:
1)the code only work outside a function in the template (i will try to put the function in other file and the call in the template or the widget)
2)the code don't work in a widget
The function i wrote only returns the role value, so you would need to echo dirty_get_role(); for my one to print.
Try putting your widget_user_role() as the last widget on your sidebar, if it then displays all the other widgets you know there summat wrong with the widget_user_role() function its self.
Also if its working when not being used as a widget it would seem to suggest you have not written the function correctly, i belive theres a certain way you are supose to write you widgets.
But as i said before, ive not really bothered with widgets so cant really help much on that side of things!
Hi phunky... tnx for your reply. I solved with this your function mod:
<?php function get_role_from_db() {
global $wpdb, $userdata;
get_currentuserinfo();
$ruoloutente = explode('"',$wpdb->get_var("SELECT meta_value FROM wp_usermeta WHERE meta_key = 'wp_capabilities' and user_id = '$userdata->ID'"));
echo $ruoloutente[1];
} ?>
I forgot the "global $wpdb" variabile... :P
I can call it without an echo, and work perfectly if I put it in the sidebar, in a plugin, or in a widget in functions.php of my template. Only don't work if i put the entire function in a phpexec widget (it's a text widget that can eval() php code), but i think it's a bug that i must discuss with the widget's author...
In any way, if there is some less dirty function that can do this, i appreciate a little help... thanks to all replyers
Hi Rediect1, glad you got it sorted!
Im almost certain there is a built in function to get the user role but ive totaly forgot what it is.
Tbh i tend to re-create the get_currentuserinfo() function myself with one that gathers all the user detail including wp_usermeta and the user role.
Anyway im glad you got it to work :)
This topic has been closed to new replies.