Home > General, Programming > How to add WordPress meta boxes to the sidebar

How to add WordPress meta boxes to the sidebar

March 27th, 2009 Leave a comment Go to comments

Unfortunately, the WordPress online documentation is a mess of old, outdated API information and when I tried to make a little plugin today and have it put a little meta box in the sidebar of the edit/new post admin pages, I had one hell of a hard time getting things to work the way it was documented.

Everywhere I looked, it told me to hook on to the “admin_menu” event when I wanted to queue up my meta box using add_meta_box().  After a bit of messing around, it seemed as though I had it working for the edit post page, but then it wasn’t showing up on the add new post page.  I did a bit of debugging and ensured that it was the fact that the add_meta_box() call wasn’t being queued properly – I assume it had something to do with when it was being added to the meta box queue… ultimately meaning that the “admin_menu” event was too early of an event for me to hook on to.

Nowhere in any of the documentation could I find any information stating what event should be hooked – in fact the documentation relating to add_meta_box() even said to use “admin_menu”.  After a few hours of messing around with their documentation and trying various event hooks, I finally found an event hook that appears to work for me – “admin_head”.  Using that event hook seems to be late enough that my meta box gets queued properly and shows up on both pages, but early enough that it’s not too late to add a meta box to the queue (which was another problem as well).

A friendly note to the WordPress developers – learn to bloody program; use output buffering (though you probably are already) and make useful event hooks.  If you’re going to introduce output queues for things like meta boxes – don’t make using the queues such a hassle (aka, these queues should not be dependant on HTML output)!

  1. Philipp
    September 7th, 2010 at 00:52 | #1

    Hy there,

    I’m running into the same problem here. The funny thing is that my box does not show up when I’m logged in as admin level 10 but works fine on level 7 and below… wtf??

    If you have any additional ideas what else I could try, I’d really appreciate that!

    Cheers,

    Phil

    • September 7th, 2010 at 01:17 | #2

      That’s quite odd – I never tested any of my stuff with different access levels so I’m not sure what’s at play in your situation. I’ll send you an email with the code I wrote (it’s very short) to show you what I came up with to get the meta box working for me.

  1. No trackbacks yet.