Take the 2-minute tour ×
WordPress Development Stack Exchange is a question and answer site for WordPress developers and administrators. It's 100% free, no registration required.

This issue has plagued me over a couple projects. I used the default Wordpress class-based template provided in the Codex to start, and built outward. The only issue I face is that my custom meta boxes will not show when creating a new post (add post). This is usually in conjunction with custom post types, but as the code from the Wordpress demonstrates, it occurs with just a regular meta box.

What needs to be done to ensure this will display when clicking 'Add New Post'?

<?php
/*
Plugin Name: TestPlug
Description: Blank
Author: No One
Version: 1.0
*/
function call_someClass() {
 return new someClass();
}
if ( is_admin() ) add_action( 'load-post.php', 'call_someClass' );

class someClass {
    public function __construct()
    {
        add_action( 'add_meta_boxes', array( &$this, 'add_some_meta_box' ) );
    }

    public function add_some_meta_box()
    {
        add_meta_box(
             'some_meta_box_name'
            ,'Some Meta Box Headline'
            ,array( &$this, 'render_meta_box_content' )
            ,'post'
            ,'advanced'
            ,'high'
        );
    }

    public function render_meta_box_content()
    {
        echo '<h1>TEST OUTPUT - this gets rendered inside the meta box.</h1>';
    }
}
?>
share|improve this question
add comment

4 Answers 4

up vote 2 down vote accepted

I would create the instance of the class on init or even admin_init and not on 'load-post.php'.

Hooking into load-post.php will not add the boxes to NEW posts and only posts that are already saved. The correct hook to add meta boxes to ONLY new posts is 'load-post-new.php'.

If you want to have the meta box display on both new posts and existing posts, use the 'add_meta_boxes' hook.

share|improve this answer
    
Yup, that's it. I think I figured this out before, but it always throws me for a loop. Spot on, thanks. –  Exit Nov 7 '11 at 19:22
add comment

replace

if ( is_admin() ) add_action( 'load-post.php', 'call_someClass' );

With

if ( is_admin() ) {
    add_action( 'load-post.php', 'call_someClass' );
    add_action( 'load-post-new.php', 'call_someClass'  );
}
share|improve this answer
    
"This answer was automatically flagged as low-quality because of its length and content", which I am sure you don't want. In addition to posting code, please try to explain what you did and why it will solve the problem. –  s_ha_dum Mar 25 '13 at 17:03
    
This worked for me! Thanks. For my code, call_someClass is actually calling a function that instanciates my class return new someClass();, and within that class' constructor is an action() that's hooked into add_meta_boxes, and so on ... –  mhulse Aug 9 '13 at 23:20
    
... in the end, I think I like using if (is_admin()) add_action('admin_init', 'call_classOrFunction'); better (as suggested by the approved answer for this question). I wonder if there's an advantage over one technique to the other? –  mhulse Aug 9 '13 at 23:36
add comment

Edit: I was wrong, admin_menu is not what you need. However after researching, I came up with this which is working for me:

add_action( 'admin_init', 'add_some_meta_box', 1 );

function add_some_meta_box()
{
    add_meta_box(
         'some_meta_box_name'
        ,'Some Meta Box Headline'
        ,'render_meta_box_content'
        ,'post'
        ,'normal'
        ,'high'
    );
}

function render_meta_box_content()
{
    echo '<h1>TEST OUTPUT - this gets rendered inside the meta box.</h1>';
}

It seems that taking it out of a class makes it work, not sure why though but it could possibly be a bug?

share|improve this answer
    
Why did you use admin_menu instead of the add_meta_boxes hook? –  Kevin Langley Jr. Nov 7 '11 at 19:06
    
@Kevin Langley Jr. Seems to be working fine in my plugin. –  Jared Nov 7 '11 at 19:10
    
Sorry, that doesn't work at all from what I'm seeing. After trying this, the meta box disappeared entirely. –  Exit Nov 7 '11 at 19:14
    
@Jared - Definitely not best practice. Just because something works, does not mean it's correct. –  Kevin Langley Jr. Nov 7 '11 at 19:19
1  
I edited my answer, you were correct. :) –  Jared Nov 7 '11 at 19:20
add comment

The problem in the top code could be that you defined the 'public function __construct()' but you never actually called it.

share|improve this answer
    
The constructor-- __construct-- runs when the class is instantiated, which is done here: return new someClass(); –  s_ha_dum Mar 6 '13 at 10:32
add comment

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.