edit_post_link( string $text = null, string $before = , string $after = , int|WP_Post $post, string $css_class = ‘post-edit-link’ )

Displays the edit post link for post.

Parameters

$textstringoptional
Anchor text. If null, default is ‘Edit This’.

Default:null

$beforestringoptional
Display before edit link.

Default:''

$afterstringoptional
Display after edit link.

Default:''

$postint|WP_Postoptional
Post ID or post object. Default is the global $post.
$css_classstringoptional
Add custom class to link. Default 'post-edit-link'.

Default:'post-edit-link'

More Information

Displays a link to edit the current post, if a user is logged in and allowed to edit the post. Can be used within The Loop or outside of it. If outside the loop, you’ll need to pass the post ID. Can be used with pages, posts, attachments, and revisions.

Use get_edit_post_link to retrieve the url.

Source

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;
}

Hooks

apply_filters( ‘edit_post_link’, string $link, int $post_id, string $text )

Filters the post edit link anchor tag.

Changelog

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

User Contributed Notes

You must log in before being able to contribute a note or feedback.