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 } ?>

14 Responses

Oct 15 2008 00:19

Very useful! It’s always boring to be forced to go to wp dashboard and retrieve the incrimined post…With that hack I can be more productive.

You’re doing a great job, both here and @ Cats Who Code (And I love your cats mascot, hehehe)

Oct 15 2008 00:23

Thanks for your kind words! Glad to see that you’re a reader of both WPRecipes and CatsWhoCode :)

Oct 18 2008 17:08

I didn’t know about the current_user_can() function, so thanks.
But wouldn’t a much better way be to use:

Which will only display for users with sufficient privileges.

Oct 18 2008 17:09

^^Code got removed:

edit_post_link(‘Edit this entry’,”,’.');

Oct 18 2008 17:21

Thank you for your code, Dan! I didn’t knew this function.
Sorry for the code which was automatically removed…A way to fix that should be a good idea for a new recipe! ;)

Oct 21 2008 23:24

Where exactly do you place the code? Index.php Single.php I’m not quite sure where… Thanks.

Oct 21 2008 23:28

@PublicRecordsGuy: Sorry, I forgot to tell it in the post! You can place this code on single.php.

Oct 22 2008 08:02

Thanks for the wonderful trick. this a very fast way to go to edit mode to modify the post.

Oct 31 2008 19:43

what if your theme doesn’t have single.php?

Oct 31 2008 19:47

A theme without a single.php file? That must be very rare! Anyways, you can use this code on your index.php file. Paste it within the loop.

Nov 02 2008 13:48

nice brother, cool info
i like this blog

Feb 04 2009 18:39

in wp 2.7 not run.

This code need to update

sorry for my little english.

Bye from Spain.

Trackbacks:

Leave a Comment

* Name, Email, Comment are Required