Home WordPress Links Functions get_edit_post_link() WordPress get_edit_post_link() Function 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.
<?php $url = get_edit_post_link(); ?> Call What 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 get_edit_post_link( int|WP_Post $post = 0, string $context = 'display' ): string|null Parameters Parameter Description $postint|WP_Post. The post ID or object. Default is 0, which means the current post. $contextstring. display (default) encodes & as & for HTML. Any other value, such as raw, keeps a plain &.
Return Value Return Value Description string The edit URL. null If 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.
1. Print the edit URL 2. Use the raw context 3. Build a custom edit button Example 1 — Print the edit URL Get the URL of the current post and print it safely.
<?php
$url = get_edit_post_link();
if ( $url ) {
echo esc_url( $url );
} https://example.com/wp-admin/post.php?post=42&action=edit How It Works get_edit_post_link() returns the edit address.if ( $url ) skips the output for visitors.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.
<?php
$url = get_edit_post_link( 15, 'raw' );
if ( $url ) {
wp_safe_redirect( $url );
exit;
} Location: https://example.com/wp-admin/post.php?post=15&action=edit How It Works raw keeps a normal & in the URL.That is the right form for a redirect header. 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.
<?php
$url = get_edit_post_link( 15 );
if ( $url ) {
echo '<a class="btn" href="' . esc_url( $url ) . '">Edit this page</a>';
} How It Works The function only gives the URL, so we write the a tag. We test the result because it can be null. Only users who can edit page 15 see the button. Frequently Asked Questions What does get_edit_post_link() do? It returns the admin URL where a post or page can be edited.
How is it different from edit_post_link()? edit_post_link() prints a full link. get_edit_post_link() returns only the URL so you can make your own HTML.
Why did I get null? The post may not exist, or the current user cannot edit it. Visitors always get null.
What does the context argument do? With display, the ampersand is encoded for HTML. With raw, it stays a plain ampersand, which is best for redirects.
Do I still need esc_url()? 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 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.
12 people found this page helpful
Helpful Share Copy link Suggestion