WordPress at a glance

get_edit_post_link() WP 2.3.0

Retrieves the link (URL) for edit post in admin-panel.

Can be used within the WordPress loop or outside of it. Can be used with any post types: pages, posts, attachments, custom post type and revisions.

Is the basis for: edit_post_link()
Works based on: current_user_can()
✈ 1 time = 0.000344s = fast | 50000 times = 0.95s = very fast PHP 7.0.8, WP 4.6.1
Hooks from function:
Return

String/null. The edit post link for the given post. null if the post type is invalid or does not allow an editing UI.

Usage

$edit_link = get_edit_post_link( $id, $context );
$id(int/WP_Post)
Post ID or post object, the edit link of which we need to get.
Default: is the global $post
$context(string)
Where we will use the link. display means that ampersand (&) will be converted to &. Set '' to not change the ampersand.
Default: 'display'

Examples

#1 Example of how get_edit_post_link() works

echo get_edit_post_link( 1 );

// return: http://site.ru/wp-admin/post.php?post=1&action=edit

#2 Display ready link

echo '<a href="' . get_edit_post_link(1) . '">Edit</a>';

#3 Print the link only if the user has sufficient capability for edit the post

if( current_user_can( 'edit_posts' ) ) {
	echo '<a href="'. get_edit_post_link(1) .'">Edit</a>';
}

Code of get_edit_post_link: wp-includes/link-template.php VER 4.9.8

<?php
function get_edit_post_link( $id = 0, $context = 'display' ) {
	if ( ! $post = get_post( $id ) )
		return;

	if ( 'revision' === $post->post_type )
		$action = '';
	elseif ( 'display' == $context )
		$action = '&amp;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 );
}

Related Functions

From tag: edit_

More from tag: Link (URL)

More from category: Posts, pages...

More from Template Tags: Links (URL)

No comments
    Hello, !     Log In . Register