get_edit_post_link

Retrieves the edit post link for post.

Description

(string|null) get_edit_post_link( (int) $id = 0, (string) $context = 'display' ); 

Can be used within the WordPress loop or outside of it. Can be used with pages, posts, attachments, and revisions.

Returns (string|null)

The edit post link for the given post. null if the post type is invalid or does not allow an editing UI.

Parameters (2)

0. $id — Optional. (int)
Post ID. Default is the ID of the global $post..
1. $context — Optional. (string) => 'display'
How to output the & character. Default &..

Usage

  1. if ( !function_exists( 'get_edit_post_link' ) ) { 
  2. require_once ABSPATH . WPINC . '/link-template.php'; 
  3.  
  4. // Optional. Post ID. Default is the ID of the global `$post`. 
  5. $id = -1; 
  6.  
  7. // Optional. How to output the '&' character. Default '&'. 
  8. $context = 'display'; 
  9.  
  10. // NOTICE! Understand what this does before running. 
  11. $result = get_edit_post_link($id, $context); 
  12.  

Defined (1)

The function is defined in the following location(s).

/wp-includes/link-template.php  
  1. function get_edit_post_link( $id = 0, $context = 'display' ) { 
  2. if ( ! $post = get_post( $id ) ) 
  3. return; 
  4.  
  5. if ( 'revision' === $post->post_type ) 
  6. $action = ''; 
  7. elseif ( 'display' == $context ) 
  8. $action = '&action=edit'; 
  9. else 
  10. $action = '&action=edit'; 
  11.  
  12. $post_type_object = get_post_type_object( $post->post_type ); 
  13. if ( !$post_type_object ) 
  14. return; 
  15.  
  16. if ( !current_user_can( 'edit_post', $post->ID ) ) 
  17. return; 
  18.  
  19. if ( $post_type_object->_edit_link ) { 
  20. $link = admin_url( sprintf( $post_type_object->_edit_link . $action, $post->ID ) ); 
  21. } else { 
  22. $link = ''; 
  23.  
  24. /** 
  25. * Filters the post edit link. 
  26. * @since 2.3.0 
  27. * @param string $link The edit link. 
  28. * @param int $post_id Post ID. 
  29. * @param string $context The link context. If set to 'display' then ampersands 
  30. * are encoded. 
  31. */ 
  32. return apply_filters( 'get_edit_post_link', $link, $post->ID, $context );