To add custom content to the "Write" admin pages (where you write posts, pages or links), WordPress 2.5 introduces a new function set: add_meta_box()

Simple plugin example :
-
<?php
-
/*
-
Plugin Name: Example: Add Meta Box
-
Plugin URI: #
-
Description: Simple example showing how to add a "meta box" in WP 2.5
-
Version: 0.0
-
Author: Ozh
-
Author URI: http://planetozh.com/blog/
-
*/
-
-
// This function tells WP to add a new "meta box"
-
function add_some_box() {
-
add_meta_box(
-
'ozh', // id of the <div> we'll add
-
'My Box', //title
-
'add_something_in_the_box', // callback function that will echo the box content
-
'post' // where to add the box: on "post", "page", or "link" page
-
);
-
}
-
-
// This function echoes the content of our meta box
-
function add_something_in_the_box() {
-
echo "I'm living in a box";
-
}
-
-
// Hook things in, late enough so that add_meta_box() is defined
-
if (is_admin())
-
add_action('admin_menu', 'add_some_box');
-
-
?>
The catch here is to hook the function that add our "meta box" (here: add_some_box()) late enough so that the function it needs, add_meta_box(), has been defined. Hooking on 'admin_menu' is fine.
If you want something compatible with both WordPress 2.3 and 2.5, you will need something like the following:
-
if (function_exists('add_meta_box') {
-
// 2.5 style
-
} else {
-
// 2.3
-
}
wrote, on 26/Feb/08 at 5:38 am # :
Nice, thanks for that info. I can't find an argument to change the order of the boxes around? Is that not possible?
said, on 26/Feb/08 at 9:41 am # :
yms » I also wanted to tweak that, but it seems there is no argument to pass in order to achieve this
replied, on 13/Mar/08 at 2:35 pm # :
I was waiting for this feature for ages, thank you
replied, on 02/Apr/08 at 7:59 pm # :
Nice snippet. But now I have a "what next" question.
Suppose I create a meta box that has some form elements in it to collect meta data. When the user saves the post, I want to be able to process and save the meta data.
How do I write a plugin to access that POSTed data?
commented, on 02/Apr/08 at 9:01 pm # :
Jacob » Learn how to write complex plugins starting with information you'll find in the Codex, and by reading plugin sources. Definitely not something that belongs to a comment reply.
pingback on 24/Apr/08 at 8:53 pm # :
[...] and “Write Page” pages will need to change their methods to work with the new design. Ozh explains how to use add_meta_box() with the new design to add those custom fields. He has a simple, straightforward example plugin. Posted in Plugins | Tagged admin menu, Version [...]
pingback on 24/Apr/08 at 9:38 pm # :
[...] and “Write Page” pages will need to change their methods to work with the new design. Ozh explains how to use add_meta_box() with the new design to add those custom fields. He has a simple, straightforward example plugin. Filed under Pressed Words, WordPress. Bookmark [...]
replied, on 29/Apr/08 at 4:02 pm # :
Hi,
Thank you for this tutorial.
What if I want to replace an existing meta box.
Lets say I made a plugin witch extends page templates.
So I have made an Page templates extended meta box, but now I want to hide the default Page template meta box.
Is this possible?
wrote, on 29/Apr/08 at 7:07 pm # :
Thomas-DK » CSS (or javascript) are the easiest ways
commented, on 10/Jun/08 at 6:29 pm # :
Hi,
I installed your plugin and looks good. Thanks! However, I keep getting "tinyMCE not defined" error (in firebug) when I try to switch to visual editor. Once I un-installed the plugin, the error was gone. I made a similar plugin before and also have the same problem. Do you know why?
Thanks!
commented, on 10/Jun/08 at 9:06 pm # :
Ping » No idea. This code has nothing to do with the editor.
thought, on 18/Jun/08 at 3:15 am # :
Hi and thanks for the great reading.
Could answer me, if is there any link or tutorial to insert these meta box's, in a plugin page ?
In my case, I'm writing a domain manager to control a few domains I have, and I want to add these meta box's on the 'Add a Domain' page.
Kind Regards!