edit_post_link()WP 1.0.0

Displays the edit post link for a post, if the user is allowed to change the post.

Must be used inside the Loop of WordPress.

1 time — 0.003579 sec (very slow) | 50000 times — 7.28 sec (fast) | PHP 7.1.11, WP 4.9.5
Hooks from the function

Return

null. Ничего (null).

Usage

edit_post_link( $text, $before, $after, $post, $css_class );
$text(string)
Anchor text. If null. Default null.
Default: 'Edit This'
$before(string)
Display before edit link.
Default: ''
$after(string)
Display after edit link.
Default: ''
$post(int|WP_Post)
Post ID or post object.
Default: global $post
$css_class(string)
Add custom class to link.
Default: 'post-edit-link'

Examples

0

#1 Display the link to edit the post

The edit post link will be displayed only for users who can edit the post (posts).

<?php edit_post_link(); ?>

Output:

<a class="post-edit-link" href="https://example.com/wp-admin/post.php?post=19&action=edit">Edit</a>
0

#2 Change the text of the link and enclose it in html tag <p>

<?php edit_post_link('edit', '<p>', '</p>'); ?>

Changelog

Since 1.0.0 Introduced.
Since 4.4.0 The $css_class argument was added.

edit_post_link() code WP 6.3

function edit_post_link( $text = null, $before = '', $after = '', $post = 0, $css_class = 'post-edit-link' ) {
	$post = get_post( $post );

	if ( ! $post ) {
		return;
	}

	$url = get_edit_post_link( $post->ID );

	if ( ! $url ) {
		return;
	}

	if ( null === $text ) {
		$text = __( 'Edit This' );
	}

	$link = '<a class="' . esc_attr( $css_class ) . '" href="' . esc_url( $url ) . '">' . $text . '</a>';

	/**
	 * Filters the post edit link anchor tag.
	 *
	 * @since 2.3.0
	 *
	 * @param string $link    Anchor tag for the edit link.
	 * @param int    $post_id Post ID.
	 * @param string $text    Anchor text.
	 */
	echo $before . apply_filters( 'edit_post_link', $link, $post->ID, $text ) . $after;
}