Add an edit post link to every single post and page on your WordPress site

In order for websites to be useful they should be easy fo their users or managers to update. Often when I’m building themes I will include an edit button in the template file using edit_post_link(). This makes me CRY a little bit (Constantly Repeating Yourself) as I would normally have the edit_post_link() function repeated across template files.

Today it occurred to me there is a better way of doing this, why not simply add a filter to ‘the_content’ and output the edit post link after the_content() is called in the templates? So this is what we have below, I’ve swapped out edit_post_link() for get_edit_post_link() which returns the url rather than echo-ing the entire hyperlink, and set a super high priority so it always appears after all the actual content and other filters have been applied: wpautop, do_shortcode etc…

function add_an_edit_link($content){
  $content .= '<p class="edit-this"><a href="' . get_edit_post_link() . '">Edit This</a></p>'; 
return $content; 
add_filter('the_content',  'add_an_edit_link', 1000);

I whacked that little snippet in my functions.php file and behold, edit links appear after every occurrence of ‘the_content()’ throughout the site. Nice and DRY!

Leave a Reply