1

I've a bunch of techniques to add some information in admin area for users. For that I use WordPress APIs and meta boxes.

Ok it's cool but what if I want to display information that are set with the post e.g I have a metabox, I want to show some preview of the settings inside post edit.

I cannot do this using global $post and $post->ID which are pretty handy for get_post_meta().

I mean it's pretty a matter of load. At this time e.g in post-new.php we do not have this information because WP_Post object has not been set up yet.

How do you do this?

EDIT: to be more precise I'm trying to get post meta in admin area. For example I have a code that add a metabox and the user enter or select something. I want to retrieve that in the same area. This way the user can see if its setting is correct or not.

I've nothing against AJAX but I'm one of those that consider nojs first and if possible or if that brings comfort for users js as additional layer.

3
  • seems there are hints here : codex
    – JMau
    Commented Apr 8, 2014 at 15:30
  • Interesting question, still it is very unclear for me what your actual goal is. Code you mock up some screenshot or try to file an edit with additional information or rephrase it a bit?
    – kaiser
    Commented Apr 8, 2014 at 15:59
  • it depends on what you want to do: using defaults passed as callaback args or stored somewhere, using ajax, get post information using post status transition... really, there is no one answer.
    – gmazzap
    Commented Apr 8, 2014 at 20:04

1 Answer 1

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.

1
  • I kinda had the feeling there is a limit here. I'm to learn to use these frameworks, actually it's high time... thanks for this answer, I often use this hook transition_post_status but I never thought about this kind of use.
    – JMau
    Commented Apr 9, 2014 at 1:57

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.