wp_post_preview_js

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

Description

wp_post_preview_js(); 

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


Usage

  1. if ( !function_exists( 'wp_post_preview_js' ) ) { 
  2. require_once ABSPATH . WPINC . '/functions.php'; 
  3.  
  4.  
  5. // NOTICE! Understand what this does before running. 
  6. $result = wp_post_preview_js(); 
  7.  

Defined (1)

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

/wp-includes/functions.php  
  1. function wp_post_preview_js() { 
  2. global $post; 
  3.  
  4. if ( ! is_preview() || empty( $post ) ) { 
  5. return; 
  6.  
  7. // Has to match the window name used in post_submit_meta_box() 
  8. $name = 'wp-preview-' . (int) $post->ID; 
  9.  
  10. ?> 
  11. <script> 
  12. ( function() { 
  13. var query = document.location.search; 
  14.  
  15. if ( query && query.indexOf( 'preview=true' ) !== -1 ) { 
  16. window.name = '<?php echo $name; ?>'; 
  17.  
  18. if ( window.addEventListener ) { 
  19. window.addEventListener( 'unload', function() { window.name = ''; }, false ); 
  20. }()); 
  21. </script> 
  22. <?php