is_preview()WP 2.0.0

Check if the user is on the post preview page. Conditional tag

The function does NOT check if the user is authorized and whether they can edit the post. Any visitor can add ?preview=true, ?preview=1, or ?preview=not_false to the URL, and the function will return true.

Use current_user_can() when you need to check user permissions.

No Hooks.

Returns

true|false. Whether the query is for a post or page preview.

Usage

is_preview();

Examples

4

#1 Basic usage example:

if ( is_preview() ){
	// some code that will be triggered only on preview pages
}

Notes

  • Global. WP_Query. $wp_query WordPress Query object.

Changelog

Since 2.0.0 Introduced.

is_preview() code WP 7.0

function is_preview() {
	global $wp_query;

	if ( ! isset( $wp_query ) ) {
		_doing_it_wrong( __FUNCTION__, __( 'Conditional query tags do not work before the query is run. Before then, they always return false.' ), '3.1.0' );
		return false;
	}

	return $wp_query->is_preview();
}