Home WordPress Links Functions edit_post_link() WordPress edit_post_link() Function Definition In WordPress, edit_post_link() prints an Edit link for a post or page. It takes the user straight to the editor in the admin area.
It is the link you see under posts when you are logged in as an admin. The function checks the user for you, so visitors see nothing. Most themes call it in the Loop, next to the post title or after the content.
Syntax Call it inside the Loop, or pass a post ID.
<?php edit_post_link( 'Edit this post' ); ?> Call What it does edit_post_link( 'Edit this post' )Print an edit link for the current post. edit_post_link( 'Edit', '<p>', '</p>' )Wrap the link in a paragraph. edit_post_link( 'Edit', '', '', 15, 'btn' )Link to post 15 and use your own CSS class.
It prints the link. If you only need the URL, use get_edit_post_link().
Full declaration edit_post_link( string $text = null, string $before = '', string $after = '', int|WP_Post $post = 0, string $class = 'post-edit-link' ): void Parameters Parameter Description $textstring. The link text. If empty, WordPress uses its default (Edit This, with the post title for screen readers). $beforestring. HTML printed before the link. $afterstring. HTML printed after the link. $postint|WP_Post. Post ID or object. Default is 0, which means the current post. $classstring. The CSS class of the link. Default is post-edit-link.
Return Value Return Value Description void Nothing is returned. The link is printed only if the user can edit the post.
WordPress Examples Three short examples. Each one shows the code and what it prints or does.
1. Add an edit link in the Loop 2. Wrap the link in a paragraph 3. Pick another post and add a CSS class Example 1 — Add an edit link in the Loop The most common use. Put it after the post title or content.
<?php
while ( have_posts() ) {
the_post();
the_title( '<h2>', '</h2>' );
edit_post_link( 'Edit this post' );
} <h2>Hello world</h2>
<a class="post-edit-link" href="https://example.com/wp-admin/post.php?post=42&action=edit">Edit this post</a> How It Works The Loop goes through each post. edit_post_link() works on the current post.An admin sees the link. A visitor sees only the title. Example 2 — Wrap the link in a paragraph Use $before and $after so you do not print empty HTML for visitors.
<?php
edit_post_link( 'Edit', '<p class="edit-link">', '</p>' ); <p class="edit-link"><a class="post-edit-link" href="https://example.com/wp-admin/post.php?post=42&action=edit">Edit</a></p> How It Works The link text is Edit. The paragraph is printed only when the link is printed. This keeps your page clean for visitors. Example 3 — Pick another post and add a CSS class Link to a specific page and style it like a button.
<?php
// Edit link for the page with ID 15.
edit_post_link( 'Edit page', '', '', 15, 'btn btn-edit' ); <a class="btn btn-edit" href="https://example.com/wp-admin/post.php?post=15&action=edit">Edit page</a> How It Works 15 is the ID of the page to edit.The fifth value replaces the default CSS class. Now you can style .btn-edit in your CSS. Frequently Asked Questions What does edit_post_link() do? It prints a link to the editor for a post or page. It only appears for users who are allowed to edit it.
Why can I not see the edit link? You are probably logged out, or your user cannot edit that post. The function prints nothing in that case.
What is the difference between edit_post_link() and get_edit_post_link()? edit_post_link() prints a full link tag. get_edit_post_link() returns only the URL so you can build your own HTML.
Can I use it outside the Loop? Yes. Pass a post ID or a post object as the fourth argument.
How do I style the link? The default class is post-edit-link. You can also pass your own class as the fifth argument.
About the author Developer, cloud engineer, and technical writer
I write practical tutorials so students and working developers can learn by doing—from databases and APIs to deployment on AWS.
11 people found this page helpful
Helpful Share Copy link Suggestion