I am putting together a framework for adding post types. I can't seem to figure out why this is not adding a metabox for my custom post type and instead causing an error:
[26-Nov-2012 02:41:26 UTC] PHP Fatal error: Call to undefined function add_meta_box() in /Applications/MAMP/htdocs/wp-content/plugins/plugin.php on line 131
<?php
/*
Copyright 2012 Em Gee (email : emgee )
This program is free software; you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
the Free Software Foundation; either version 2 of the License, or
(at your option) any later version.
This program is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU General Public License for more details.
You should have received a copy of the GNU General Public License
along with this program; if not, write to the Free Software
Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
Plugin Name: Biographies
Description: Adds Custom Post Type to easily add and manage biographies for NMS
Version: 1.0
Author: EMGEE
Author URI: http://www.google.com
*/
/**
*
* NMS Biographies
*
*
* @package NMSBiographies
*/
class mgwpbase {
static protected $plugin_version = "base";
static protected $thumbnail_name = "";
static protected $thumbnail_width = "";
static protected $thumbnail_height = "";
static protected $post_type_supports = array ( "author", "title", "thumbnail", "revisions", "editor" );
static protected $post_type_name = "";
static protected $post_type = "";
static protected $post_type_icon = "";
static protected $post_type_rewrite = "";
static protected $post_type_name_singular = "";
protected static $ins = null;
public function _setup() {
static::register_post_type();
static::register_thumbnail();
add_action("add_meta_boxes", static::add_meta_boxes());
}
static function register_thumbnail(){
$thumbnail_name = static::$thumbnail_name;
$thumbnail_width = static::$thumbnail_width;
$thumbnail_height = static::$thumbnail_height;
if (!$thumbnail_name || !$thumbnail_width || !$thumbnail_height) return false;
add_image_size( $thumbnail_name, $thumbnail_width, $thumbnail_height, true );
}
static function register_post_type(){
$post_type = static::$post_type;
$post_type_name = static::$post_type_name;
$post_type_supports = static::$post_type_supports;
$post_type_icon = static::$post_type_icon;
$post_type_rewrite = static::$post_type_rewrite;
static::$post_type_name_singular ? $post_type_name_singular = static::$post_type_name_singular : $post_type_name_singular = static::$post_type_name;
if (!$post_type || !$post_type_name) return false;
$labels = array(
'name' => _x( $post_type_name_singular, $post_type ),
'singular_name' => _x( $post_type_name_singular, $post_type ),
'add_new' => _x( 'Add New', $post_type ),
'add_new_item' => _x( 'Add New ' . $post_type_name_singular, $post_type ),
'edit_item' => _x( 'Edit ' . $post_type_name_singular, $post_type ),
'new_item' => _x( 'New ' . $post_type_name_singular, $post_type ),
'view_item' => _x( 'View ' . $post_type_name_singular, $post_type ),
'search_items' => _x( 'Search ' . $post_type_name, $post_type ),
'not_found' => _x( 'No ' . $post_type_name . ' found', $post_type ),
'not_found_in_trash' => _x( 'No ' . $post_type_name . ' found in Trash', $post_type ),
'parent_item_colon' => _x( 'Parent ' . $post_type_name_singular . ':', $post_type ),
'menu_name' => _x( $post_type_name, $post_type ),
);
$args = array(
'labels' => $labels,
'hierarchical' => false,
'supports' => $post_type_supports,
'public' => true,
'show_ui' => true,
'show_in_menu' => true,
'show_in_nav_menus' => true,
'publicly_queryable' => true,
'exclude_from_search' => false,
'has_archive' => true,
'query_var' => true,
'can_export' => true,
'capability_type' => 'post',
);
if ($post_type_icon){
$args['menu_icon'] = $post_type_icon;
}
if (!$post_type_rewrite) $args['rewrite'] = false;
else $args['rewrite'] = true;
register_post_type( $post_type, $args );
}
}
class nmsbiographies extends mgwpbase {
static protected $plugin_version = 1.0;
static protected $thumbnail_name = "nms-biographies";
static protected $thumbnail_width = 150;
static protected $thumbnail_height = 150;
static protected $post_type = "nms_bios";
static protected $post_type_name = "NMS Biographies";
static protected $post_type_name_singular = "NMS Biography";
public function _setup() {
parent::_setup();
}
public static function init() {
add_action('init', static::_setup());
}
static function add_meta_boxes(){
add_meta_box( 'test_div', "Test", static::new_meta, 'gallery', 'normal', 'low');
}
function new_meta($post) {
echo "TESTING";
}
}
nmsbiographies::init();
Michael Caputo answers:
Your function is called add_meta_boxes, not add_meta_box
Michael Caputo comments:
Also, more info on how to add a meta box: http://codex.wordpress.org/Function_Reference/add_meta_box
mackrider comments:
Not sure this response does any good, I am trying to use WordPress' API call for add_meta_box
Matty Cohen answers:
Hi there,
From what I can see in the code provided, you'd need to have the following in place:
1. An internal method (possibly called "add_meta_boxes", if you'd like). Inside this method is where you could call the [[LINK href="http://codex.wordpress.org/Function_Reference/add_meta_box"]]add_meta_box[[/LINK]]()
function.
2. When calling this method, ensure that your action is not set to "add_meta_boxes" but to "admin_menu", which is late enough in the WordPress admin's load cycle to call your meta boxes.
3. When hooking your method onto the add_action(), the function/method name you are hooking on should be passed as a string or an array and not as a direct call to the function itself.
For example, where you have,
add_action("add_meta_boxes", static::add_meta_boxes());
You could have,
add_action( 'admin_menu', array( self, 'add_meta_boxes' ) );
Or if you had instantiated the class, you would have,
add_action( 'admin_menu', array( $this, 'add_meta_boxes' ) );
I hope this helps towards clarifying your query. Please see the [[LINK href="http://codex.wordpress.org/Function_Reference/add_meta_box"]]add_meta_box[[/LINK]]()
section of the WordPress Codex for the remaining functions required to display content within a meta box.
Thanks and regards,
Matty.