The get_edit_post_link() WordPress PHP function retrieves the edit post link for a post.
Usage
get_edit_post_link( $post, $context );
Example:
Input:
$post_id = 42; echo get_edit_post_link( $post_id );
Output:
https://example.com/wp-admin/post.php?post=42&action=edit
Parameters
$post
(int|WP_Post) (optional): Post ID or post object. Default is the global$post
.$context
(string) (optional): How to output the ‘&’ character. Default is ‘&’. Default value: ‘display’.
More information
See WordPress Developer Resources: get_edit_post_link
Examples
Display edit post link in a theme
Explanation: Add the edit post link to your theme’s single.php
or content-single.php
file.
Code:
// Add this code within the loop if ( current_user_can( 'edit_posts' ) ) { echo '<a href="' . get_edit_post_link() . '">Edit Post</a>'; }
Display edit post link with custom text
Explanation: Display the edit post link with custom text instead of the default “Edit Post”.
Code:
if ( current_user_can( 'edit_posts' ) ) { echo '<a href="' . get_edit_post_link() . '">Edit This Article</a>'; }
Display edit post link with a custom CSS class
Explanation: Add a custom CSS class to style the edit post link.
Code:
if ( current_user_can( 'edit_posts' ) ) { echo '<a class="custom-edit-link" href="' . get_edit_post_link() . '">Edit Post</a>'; }
Hide the edit post link from non-administrators
Explanation: Use a filter to remove the edit post link for users without the ‘administrator’ role.
Code:
function wpdocs_remove_get_edit_post_link( $link ) { if ( current_user_can( 'administrator' ) ) { return $link; } return null; } add_filter( 'get_edit_post_link', 'wpdocs_remove_get_edit_post_link' );
Display edit post link for a specific post ID
Explanation: Display the edit post link for a specific post with a given ID.
Code:
$post_id = 42; if ( current_user_can( 'edit_posts' ) ) { echo '<a href="' . get_edit_post_link( $post_id ) . '">Edit Post</a>'; }