WordPress.org

Ready to get started?Download WordPress

Forums

[resolved] Custom meta box not showing up (4 posts)

  1. erikstainsby
    Member
    Posted 11 months ago #

    I've created a custom post type and now need to attach a custom meta box to the edit context. The code I've cobbled together is not doing anything that I can see.

    Here's some of the relevant bits:

    class Fixtures {
       public function __construct() {
          add_action( 'init', array(&$this,'create_post_type'));
          add_action( 'add_meta_box', array(&$this,'fixtures_add_custom_meta_box'));
       }
    
       function create_post_type() {
          /* code works - omitted */
       }
    
       public function fixtures_add_custom_meta_box() {
    	error_log( __FUNCTION__ );   // never shows up in debug.log
    	add_meta_box(
                'fixtures-metabox'
                ,'Fixture Attributes'
                ,array(&$this, 'render_custom_meta_box')
                ,'post'
                ,'normal'
                ,'high'
            );
        }
    
        public function render_custom_meta_box() {
        	error_log( __FUNCTION__ );     // never shows up in debug.log
        	 wp_nonce_field( plugin_basename( __FILE__ ), 'fixtures_noncename' );
    
        	 // the actual fields presented by the meta_box
        	 // - potential parents - only none, other fixtures
        	 // - ordering number
        	 // we omit the template option for now to simplify the plugin
    
        	 $value = get_post_meta( $post->ID, '_fixtures_parent_id', true );
        	 echo '<label for="fixtures_parent_id">';
        	 echo 'Parent Fixture';
        	 echo '</label> ';
        	 echo '<input type="text" id="fixtures_parent_id" name="fixtures_parent_id" value="'.esc_attr($value).'" />';
    
        	 $value = get_post_meta( $post->ID, '_fixtures_order', true );
        	 echo '<label for="fixtures_order">';
        	 echo'Order';
        	 echo '</label> ';
        	 echo '<input type="text" id="fixtures_order" name="fixtures_order" value="'.esc_attr($value).'" size="4" />';
        }
    }
    
    $fixtures = new Fixtures();

    Any insights greatly appreciated.
    ~ Erik

  2. erikstainsby
    Member
    Posted 11 months ago #

    Caught one error myself:
    add_action( 'add_meta_box',
    ought to be
    add_action( 'add_meta_boxes',

    Function fixtures_add_custom_meta_box() now runs but still does not render anything to page.

  3. ianhaycox
    Member
    Posted 11 months ago #

    Maybe add_meta_box should be passing the custom_post_type rather than than 'post' ?

    Or possibly the action should be

    add_action( 'add_meta_boxes_' . $custom_post_slug, array(&$this, ...

    Couple of things to try.

    Ian.

  4. erikstainsby
    Member
    Posted 11 months ago #

    Thanks Ian. I had forgotten to change the custom_post_type string back to 'fixtures' from 'post' after finding my previously mentioned error. Works now.

Reply

You must log in to post.

About this Topic