1

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>';
    }
}
?>

4 Answers 4

2

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.

1
  • 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, 2011 at 19:22
2

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'  );
}
3
  • "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, 2013 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, 2013 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, 2013 at 23:36
0

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?

5
  • Why did you use admin_menu instead of the add_meta_boxes hook? Nov 7, 2011 at 19:06
  • @Kevin Langley Jr. Seems to be working fine in my plugin.
    – Jared
    Nov 7, 2011 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, 2011 at 19:14
  • @Jared - Definitely not best practice. Just because something works, does not mean it's correct. Nov 7, 2011 at 19:19
  • 1
    I edited my answer, you were correct. :)
    – Jared
    Nov 7, 2011 at 19:20
-1

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

1
  • The constructor-- __construct-- runs when the class is instantiated, which is done here: return new someClass();
    – s_ha_dum
    Mar 6, 2013 at 10:32

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

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