Skip to content

Instantly share code, notes, and snippets.

@JiveDig
Last active September 16, 2020 19:13
Show Gist options
  • Save JiveDig/95dec483b904552876c6 to your computer and use it in GitHub Desktop.
Save JiveDig/95dec483b904552876c6 to your computer and use it in GitHub Desktop.
Add a custom 'Edit Post' link on posts the logged in user authored.Uses tsm_is_current_users_post( $post_id ) helper function here https://gist.github.com/JiveDig/c2d59c4efd996c41dd23. Adds ?post_id=123 query arg to the end of a page URL
<?php
/**
* Add a custom 'Edit Post' link on posts the logged in user authored
* Links to a separate page with ?post_id=123 query string
* Uses tsm_is_current_users_post( $post_id ) helper function
*
* @author Mike Hemberger
* @link http://thestizmedia.com/front-end-post-editing-with-caldera-forms/
* @return bool/string false or edit post link
*/
add_filter( 'the_content', 'tsm_do_edit_post_link' );
function tsm_do_edit_post_link( $content ) {
$post_types = array('post','page'); // Change to your post types
// Bail if not our post types
if ( ! in_array( get_post_type(), $post_types ) ) {
return $content;
}
// Bail if not the current users post
if ( ! tsm_is_current_users_post( get_the_ID() ) ) {
return $content;
}
// Add link to /edit-post/ form with current page ID as the 'post_id' query arg value
$edit_link = '<p><a href="' . esc_url( add_query_arg( 'post_id', get_the_ID(), home_url('/edit-post/') ) ) . '">' . __( '[Edit Post]', 'caldera-forms' ) . '</a></p>';
// Return the post content with our edit post link before it
return $edit_link . $content;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment