get_edit_post_link() WordPress Function

The get_edit_post_link() function is used to retrieve the URL for the edit post page of a given post. This is useful for creating links that allow users to edit their posts.

get_edit_post_link( int|WP_Post $id, string $context = 'display' ) #

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, and revisions.


Top ↑

Parameters

$id

(int|WP_Post)(Optional) Post ID or post object. Default is the global $post.

$context

(string)(Optional) How to output the '&' character. Default '&'.

Default value: 'display'


Top ↑

Return

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


Top ↑

Source

File: wp-includes/link-template.php

function get_edit_post_link( $id = 0, $context = 'display' ) {
	$post = get_post( $id );
	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;
	}

	if ( $post_type_object->_edit_link ) {
		$link = admin_url( sprintf( $post_type_object->_edit_link . $action, $post->ID ) );
	} else {
		$link = '';
	}

	/**
	 * 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 );
}


Top ↑

Changelog

Changelog
VersionDescription
2.3.0Introduced.

The content displayed on this page has been created in part by processing WordPress source code files which are made available under the GPLv2 (or a later version) license by theĀ Free Software Foundation. In addition to this, the content includes user-written examples and information. All material is subject to review and curation by the WPPaste.com community.

Show More
Show More