WordPress Development Stack Exchange is a question and answer site for WordPress developers and administrators. It's 100% free, no registration required.

Sign up
Here's how it works:
  1. Anybody can ask a question
  2. Anybody can answer
  3. The best answers are voted up and rise to the top

I got a set of meta boxes containing different form fields. All of them are build inside a set of classes.

Now I have the problem that I have to hook the construction of the fields early enough to hook the needed scripts & styles. Everything is fine - I can display them, styles & scripts appear & work, saving the data works - but I can't retrieve the post meta data to fill the values of the fields.

Point is that the fields get hooked at admin_menu, which is too early to get the $post_id. Now I saw that I could also use $_GET['post'] to retrieve the ID.

What speaks against this?

share|improve this question
nothing, use it.... But why do you need the post_id at the init point? – onetrickpony Jul 6 '11 at 18:17
I need it for get_post_meta( $post_id, etc. ); to fill the values for my form fields if something was already added to the database. – kaiser Jul 6 '11 at 18:26
@One Trick Pony Edit: Class is hooked on admin_init. Doesn't make a big difference though. – kaiser Jul 7 '11 at 6:41
up vote 3 down vote accepted

Once you're in the function that actually prints the html for the metabox, the global $post object should always be set the to current post. Is there a specific reason that you need to know the post_id during init?

share|improve this answer
I need it for get_post_meta( $post_id, etc. ); to fill the values for my form fields if something was already added to the database. So I'm not talking about $post, but about $_GET['post']; - which I know found out, won't work on other post types as on 'post'. Any ideas? – kaiser Jul 6 '11 at 18:27
You will not be doing this at init though ... you will be doing it much later in the actual meta box. In the callback function for your metabox you can use global $post or get_post() or get_the_ID() any of these functions should work. – mfields Jul 6 '11 at 18:29
You shouldn't need the post ID on init, like Michael said you should be filling in form fields at the point of rendering the metabox, which shouldn't be occuring at init(metaboxes also have their own action already, if you're not using it, you should be). – Mark Duncan Jul 6 '11 at 18:54
@mfields @Mark Duncan You can't call scripts or styles from inside a meta box callback. The add_meta_boxes hook gets fired long after all enqueue & register hooks happend, so this is too late for (for example: colorpicker) script depended fields. Edit: The class is hooked on admin_menu. Not much better. – kaiser Jul 7 '11 at 6:35
In that case i agree, use $_GET['post'], though you still don't necessarily need it on init(you're at least not expecting it on every page are you?). I'd suggest the load-{$whateverpage} hook if this is only for specific admin pages(still early enough to fire off an enqueue). – t31os Jul 7 '11 at 20:59

Your Answer


By posting your answer, you agree to the privacy policy and terms of service.

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