Grow your CSS skills. Land your dream job.

Last updated on:

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(); ?>

Comments

  1. really handy piece of code.
    thanks Chris.

  2. Jim
    Permalink to comment#

    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’, ”, ”); ?>

    • Permalink to comment#

      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

Leave a Comment

Posting Code

Markdown is supported in the comment area, so you can write inline code in backticks like `this` or multiline blocks of code in in triple backtick fences like ```this```. You don't need to escape code in backticks, Markdown does that for you.

Sadly, it's kind of broken. WordPress only accepts a subset of HTML in comments, which makes sense, because certainly some HTML can't be allowed, like <script> tags. But this stripping happens before the comment is processed by Markdown (via Jetpack). It seems to me that would be reversed, because after Markdown processes code in backticks, it's escaped, thus safe. If you think you can fix this issue, get in touch!

If you need to make sure the code (typically HTML) you post absolutely posts correctly, escape it and put it within <pre><code> tags.

Current ye@r *

*May or may not contain any actual "CSS" or "Tricks".