Code Snippet

Home » Code Snippets » WordPress » Admin Panel Link Only For Admins

Admin Panel Link Only For Admins

<?php if (current_user_can("manage_options")) : ?>
       <a href="<?php echo bloginfo("siteurl") ?>/wp-admin/">Admin</a>
<?php endif; ?>

If a user is logged in and they are an Admin of the site (not just an subscriber or author), then display a link to get them to the WordPress Admin area. Otherwise, display nothing.

More specific than the regular log in / log out function, which will show for everyone:

<?php wp_loginout(); ?>

Subscribe to The Thread

  1. really handy piece of code.
    thanks Chris.

  2. I think the following (within the loop) does the same thing and lets you go directly to the post or page you want to edit:

    <?php edit_post_link(‘Edit’, ”, ”); ?>

    • Really handy! So, why not combine both scripts and have “Login” and “Edit” for Admins only?

      For example, inside the loop:

      
      // Begin WordPress loop
      <?php
      get_header();
      if (have_posts()) : while (have_posts()) : the_post();
      ?>
      
      // Admin only code
      <?php if (current_user_can("manage_options")) : ?>
      <a href="<?php echo bloginfo("siteurl") ?>/wp-admin/">Admin</a>
      <?php  edit_post_link(‘Edit’, ”, ”);  ?>
      <?php endif; ?>
      
      // code here, get contents
      ...
      ...
      
      // End WordPress loop
      <?php endwhile; else: ?>
      Sorry, no pages matched your criteria.
      <?php endif; get_footer(); ?>
      
  3. Really a handy code but some more descriptions would have really made me appreciate you more

Speak, my friend

At this moment, you have an awesome opportunity* to be the person your mother always wanted you to be: kind, helpful, and smart. Do that, and we'll give you a big ol' gold star for the day (literally).

Posting tips:
  • You can use basic HTML
  • When posting code, please turn all
    < characters into &lt;
  • If the code is multi-line, use
    <pre><code></code></pre>
Thank you,
~ The Management ~