WordPress at a glance

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.

Notes:

  • Since 4.4.0 The $class argument was added.
Works based on: get_edit_post_link()
✈ 1 time = 0.003579s = very slow | 50000 times = 7.28s = fast PHP 7.1.11, WP 4.9.5
Hooks from function:
Return

Nothing (null).

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(); ?>

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

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

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

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

	if ( ! $url = get_edit_post_link( $post->ID ) ) {
		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;
}

Related Functions

From tag: edit_

More from category: Posts, pages...

More from Template Tags: Links (URL)

No comments
    Hello, !     Log In . Register