edit_post_link() WordPress Function

The edit_post_link() function allows you to add an "edit" link to a post or page in WordPress. This can be useful if you want to allow users to edit their own posts, or if you want to offer a way for people to suggest edits to your content. The edit_post_link() function takes two parameters: the post ID and the text to be displayed for the link. For example, to add an "edit" link to a post with an ID of 1234, you would use the following code: This would display a link with the text "Edit this post" that links to the editing screen for the post with an ID of 1234.

edit_post_link( string $text = null, string $before = '', string $after = '', int|WP_Post $id, string $class = 'post-edit-link' ) #

Displays the edit post link for post.


Parameters

$text

(string)(Optional) Anchor text. If null, default is 'Edit This'.

Default value: null

$before

(string)(Optional) Display before edit link.

Default value: ''

$after

(string)(Optional) Display after edit link.

Default value: ''

$id

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

$class

(string)(Optional) Add custom class to link.

Default value: 'post-edit-link'


Top ↑

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.


Top ↑

Source

File: wp-includes/link-template.php

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


Top ↑

Changelog

Changelog
VersionDescription
4.4.0The $class argument was added.
1.0.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