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:1452
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
- Scaling
- Constant
- Instructions
- 8–65
- Plugin surface
- 1 hook
- Called by
- 24
Reaches the database via get_post().
No loop in the body: the same number of instructions runs whatever you pass in.
Executed per call on PHP 8.5, depending on the branch taken. The body compiles to 99.
Third-party callbacks on 'get_edit_post_link' run inside this call, and their cost is not bounded by anything here.
24 places in core call this, so the cost is paid more often than your own code shows.
What it touches
- querycontent query
get_post()called directly - hookthird-party callbacks
apply_filters()called directly - optionoption read or write
get_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.
| When | Instructions | Calls it makes |
|---|---|---|
| always | 8 | get_post() |
| always | 20–22 | get_post(), get_post_type_object() |
!current_user_can() | 27–29 | get_post(), get_post_type_object(), current_user_can() |
current_user_can() && !$post_type_object | 47–49 | get_post(), get_post_type_object(), current_user_can(), apply_filters() |
current_user_can() | 57–60 | get_post(), get_post_type_object(), current_user_can(), sprintf(), admin_url(), apply_filters() |
current_user_can() | 60–65 | get_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:
Uses · 6
- get_post()Retrieves post data given a post ID or post object.
- get_post_type_object()Retrieves a post type object by name.
- current_user_can()Returns whether the current user has the specified capability.
- get_stylesheet()Retrieves name of the current stylesheet.
- admin_url()Retrieves the URL to the admin area for the current site.
- apply_filters()Calls the callback functions that have been added to a filter hook.
Used by · 24
- WP_Comments_List_Table::column_response()Outputs the response column.
- WP_Media_List_Table::_get_row_actions()Gets the row actions for a media item.
- WP_Media_List_Table::column_parent()Handles the parent column output.
- WP_Media_List_Table::column_title()Handles the title column output.
- WP_Posts_List_Table::column_title()Handles the title column output.
- WP_Posts_List_Table::handle_row_actions()Generates and displays row action links.
- _admin_notice_post_locked()Outputs the HTML for the notice to say that someone else is editing or has taken over editing of this post.
- attachment_submitbox_metadata()Displays non-editable attachment metadata in the publish meta box.
- do_block_editor_incompatible_meta_box()Renders a "fake" meta box with an information message, shown on the block editor, when an incompatible meta box is found.
- edit_post_link()Displays the edit post link for post.
- post_submit_meta_box()Displays post submit form fields.
- redirect_post()Redirects to previous page.
Show all 24
- twentyseventeen_entry_footer()Prints HTML with meta information for the categories, tags and comments.
- twentytwenty_edit_post_link()Filters the edit post link to add an icon and use the post meta structure.
- wp_admin_bar_edit_menu()Provides an edit link for posts and terms.
- wp_dashboard_recent_drafts()Show recent drafts of the user on the dashboard.
- wp_dashboard_recent_posts()Generates Publishing Soon and Recently Published sections.
- wp_get_post_revisions_url()Returns the url for viewing and potentially restoring revisions of a given post.
- wp_notify_postauthor()Notifies an author (and/or others) of a comment/trackback/pingback on a post.
- wp_post_revision_title()Retrieves formatted date timestamp of a revision (linked to that revisions's page).
- wp_post_revision_title_expanded()Retrieves formatted date timestamp of a revision (linked to that revisions's page).
- wp_prepare_attachment_for_js()Prepares an attachment post object for JS, where it is expected to be JSON-encoded and fit into an Attachment model.
- wp_render_empty_block_template_warning()Renders a warning screen for empty block templates.
- wp_send_note_notification()Sends a single note mention notification email.
Source code
function get_edit_post_link( $post = 0, $context = 'display' ) { $post = get_post( $post ); if ( ! $post ) { return null; } 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 null; } if ( ! current_user_can( 'edit_post', $post->ID ) ) { return null; } $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.
Signature, return type and hooks compared across 5 parsed releases.
Adds custom links for wp_template_part and wp_template post types.from the docblock
About this page
- Parsed data
- Generated from the wordpress-develop 7.1.0 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.