Display Info if admin, otherwise hide
-
In my sidebar I would like a link that says “Admin Dashboard” when an admin is logged in. If the person that is logged in is not an admin but a regular user, then I want the link to say “Change Account Information”.
Is there a built-in WP function for this such as is_admin?
Thanks!
-
There’s no conditional function like that (there *is* an is_admin(), but it tests whether one is on an admin page).
However, you could check for a value to $user_ID — which will have one only when a visitor to your site is logged in — then use the ‘internal’ function current_user_can() to perform your test for an admin:
<?php global $user_ID; if( $user_ID ) : ?> <?php if( current_user_can('level_10') ) : ?> "Admin Dashboard" <?php else : ?> "Change Account Information" <?php endif; ?> <?php endif; ?>
Reference:
https://codex.www.ads-software.com/Roles_and_CapabilitiesThat was exactly what I was looking for. Thank you so much!
Thanks so much both you guys. gdnovey for asking and Kafkaesqui for knowing an answer. I had a need for a simple site. It has a little web application. It’s free but it’s important to me that people sign up. Using this information, if somebody is registered and signed up, then I can make the content of a certain page appear.
If they’re not logged in, an alternate ‘sorry’ message displays, and requests them to register and log in.
This bit of trickery uses the ‘Inline PHP’ plugin, so that I can do the trick on only one page, by typing (as html) the following code onto the page. (*The ‘Inline PHP* plugin uses ‘<exec>’ opening and closing tags instead of the usual php open and close tags.)
<exec> global $user_ID; if( $user_ID ) { echo "<iframe src=\"https://somedomain.com/somepage.php\" name=\"somename\" width=\"520\" height=\"520\">You shouldn't be reading this unless something is dreadfully amiss.</iframe> "; } else { echo "Oops! You must be logged in to view this page! If you have an account please log in on the 'Login' page. If you don't have a free account yet, please <b>register</b> on the Login page. You will be sent a confirmation email, and then when you log in, The webapp will appear right here on this page. Thank you! "; }; </exec>
I apologize that the iframe line is so long. I got some garbage when I tried formatting my code prettier. Running in one long line is what seems to work the best.
When a logged-in viewer views this page, the inline frame is open and through it the visitor sees the web-application, which is actually stored elsewhere as it requires more code than fits nicely on a wordpress page.
Now I need to find a way to prevent anybody from just reading the page source where the true location is openly stated, and just going to the page where the application really sits. I bet there’s a way; probably it will show up soon.
I think it’s weird that ordinary users have access to Site Admin capabilities (at least within the Tiga theme), but your post above inspired me to fix that. Here’s my suggested change: in wp-includes\general-template.php, I modified the else clause in wp_register so that it returns an empty link if the user is logged in, but is not the site admin. If this is not a good thing to do, I hope you’ll tell me why.
admin of Tax Policy Blog @ OptimalPortfolio.net
----------------- a slightly modified wp_register() ------------------ function wp_register( $before = ' <li>', $after = '</li> ' ) { if ( ! is_user_logged_in() ) { # no one is logged in if ( get_option('users_can_register') ) $link = $before . '<a href="' . get_option('siteurl') . '/wp-login.php?action=register">' . __('Register') . '</a>' . $after; else $link = ''; } else { # someone is logged in if ( current_user_can('level_10') ) $link = $before . '<a href="' . get_option('siteurl') . '/wp-admin/">' . __('Site Admin') . '</a>' . $after; else $link = ''; } echo apply_filters('register', $link); }
I,
I’m doing my first wordpress plugin and I have the same problem
Display Info if admin, otherwise hide
but for the Administration Panel.
In fact, I would like to hide Advanced Options on Edit Form.
Actually, I use css for hide the options and it’s work perfectly… but for everyone !`
function ds_hide_post_form () { ?>
<style>
#categorydiv, #tagsdiv, #postexcerpt, #trackbacksdiv, #postcustom, #ajax-response, #commentstatusdiv, #passworddiv, #authordiv, #previewview, .inside, .side-info {
display: none;
}
</style>
<?php }
add_action( ‘admin_head’, ‘ds_hide_post_form’ );
?>`I try to use $user_ID, $user_identity, $current_user but this variables are always empty and I don’t understand why !
Do you have a idea ?
ThanksI forgot to tell you that when I try to use a function like “get_currentuserinfo ();” always get this kind of error:
Fatal error: Call to undefined function get_currentuserinfo() in /home/…
Can you help me please ?
Tanks
I discovered my mistake!
I do not put “get_currentuserinfo ();” inside my function …
A beginner error I suppose!For some of you, this plugin might help:
AlexSherby,
I am seeking to do the same thing. Did you find something that worked? If so, would you share it?
Thanks,
EdAlexSherby,
I have yet to determine how to make this a plugin (I played with it for a few minutes, but the raw code won’t work as plug-in), however I have found a way to hide the areas I wanted gone…Here is the code:
<? // If user is less than admin, hides certain areas in Advanced Options get_currentuserinfo() ; global $user_level; if ($user_level > 9) {$hidefromuser=""; } else {$hidefromuser="display: none";} ?> <style> #pagecustomdiv, #pagetemplatediv, #postcustom, #trackbacksdiv, #revisionsdiv {<?php echo $hidefromuser; ?>;} </style>
I have placed this at the top of the following pages, and it simply hides the items I wanted hidden:
wp-admin\edit-form-advanced.php
wp-admin\edit-page-form.phpAs you are aware of, adding other sections to this is as simple as looking at the source code of the page and post admin sections for the DIV labels.
Hope that helps!
Ed
- The topic ‘Display Info if admin, otherwise hide’ is closed to new replies.