The edit_post_link() WordPress PHP function displays the edit link for a post.
Usage
Here’s how you can use the edit_post_link() function:
edit_post_link('Edit this post', '<p>', '</p>', 123, 'custom-edit-link');
In this example, ‘Edit this post’ is the anchor text for the link, <p>
and </p>
are displayed before and after the link respectively, 123
is the post ID, and ‘custom-edit-link’ is the CSS class added to the link.
Parameters
$text
(string) (Optional): This is the anchor text. If null, the default is ‘Edit This’. Default: null$before
(string) (Optional): This is displayed before the edit link. Default: ”$after
(string) (Optional): This is displayed after the edit link. Default: ”$post
(int|WP_Post) (Optional): This is the Post ID or post object. Default is the global $post.$css_class
(string) (Optional): This allows you to add a custom class to the link. Default ‘post-edit-link’. Default: ‘post-edit-link’
More information
See WordPress Developer Resources: edit_post_link
Examples
Default Usage
In this example, the function will display the edit post link using default parameters.
edit_post_link();
Display “Edit” in Paragraph Tag
This will display the edit post link, with link text “edit”, in a paragraph (<p>
) tag.
edit_post_link( __( 'edit', 'textdomain' ), '<p>', '</p>' );
Display Edit Link with Custom CSS Class
This example shows how to add a custom CSS class to the edit link.
edit_post_link( __( 'Edit', 'textdomain' ), '<p>', '</p>', null, 'custom-edit-link' );
Display Edit Link for Specific Post
Here, the function will display the edit link for the post with ID 123.
edit_post_link( __( 'Edit', 'textdomain' ), '<p>', '</p>', 123 );
Display Edit Link with Custom Text
In this case, the function will display “Edit this post” as the text for the edit link.
edit_post_link( 'Edit this post', '<p>', '</p>' );