wp_post_preview_js

The timeline below displays how wordpress function wp_post_preview_js has changed across different WordPress versions. If a version is not listed, refer to the next available version below.

WordPress Version: 6.4

/**
 * Outputs a small JS snippet on preview tabs/windows to remove `window.name` when a user is navigating to another page.
 *
 * This prevents reusing the same tab for a preview when the user has navigated away.
 *
 * @since 4.3.0
 *
 * @global WP_Post $post Global post object.
 */
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;
    ob_start();
    ?>
	<script>
	( function() {
		var query = document.location.search;

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

		if ( window.addEventListener ) {
			window.addEventListener( 'pagehide', function() { window.name = ''; } );
		}
	}());
	</script>
	<?php 
    wp_print_inline_script_tag(wp_remove_surrounding_empty_script_tags(ob_get_clean()));
}

WordPress Version: 5.4

/**
 * Outputs a small JS snippet on preview tabs/windows to remove `window.name` on unload.
 *
 * This prevents reusing the same tab for a preview when the user has navigated away.
 *
 * @since 4.3.0
 *
 * @global WP_Post $post Global post object.
 */
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 
}

WordPress Version: 5.3

/**
 * Outputs a small JS snippet on preview tabs/windows to remove `window.name` on unload.
 *
 * This prevents reusing the same tab for a preview when the user has navigated away.
 *
 * @since 4.3.0
 *
 * @global WP_Post $post Global post object.
 */
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 
}

WordPress Version: 4.9

/**
 * Outputs a small JS snippet on preview tabs/windows to remove `window.name` on unload.
 *
 * This prevents reusing the same tab for a preview when the user has navigated away.
 *
 * @since 4.3.0
 *
 * @global WP_Post $post
 */
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 
}

WordPress Version: 4.3

/**
 * Outputs a small JS snippet on preview tabs/windows to remove `window.name` on unload.
 *
 * This prevents reusing the same tab for a preview when the user has navigated away.
 *
 * @since 4.3.0
 */
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 
}