WordPress get_edit_post_link() Function

Beginner
5 min read
Updated: Nov 2026
3 examples
Returns a URL

Definition

In WordPress, get_edit_post_link() returns the URL of the edit screen for a post or page. It gives you only the address, so you can build your own link or button.

If the post does not exist or the current user cannot edit it, the function returns null. This is why you should check the result before you print it.

Syntax

Get the URL, check it, then print it.

WordPress
<?php $url = get_edit_post_link(); ?>
CallWhat it does
get_edit_post_link()Edit URL of the current post.
get_edit_post_link( 15 )Edit URL of post 15.
get_edit_post_link( 15, 'raw' )Edit URL with a plain & (good for redirects).

It returns the URL. To print a ready-made link, use edit_post_link().

Full declaration
WordPress
get_edit_post_link( int|WP_Post $post = 0, string $context = 'display' ): string|null

Parameters

ParameterDescription
$postint|WP_Post. The post ID or object. Default is 0, which means the current post.
$contextstring. display (default) encodes & as &amp; for HTML. Any other value, such as raw, keeps a plain &.

Return Value

Return ValueDescription
stringThe edit URL.
nullIf the post does not exist or the user cannot edit it.

WordPress Examples

Three short examples. Each one shows the code and what it prints or does.

Example 1 — Print the edit URL

Get the URL of the current post and print it safely.

WordPress
<?php
$url = get_edit_post_link();
if ( $url ) {
  echo esc_url( $url );
}

How It Works

  1. get_edit_post_link() returns the edit address.
  2. if ( $url ) skips the output for visitors.
  3. esc_url() keeps the output safe.

Example 2 — Use the raw context

Use raw when the URL is not printed in HTML, for example in a redirect.

WordPress
<?php
$url = get_edit_post_link( 15, 'raw' );
if ( $url ) {
  wp_safe_redirect( $url );
  exit;
}

How It Works

  1. raw keeps a normal & in the URL.
  2. That is the right form for a redirect header.
  3. The default display form is only for printing inside HTML.

Example 3 — Build a custom edit button

Make your own link with your own HTML.

WordPress
<?php
$url = get_edit_post_link( 15 );
if ( $url ) {
  echo '<a class="btn" href="' . esc_url( $url ) . '">Edit this page</a>';
}

How It Works

  1. The function only gives the URL, so we write the a tag.
  2. We test the result because it can be null.
  3. Only users who can edit page 15 see the button.

Frequently Asked Questions

It returns the admin URL where a post or page can be edited.
edit_post_link() prints a full link. get_edit_post_link() returns only the URL so you can make your own HTML.
The post may not exist, or the current user cannot edit it. Visitors always get null.
With display, the ampersand is encoded for HTML. With raw, it stays a plain ampersand, which is best for redirects.
Yes, when you print the URL inside an HTML attribute. It is a good habit for every URL.

Next: get_home_url()

Return the home URL of a site, with an optional path. Can pick a site on multisite.

get_home_url() 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.

12 people found this page helpful