get_preview_post_link

Retrieves the URL used for the post preview.

Description

(string|null) get_preview_post_link( (constant) $post = null, (array) $query_args = array(), (string) $preview_link = '' ); 

Allows additional query args to be appended.

Returns (string|null)

URL used for the post preview, or null if the post does not exist.

Parameters (3)

0. $post — Optional. (constant) => null
Post ID or WP_Post object. Defaults to global $post..
1. $query_args — Optional. (array) => array()
Array of additional query args to be appended to the link. Default empty array.
2. $preview_link — Optional. (string) => ''
Base preview link to be used if it should differ from the post permalink. Default empty.

Usage

  1. if ( !function_exists( 'get_preview_post_link' ) ) { 
  2. require_once ABSPATH . WPINC . '/link-template.php'; 
  3.  
  4. // Optional. Post ID or `WP_Post` object. Defaults to global `$post`. 
  5. $post = null; 
  6.  
  7. // Optional. Array of additional query args to be appended to the link. 
  8. // Default empty array. 
  9. $query_args = array(); 
  10.  
  11. // Optional. Base preview link to be used if it should differ from the 
  12. // post permalink. Default empty. 
  13. $preview_link = ''; 
  14.  
  15. // NOTICE! Understand what this does before running. 
  16. $result = get_preview_post_link($post, $query_args, $preview_link); 
  17.  

Defined (1)

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

/wp-includes/link-template.php  
  1. function get_preview_post_link( $post = null, $query_args = array(), $preview_link = '' ) { 
  2. $post = get_post( $post ); 
  3. if ( ! $post ) { 
  4. return; 
  5.  
  6. $post_type_object = get_post_type_object( $post->post_type ); 
  7. if ( is_post_type_viewable( $post_type_object ) ) { 
  8. if ( ! $preview_link ) { 
  9. $preview_link = set_url_scheme( get_permalink( $post ) ); 
  10.  
  11. $query_args['preview'] = 'true'; 
  12. $preview_link = add_query_arg( $query_args, $preview_link ); 
  13.  
  14. /** 
  15. * Filters the URL used for a post preview. 
  16. * @since 2.0.5 
  17. * @since 4.0.0 Added the `$post` parameter. 
  18. * @param string $preview_link URL used for the post preview. 
  19. * @param WP_Post $post Post object. 
  20. */ 
  21. return apply_filters( 'preview_post_link', $preview_link, $post );