Do not include wp-admin/includes/template.php to get add_meta_box()
Don’t include wp-admin/includes/template.php to get add_meta_box() defined. This is a very wrong way to go about adding meta boxes.
The proper way to call add_meta_box() is to consider it to be admin-only (because it is). What you do is call add_meta_box() on the admin_init hook, or even better, the add_meta_boxes or add_meta_boxes_{$page} hooks. (Where $page is either a post type or ‘link’ or ‘comments’.)
Then there’s no need to include an admin file — the function will always exist when it is called.
During 3.3′s development, we noticed that some plugins were doing this, so we wanted to make the PSA.
Marko Heijnen 12:38 am on December 7, 2011 Permalink
I do it in one project because I also want to use it on the backend and frontend. It probably will break then
Reason why I did it was because of reusable code and in my opinion that was to use what WordPress itself also uses.
What would be the best way to to that? Make something simular by yourself on the frontpage that calls the functions of the meta boxes?
Andrew Nacin 3:21 am on December 7, 2011 Permalink
I’ve never seen meta boxes used on the frontend. There are too many assets that need to come together for them to work well. That said, if you do need to load admin assets, then you should load the entire bootstrap: wp-admin/includes/admin.php. That’s the only way you can be sure nothing will ever break. Of course, it’s a pretty big performance and memory hit, but that’s the price to pay for using admin functions on the frontend.
Marko Heijnen 7:37 am on December 7, 2011 Permalink
I did fix it now by loading screen.php too. In this case it is a project that I will maintain. I will never going to publish something like this in a plugin.
In my case I got a post type company. A user can manage his/her company on the front-end but the customer will manage it on the backend. Do be always the same I thought to load meta boxes on the frontend. Probably I should make another set of actions to load the same functions for the frontend. So it is save to use and still one function that handles it.
Rafael 1:27 am on December 7, 2011 Permalink
Damn, and Im here wondering: “who does that anyway?!” while reading the post…