get_preview_post_link() WordPress Function

The get_preview_post_link() function is used to retrieve the URL of a post preview. This is useful for previewing drafts of posts before they are published.

get_preview_post_link( int|WP_Post $post = null, array $query_args = array(), string $preview_link = '' ) #

Retrieves the URL used for the post preview.


Description

Allows additional query args to be appended.


Top ↑

Parameters

$post

(int|WP_Post)(Optional) Post ID or WP_Post object. Defaults to global $post.

Default value: null

$query_args

(array)(Optional) Array of additional query args to be appended to the link.

Default value: array()

$preview_link

(string)(Optional) Base preview link to be used if it should differ from the post permalink.

Default value: ''


Top ↑

Return

(string|null) URL used for the post preview, or null if the post does not exist.


Top ↑

Source

File: wp-includes/link-template.php

function get_preview_post_link( $post = null, $query_args = array(), $preview_link = '' ) {
	$post = get_post( $post );
	if ( ! $post ) {
		return;
	}

	$post_type_object = get_post_type_object( $post->post_type );
	if ( is_post_type_viewable( $post_type_object ) ) {
		if ( ! $preview_link ) {
			$preview_link = set_url_scheme( get_permalink( $post ) );
		}

		$query_args['preview'] = 'true';
		$preview_link          = add_query_arg( $query_args, $preview_link );
	}

	/**
	 * Filters the URL used for a post preview.
	 *
	 * @since 2.0.5
	 * @since 4.0.0 Added the `$post` parameter.
	 *
	 * @param string  $preview_link URL used for the post preview.
	 * @param WP_Post $post         Post object.
	 */
	return apply_filters( 'preview_post_link', $preview_link, $post );
}


Top ↑

Changelog

Changelog
VersionDescription
4.4.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