WordPress.org

Ready to get started?Download WordPress

Forums

add_meta_box undefined WP 3.1.3 (4 posts)

  1. Rebecca Putinski
    Member
    Posted 3 years ago #

    I just downloaded and installed WP 3.1.3 for a test site.

    When I try add_meta_box(...) from my custom theme's functions.php, I get the error in my server log that the function is undefined. The codex claims this function has been around since 2.5.

    Do I need to be using this somewhere else? I looked around for examples on usage and they all seem to be used in functions.php.

  2. Dion Hulse
    WordPress Dev
    Posted 3 years ago #

    add_meta_box() is, and always has been, only defined in the admin area.

    You can use add_meta_box() in your themes functions.php, you just need to only execute it on admin requests, for example:

    add_action('admin_init', 'mytheme_admin_init');
    function mytheme_admin_init() {
    add_meta_box(....);
    }
  3. Rebecca Putinski
    Member
    Posted 3 years ago #

    Aha, great, thanks! That definitely helps. I was confused because I could do things like have actions for custom post types and lots of other things to do with posts, but apparently not create a meta box for a post without it being in admin_init. Inconsistencies ftl.

  4. Dion Hulse
    WordPress Dev
    Posted 3 years ago #

    When it comes to the themes functions.php, you shouldn't really be running anything outside of actions. ie. All code should be wrapped in functions hooked to init or later usually (Aside from theme setup stuff, which can be hooked to 'after_setup_theme')

    So the only functions you should call in functions.php are add_filter() and add_action()

    Have a look at TwentyTen's functions.php for an example of this: http://core.trac.wordpress.org/browser/trunk/wp-content/themes/twentyten/functions.php#L50

Topic Closed

This topic has been closed to new replies.

About this Topic