WordPress edit_post_link() Function

Beginner
6 min read
Updated: Nov 2026
3 examples
Prints a link

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.

WordPress
<?php edit_post_link( 'Edit this post' ); ?>
CallWhat 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
WordPress
edit_post_link( string $text = null, string $before = '', string $after = '', int|WP_Post $post = 0, string $class = 'post-edit-link' ): void

Parameters

ParameterDescription
$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 ValueDescription
voidNothing 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.

Example 1 — Add an edit link in the Loop

The most common use. Put it after the post title or content.

WordPress
<?php
while ( have_posts() ) {
  the_post();
  the_title( '<h2>', '</h2>' );
  edit_post_link( 'Edit this post' );
}

How It Works

  1. The Loop goes through each post.
  2. edit_post_link() works on the current post.
  3. 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.

WordPress
<?php
edit_post_link( 'Edit', '<p class="edit-link">', '</p>' );

How It Works

  1. The link text is Edit.
  2. The paragraph is printed only when the link is printed.
  3. 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.

WordPress
<?php
// Edit link for the page with ID 15.
edit_post_link( 'Edit page', '', '', 15, 'btn btn-edit' );

How It Works

  1. 15 is the ID of the page to edit.
  2. The fifth value replaces the default CSS class.
  3. Now you can style .btn-edit in your CSS.

Frequently Asked Questions

It prints a link to the editor for a post or page. It only appears for users who are allowed to edit it.
You are probably logged out, or your user cannot edit that post. The function prints nothing in that case.
edit_post_link() prints a full link tag. get_edit_post_link() returns only the URL so you can build your own HTML.
Yes. Pass a post ID or a post object as the fourth argument.
The default class is post-edit-link. You can also pass your own class as the fifth argument.

Next: edit_tag_link()

Print an edit link for a tag if the user can edit it.

edit_tag_link() tutorial →

About the author

Mari Selvan M P
Mari Selvan M P 🔗

Developer, cloud engineer, and technical writer

  • Experience 12 years building web and cloud systems
  • Focus Full Stack Development, AWS, and Developer Education

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