In: , ,
On: 2008 / 02 / 25 Viewed: 62673 times
Shorter URL for this post: http://ozh.in/gz

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()

add_meta_box

Simple plugin example :

  1. <?php
  2. /*
  3. Plugin Name: Example: Add Meta Box
  4. Plugin URI: #
  5. Description: Simple example showing how to add a "meta box" in WP 2.5
  6. Version: 0.0
  7. Author: Ozh
  8. Author URI: http://planetozh.com/blog/
  9. */
  10.  
  11. // This function tells WP to add a new "meta box"
  12. function add_some_box() {
  13.     add_meta_box(
  14.         'ozh', // id of the <div> we'll add
  15.         'My Box', //title
  16.         'add_something_in_the_box', // callback function that will echo the box content
  17.         'post' // where to add the box: on "post", "page", or "link" page
  18.     );
  19. }
  20.  
  21. // This function echoes the content of our meta box
  22. function add_something_in_the_box() {
  23.     echo "I'm living in a box";
  24. }
  25.  
  26. // Hook things in, late enough so that add_meta_box() is defined
  27. if (is_admin())
  28.     add_action('admin_menu', 'add_some_box');
  29.  
  30. ?>

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:

  1. if (function_exists('add_meta_box') {
  2.     // 2.5 style
  3. } else {
  4.     // 2.3
  5. }

Shorter URL

Want to share or tweet this post? Please use this short URL: http://ozh.in/gz

Metastuff

This entry "WordPress Snippet: add_meta_box()" was posted on 25/02/2008 at 10:51 pm and is tagged with , ,
Watch this discussion : Comments RSS 2.0.

17 Blablas

  1. 1
    yms Germany »
    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?

  2. 2
    Ozh France »
    thought, 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

  3. 3
    Frank Malina United Kingdom »
    thought, on 13/Mar/08 at 2:35 pm # :

    I was waiting for this feature for ages, thank you

  4. 4
    Jacob United States »
    commented, 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?

  5. 5
    Ozh France »
    thought, 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.

  6. 6
    Adding Advanced Options Boxes in WordPre... United States »
    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 [...]

  7. 7
    Il Filosofo » Adding Advanced Opti... United States »
    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 [...]

  8. 8
    Thomas-DK Denmark »
    wrote, 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?

  9. 9
    Ozh France »
    wrote, on 29/Apr/08 at 7:07 pm # :

    Thomas-DK » CSS (or javascript) are the easiest ways

  10. 10
    Ping Canada »
    said, 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!

  11. 11
    Ozh France »
    wrote, on 10/Jun/08 at 9:06 pm # :

    Ping » No idea. This code has nothing to do with the editor.

  12. 12
    Giovanni Brazil »
    replied, 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!

  13. 13
    ben France »
    replied, on 28/Aug/08 at 4:15 am # :

    Next step is how to save a data field :)

  14. 14
    anilmakhijani.com » Adding Meta In... France »
    pingback on 13/Oct/08 at 7:27 pm # :

    [...] I found a useful blog post on how to add text fields to the "link management" admin menu in WordPress:  http://planetozh.com/blog/2008/02/wordpress-snippet-add_meta_box/ [...]

  15. 15
    Usando Wordpress Hooks - Ganchos | Tutor... United States »
    pingback on 24/Dec/08 at 3:03 pm # :

    [...] documentação do WordPress dá pra saber que a (nova) função para criar caixas de opções é a add_meta_box. E se add adiciona, remove exclui e te pergunta três parâmetros. [...]

  16. 16
    Roger Brazil »
    replied, on 07/Apr/09 at 3:56 pm # :

    Giovanni,

    Veja se isso ajuda:
    Maybe this can help:

    http://www.code-styling.de/english/how-to-use-wordpress-metaboxes-at-own-plugins

    Câmbio?
    Over?

  17. 17
    WordPress snippet: add_meta_box() per ag... Italy »
    pingback on 29/Apr/09 at 2:15 pm # :

    [...] breve tutorial con un pezzo di codice per aggiungere tramite plugin del contenuto personalizzato nella pagina di [...]

Leave a Reply

Comment Guidelines or Die

  • HTML: You can use these tags: <a href=""> <em> <i> <b> <strong> <blockquote>
  • Posting code: Post raw code (no <> &lt; etc) within appropriate tags : [php][/php], [css][/css], [html][/html], [js][/js], [sql][/sql], [xml][/xml], or generic [code][code]
  • Gravatars: Curious about the little images next to each commenter's name ? Go to Gravatar.
  • Spam: Various spam plugins on patrol. I'll put pins in a Voodoo doll if you spam me.
  • I will mark as Spam test comments, all comments with SEO names (ie "My Cool Online Shop" instead of "Joe") or containing forum-like signatures.

Read more ?