wppaste
WordPress

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

Since
2.3.0, 6.3.0
Source
wp-includes/link-template.php:1453
Retrieves the edit post link for post.

Description

Can be used within the WordPress loop or outside of it. Can be used with pages, posts, attachments, revisions, global styles, templates, and template parts.

Compatibility

WordPress
since 6.3.0
PHP
7.4–8.6-dev
  • 6.7.7
  • 6.8.8
  • 6.9.7
  • 7.0.4
  • 7.1.0

Present in every tracked release (6.7.7 to 7.1.0), and compiles on PHP 7.4 through 8.6-dev.

Parameters

$postint|WP_Postoptional
Post ID or post object. Default is the global $post.Default: 0
$contextstringoptional
How to output the '&' character. Default '&'.Default: 'display'

Return value

string|null
The edit post link for the given post. Null if the post type does not exist or does not allow an editing UI.

Performance profile

How much work a call to get_edit_post_link() does, and what it touches: the algorithmic scaling, the Zend instruction count per call across PHP versions, the hooks it hands control to, and the core code that calls it. Measured from the compiled opcodes, not a stopwatch, so every number is identical on any machine running the same PHP version, and every function in core is ranked by cost.

Cost class
Heavy

Reaches the database via get_post().

Scaling
Constant

No loop in the body: the same number of instructions runs whatever you pass in.

Instructions
8–65

Executed per call on PHP 8.5, depending on the branch taken. The body compiles to 99.

Plugin surface
1 hook

Third-party callbacks on 'get_edit_post_link' run inside this call, and their cost is not bounded by anything here.

Called by
21

21 places in core call this, so the cost is paid more often than your own code shows.

What it touches

  • querycontent queryget_post()called directly
  • hookthird-party callbacksapply_filters()called directly
  • optionoption read or writeget_option()one call below get_edit_post_link()

Further down the call graph this can also reach cache, serialize and transient. Those are the worst case, several calls deep and usually down an error path, not what a normal call pays.

What one call costs · 6 distinct outcomes

One number would be a lie: the work depends on which branch runs. These are every distinct cost get_edit_post_link() can have, taken from its control-flow graph on PHP 8.5.

WhenInstructionsCalls it makes
always8get_post()
always20–22get_post(), get_post_type_object()
!current_user_can()27–29get_post(), get_post_type_object(), current_user_can()
current_user_can() && !$post_type_object47–49get_post(), get_post_type_object(), current_user_can(), apply_filters()
current_user_can()57–60get_post(), get_post_type_object(), current_user_can(), sprintf(), admin_url(), apply_filters()
current_user_can()60–65get_post(), get_post_type_object(), current_user_can(), get_stylesheet(), urlencode(), sprintf(), admin_url(), apply_filters()

Across PHP versions

Compiles the same on PHP 7.4, 8.1, 8.2, 8.3, 8.4, 8.5 and 8.6-dev: 99 instructions, 8–65 executed per call, 9 branches. The work does not change between versions.

An instruction is not a fixed amount of time, so a matching count is not necessarily the same speed; what it rules out is a difference in the work itself.

Hooks and filters fired · 1

One hook fires while get_edit_post_link() runs, in this order:

  1. apply_filters( get_edit_post_link )filterline 1499 (+46 into the body)

    Filters the post edit link.

Uses · 6

Used by · 21

Show all 21

Source code

function get_edit_post_link( $post = 0, $context = 'display' ) {	$post = get_post( $post ); 	if ( ! $post ) {		return;	} 	if ( 'revision' === $post->post_type ) {		$action = '';	} elseif ( 'display' === $context ) {		$action = '&action=edit';	} else {		$action = '&action=edit';	} 	$post_type_object = get_post_type_object( $post->post_type ); 	if ( ! $post_type_object ) {		return;	} 	if ( ! current_user_can( 'edit_post', $post->ID ) ) {		return;	} 	$link = ''; 	if ( 'wp_template' === $post->post_type || 'wp_template_part' === $post->post_type ) {		$slug = urlencode( get_stylesheet() . '//' . $post->post_name );		$link = admin_url( sprintf( $post_type_object->_edit_link, $post->post_type, $slug ) );	} elseif ( 'wp_navigation' === $post->post_type ) {		$link = admin_url( sprintf( $post_type_object->_edit_link, (string) $post->ID ) );	} elseif ( $post_type_object->_edit_link ) {		$link = admin_url( sprintf( $post_type_object->_edit_link . $action, $post->ID ) );	} 	/**	 * Filters the post edit link.	 *	 * @since 2.3.0	 *	 * @param string $link    The edit link.	 * @param int    $post_id Post ID.	 * @param string $context The link context. If set to 'display' then ampersands	 *                        are encoded.	 */	return apply_filters( 'get_edit_post_link', $link, $post->ID, $context );}

Changelog

Introduced in 2.3.0. Unchanged from 6.7.7 through 7.1.0.

  1. 6.7.7
  2. 6.8.8
  3. 6.9.7
  4. 7.0.4
  5. 7.1.0

Signature, return type and hooks compared across 5 parsed releases.

6.3.0
Adds custom link for wp_navigation post types.
Adds custom links for wp_template_part and wp_template post types.from the docblock
2.3.0
Introduced.from the docblock

About this page

Parsed data
Generated from the wordpress-develop 6.7.7 tag, from src/wp-includes/link-template.php, and regenerated for each WordPress release so it tracks the code rather than a snapshot of it.
Corrections
Something wrong on this page? Report it and it gets fixed in the next regeneration.