How to: Add a “edit” link on each post

Sometimes, you read one of your blog post and you see a typo or an error. Sure, you got to correct it. But who said you must open your WordPress dashboard, go to “Manage” and then edit the post? In this recipe, I’m going to show you how to add a button to allow the admin to directly edit the post.

To achieve this recipe, we need the current_user_can() WordPress function. This function checks the given parameter, which is the level of the current user, and returns true if the level of the current user is superior or equals to the given parameter.

We don't want that an author or contributor can edit a published post. With current_user_can('level_10'), we're making sure that only administrator(s) can edit the post.

Then, we're using WordPress basic functions (bloginfo() and the_ID()) to build a direct link to the dashboard page where the post will be editable.

<?php
if (current_user_can('level_10')){ ?>
    <a href="<?php bloginfo('wpurl');?>/wp-admin/edit.php?p=<?php the_ID(); ?>">Edit Post</a>
<?php } ?>

Leave a Comment

* Name, Email, Comment are Required

WP Theme generator

Sponsored Likebox