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
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)
Thanks for your kind words! Glad to see that you’re a reader of both WPRecipes and CatsWhoCode
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.
^^Code got removed:
edit_post_link(‘Edit this entry’,”,’.');
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!
Where exactly do you place the code? Index.php Single.php I’m not quite sure where… Thanks.
@PublicRecordsGuy: Sorry, I forgot to tell it in the post! You can place this code on single.php.
Thanks for the wonderful trick. this a very fast way to go to edit mode to modify the post.
what if your theme doesn’t have single.php?
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.
nice brother, cool info
i like this blog
in wp 2.7 not run.
This code need to update
sorry for my little english.
Bye from Spain.
Trackbacks: