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.
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 usingget_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):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.