Wordpress: user restrictions tags
Mar19Difficulty: 




I was working on a Wordpress blog, where I wanted to put some handy coding information, just for myself and my colleague’s. Ofcourse I don’t want the whole world to read it and luckily Wordpress can set the visibility of posts to private.
That’s nice, but when you have only private posts in a menu category, this menu option won’t be shown on the page. So, what I did, is placing a first category post (with a date of early history), in front of it. And this page contains no secret information.
Well yeah, ok, then the category option is visible in my menu, but when unknown blog readers click on this link, they will see a useless blogpost.
To solve this you need to script some php / wordpress function tags in your template.
Let’s use the current_user object. It can retrieve the level of the user.
The user level of an ‘Admin’ is 10, the level of a “Contributor” 1.
What I want is to show my “secret” menu, for everybody of userlevel 1 or above.
And to display to everybody the “normal” menu.
This is the code for in my sidebar.php:
//my normal menu, excluding the (id of the) secret menu
<?php wp_list_categories('exclude=10'); ?>
//my secret menu, excluding the (id of the) normal menu
<?php global $current_user;
get_currentuserinfo();
if($current_user->user_level >= 1){
wp_list_categories('exclude=11');
}
?>
Now you just have to create some level 1 or higher, users!
Posted in php |current_user, / php, / user, / user levels, / wordpress, / wordpress user restrictions
» 1 Comment, post your comment now. »
One Response to “Wordpress: user restrictions tags”
U can use this idea, aswell for hiding content.
Let’s say all user levels lower then 1, will see the string “you should be logged in to read more…”. – And the all user levels >= 1 see the hidden content.
Comments
- Pulkit Singhal
- Neil
- mike nittmann
- geniutrixone
- Mika
- Romfrosk
- steve
- shahzad
- Maurice
- Lee
- Oskar Krawczyk