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!

Latest Posts



Fill in the form below to send us a message. Your details will not be used for promotional purposes or passed on to any third parties.


By continuing to use the site, you agree to the use of cookies. more information

The cookie settings on this website are set to "allow cookies" to give you the best browsing experience possible. If you continue to use this website without changing your cookie settings or you click "Accept" below then you are consenting to this.