WordPress at Your Fingertips

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

Usage

edit_post_link( $text, $before, $after, $id, $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: ''
$id(int|WP_Post)
Post ID or post object.
Default: global $post
$class(string)
Add custom class to link.
Default: 'post-edit-link'

Examples

#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>

#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 $class argument was added.

Code of edit_post_link() WP 5.8.1

function edit_post_link( $text = null, $before = '', $after = '', $id = 0, $class = 'post-edit-link' ) {
	$post = get_post( $id );
	if ( ! $post ) {
		return;
	}

	$url = get_edit_post_link( $post->ID );
	if ( ! $url ) {
		return;
	}

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

	$link = '<a class="' . esc_attr( $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;
}

edit_

Posts, pages...

Links (URL)

vladlu 100vlad.lu
Editors: Kama 101
No comments
    Log In