I have 2 files in my plugin for this, one makes the custom post type and its meta box and the fields for the meta box. The 2nd file prints out the meta box, its basically the layout.
I am able to succesfully save data for text fields and checkbox and select list. however for the life of me i cant figure out how to add radio and url fields, as well as other kinds of fields maybe a little more complicated later on, im stuck with the 3 fields (text, select, checkbox).
Can someone help me? Is the template and the way im creating this ok? Ive searched so many tutorials and some use meta boxs and meta fields, some use classes, others dont use class, its so confusing, im not too sure if im going about this the best way.
My first file:
<?php
/**
* The Constructor
*/
public function __construct()
{
// register actions
add_action('init', array(&$this, 'init'));
add_action('admin_init', array(&$this, 'admin_init'));
} // END public function __construct()
/**
* hook into WP's init action hook
*/
public function init()
{
// Initialize Post Type
$this->create_post_type();
add_action('save_post', array(&$this, 'save_post'));
} // END public function init()
/**
* Create the post type
*/
public function create_post_type()
{
register_post_type(self::POST_TYPE,
array(
'labels' => array(
'name' => __(sprintf('%ss', ucwords(str_replace("_", " ", self::POST_TYPE)))),
'singular_name' => __(ucwords(str_replace("_", " ", self::POST_TYPE))),
),
'public' => true,
'has_archive' => true,
'publicly_queryable' => true,
'query_var' => true,
'rewrite' => true,
'capability_type' => 'post',
'description' => __("Artist post type for My Plugin"),
'show_ui' => true,
'supports' => array(
'title', 'editor', 'thumbnail', 'revisions',
),
'menu_position' => 5,
// 'menu_icon' => plugins_url( 'images/image.png', __FILE__ ),
// 'taxonomies' => array( 'category', 'post_tag' ), // add default post categories and tags
)
);
}
/**
* Save the metaboxes for this custom post type
*/
public function save_post($post_id)
{
// verify if this is an auto save routine.
// If it is our form has not been submitted, so we dont want to do anything
if(defined('DOING_AUTOSAVE') && DOING_AUTOSAVE)
{
return;
}
if(isset($_POST['post_type']) && $_POST['post_type'] == self::POST_TYPE && current_user_can('edit_post', $post_id))
{
foreach($this->_meta as $field_name)
{
// Update the post's meta field
update_post_meta($post_id, $field_name, $_POST[$field_name]);
}
}
else
{
return;
} // if($_POST['post_type'] == self::POST_TYPE && current_user_can('edit_post', $post_id))
} // END public function save_post($post_id)
/**
* hook into WP's admin_init action hook
*/
public function admin_init()
{
// Add metaboxes
add_action('add_meta_boxes', array(&$this, 'add_meta_boxes'));
} // END public function admin_init()
/**
* hook into WP's add_meta_boxes action hook
*/
public function add_meta_boxes()
{
// Add this metabox to every selected post
add_meta_box(
sprintf('pr_my_plugin_%s_section', self::POST_TYPE),
sprintf('%s Information', ucwords(str_replace("_", " ", self::POST_TYPE))),
array(&$this, 'add_inner_meta_boxes'),
self::POST_TYPE
);
} // END public function add_meta_boxes()
/**
* called off of the add meta box
*/
public function add_inner_meta_boxes($post)
{
// Render the job order metabox
include(sprintf("%s/../templates/%s_metabox.php", dirname(__FILE__), self::POST_TYPE));
} // END public function add_inner_meta_boxes($post)
} // END class Artist_Template
} // END if(!class_exists('Artist_Template'))
?>
My 2nd file
<?php
$values = get_post_custom( $post->ID );
$selected = isset( $values['meta_f'] ) ? esc_attr( $values['meta_f'][0] ) : â;
$check = isset( $values['meta_g'] ) ? esc_attr( $values['meta_g'][0] ) : â;
$radioselected = isset( $values['meta_h'] ) ? esc_attr( $values['meta_h'][0] ) : â;
?>
<table>
<tr valign="top">
<th class="metabox_label_column">
<label for="meta_country" class="pr-myplugin-admin"><?php _e( 'Country', 'pr_my_plugin' )?></label>
</th>
<td>
<input type="text" id="meta_country" name="meta_country" value="<?php echo @get_post_meta($post->ID, 'meta_country', true); ?>" />
</td>
</tr>
<tr valign="top">
<th class="metabox_label_column">
<label for="meta_fburl" class="pr-myplugin-admin"><?php _e( 'Facebook URL', 'pr_my_plugin' )?></label>
</th>
<td>
<input type="text_url" id="meta_fburl" name="meta_fburl" value="<?php echo @get_post_meta($post->ID, 'meta_fburl', true); ?>" />
</td>
</tr>
<tr valign="top">
<th class="metabox_label_column">
<label for="meta_twitterurl" class="pr-myplugin-admin"><?php _e( 'Twitter URL', 'pr_my_plugin' )?></label>
</th>
<td>
<input type="text_url" id="meta_twitterurl" name="meta_twitterurl" value="<?php echo @get_post_meta($post->ID, 'meta_twitterurl', true); ?>" />
</td>
</tr>
<tr valign="top">
<th class="metabox_label_column">
<label for="meta_website" class="pr-myplugin-admin"><?php _e( 'Website URL', 'pr_my_plugin' )?></label>
</th>
<td>
<input type="text_url" id="meta_website" name="meta_website" value="<?php echo @get_post_meta($post->ID, 'meta_website', true); ?>" />
</td>
</tr>
<tr valign="top">
<th class="metabox_label_column">
<label for="meta_e" class="pr-myplugin-admin"><?php _e( 'Meta E', 'pr_my_plugin' )?></label>
</th>
<td>
<input type="text" name="meta_e" id="meta_e" value="<?php echo @get_post_meta($post->ID, 'meta_e', true); ?>" />
</td>
</tr>
<tr valign="top">
<th class="metabox_label_column">
<label for="meta_f" class="pr-myplugin-admin"><?php _e( 'Meta F', 'pr_my_plugin' )?></label>
</th>
<td>
<select name="meta_f" id="meta_f">
<option value="red" <?php selected( $selected, 'red' ); ?>>Red</option>
<option value="blue" <?php selected( $selected, 'blue' ); ?>>Blue</option>
</select>
</td>
</tr>
<tr valign="top">
<th class="metabox_label_column">
<label for="meta_g" class="pr-myplugin-admin"><?php _e( 'Meta G', 'pr_my_plugin' )?></label>
</th>
<td>
<input type="checkbox" id="meta_g" name="meta_g" <?php checked( $check, 'on' ); ?> />
</td>
</tr>
</table>