wp_post_preview_js() WordPress Function

The wp_post_preview_js() function allows you to preview your post before you publish it. This is a useful function if you want to make sure that your post looks the way you want it to before you make it public.

wp_post_preview_js() #

Outputs a small JS snippet on preview tabs/windows to remove window.name on unload.


Description

This prevents reusing the same tab for a preview when the user has navigated away.


Top ↑

Source

File: wp-includes/functions.php

function wp_post_preview_js() {
	global $post;

	if ( ! is_preview() || empty( $post ) ) {
		return;
	}

	// Has to match the window name used in post_submit_meta_box().
	$name = 'wp-preview-' . (int) $post->ID;

	?>
	<script>
	( function() {
		var query = document.location.search;

		if ( query && query.indexOf( 'preview=true' ) !== -1 ) {
			window.name = '<?php echo $name; ?>';
		}

		if ( window.addEventListener ) {
			window.addEventListener( 'unload', function() { window.name = ''; }, false );
		}
	}());
	</script>
	<?php
}


Top ↑

Changelog

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