Leave a Reply

1 comment

  1. Actually is not true that on post-new.php global $post is not defined, WordPress create a post and save it in database as auto-draft using get_default_post_to_edit before output the page and so the metabox, so you can rely on global post object.

    However, I don’t like using global post object and to get the brand-new post on post-new.php I use transition post status, like so (proof of concept):

    function new_post_info( $new = NULL, $old = NULL, $post = NULL ) {
      static $info = NULL;
      if (  $new === 'auto-draft' && ! empty( $post )  ) {
        $info = $post;
      }
      return $info;
    }
    
    add_action('transition_post_status', 'new_post_info', 999, 3 );
    
    add_action( 'add_meta_boxes', function() {
      add_meta_box( 'test', 'Test', function() {
        echo '<pre>';
        print_r( new_post_info() );
        echo '</pre>';
      }, 'post' );
    });
    

    In this way in metabox I can get information on the default draft generated and saved by WordPress.

    That said, if you want to show to an user a “live preview” of what she is doing, there is no other chances than use javascript or reload the entire page, and the first is largely better, the second should only be used as fallback for users with javascript disabled.

    Over there exist some javascript frameworks like Angular or Ember (and others) capable to live UI binding (automatically update a view according to user input) without you have to “manually” implement ajax, but… they are javascript… magic doesn’t exists.