Reputation: 9314
Not sure what've I've done wrong, probably something really silly like I've missed an attribute or something.
Right I'm trying to build a feature on a theme that displays on the homepage. That bit I can do just fine. What I want to do is make this a little easier for the user. So they click image slider on the left hand side, add a title, add a description and! An image from the media library.
So far, to simplify my idea, I just want to add a custom meta field, as far as I understand, so the user can add an image address.
My problem is this meta box is not displaying in the admin panel and I'm not sure why? I've been reading and follow serval tutorials etc. Just can't seem to get it to display? Can anybody see anything wrong?
Here's what I've built
function post_support(){
add_theme_support('post-formats',array('aside','Image Slider'));
}
/**
* create slider, adding post register
*/
function slider_create_slider(){
register_post_type('Image Slider',
array('labels'=>
array('name'=>__('Image slider'),
'singular_name'=>__('Image slider'),
'add_new'=>__('Add New slider object'),
'edit_item'=>__('Edit Slide object'),
'new_item'=>__('Add New Slide object'),
'view_item'=>__('View slide object'),
'search_items'=>__('Search Slide Objects'),
'not_found'=>__('No Slide objects found'),
'not_found_in_trash'=>__('No slide Objects found in the bin.')),
'public'=>true,
'show_ui'=>true,
'capibility_type'=>'post','hierarchical'=>false,'rewrite'=>true,'menu_position'=>20,'supports'=>array('title','editor','thumbnail')));
}
/**
* adding slider's meta boxes
*/
function slider_add_meta_boxes($post){
//Only need to add image meta
add_meta_box('ImageSliderMeta',__('image'),'slider_image_meta_box',__('image'),'side','default');
}
function slider_image_meta_box(){
$image=get_post_meta($post->ID,'ImageSliderMeta',true);
//only testing atm
?>
<label>Image (url)</label><input name="ImageSliderMeta" value="<?echo$image;?>"/>
?>
}
function save_image_meta_box(){
global$post;
update_post_meta($post->ID,'ImageSliderMeta',$_POST['ImageSliderMeta']);
}
add_action('after_setup_theme','post_support');
add_action('init','slider_create_slider');
add_action('add_meta_boxes','slider_add_meta_boxes');
add_action('save_post','save_image_meta_box');
Is it because I've structured my add_action
's incorrectly?
Upvotes: 0
Views: 866
Reputation: 517
Update
Your add_meta_box function is not passing the correct arguments.
Argument #4, $screen is the post types you want to show the meta boxes for. Use the following;
function slider_create_slider(){
register_post_type('imageslider'
...
}
function slider_add_meta_boxes($post)
{
add_meta_box('ImageSliderMeta',__('image'),'slider_image_meta_box', 'imageslider');
}
http://codex.wordpress.org/Function_Reference/add_meta_box
Upvotes: 2
Reputation: 1016
I did it a few times. First, you must create the post type. In your plugin file, you must require
the files, that define your custom post types and meta boxes. In your meta box file, you mostly generate a HTML code of the form element and add a function, that updates the post meta. I will give you an example (options_myplugin.php, required in the main file):
function myplugin_staff_meta_box_add() {
add_meta_box('myplugin-staff-edit', 'Staff Social Meta Box', 'myplugin_meta_box_staff_cb', 'staffs', 'normal', 'high');
}
function myplugin_meta_box_staff_cb($post) {
$position = get_post_meta($post->ID, 'position', true);
$facebook = get_post_meta($post->ID, 'facebook', true);
$twitter = get_post_meta($post->ID, 'twitter', true);
$gplus = get_post_meta($post->ID, 'gplus', true);
$linkedin = get_post_meta($post->ID, 'linkedin', true);
wp_nonce_field('staff_meta_box_nonce', 'staff_meta_box_nonce');
?>
<p class="myplugin_option_box">
<label for="position" class="myplugin-desc"><?php _e('Position', 'myplugin-nictitate-toolkit'); ?>:</label>
<input id="position" type="text" name="position"
class="myplugin-option-input" value="<?php echo $position; ?>">
<span>Ex: Project Manager</span>
</p>
<p class="myplugin_option_box">
<label for="facebook" class="myplugin-desc"><?php _e('Facebook', 'myplugin-nictitate-toolkit'); ?>:</label>
<input id="facebook" type="text" name="facebook"
class="myplugin-option-input" value="<?php echo $facebook; ?>">
<span>Ex: http://facebook.com/myplugintheme</span>
</p>
<p class="myplugin_option_box">
<label for="twitter" class="myplugin-desc"><?php _e('Twitter', 'myplugin-nictitate-toolkit'); ?>:</label>
<input id="twitter" type="text" name="twitter"
class="myplugin-option-input" value="<?php echo $twitter; ?>">
<span>Ex: http://twitter.com/myplugintheme</span>
</p>
<p class="myplugin_option_box">
<label for="gplus" class="myplugin-desc"><?php _e('Google Plus', 'myplugin-nictitate-toolkit'); ?>:</label>
<input id="gplus" type="text" name="gplus"
class="myplugin-option-input" value="<?php echo $gplus; ?>">
<span>Ex: http://plus.google.com/myplugintheme</span>
</p>
<p class="myplugin_option_box">
<label for="linkedin" class="myplugin-desc"><?php _e('LinkedIn', 'myplugin-nictiate-toolkit'); ?></label>
<input id="linkedin" type="text" name="linkedin" class="myplugin-option-input" value="<?php echo $linkedin; ?>"/>
<span>Ex: http://linkedin.com/myplugintheme</span>
</p>
<?php
}
add_action('save_post', 'myplugin_save_staff_data');
function myplugin_save_staff_data($post_id) {
if (defined('DOING_AUTOSAVE') && DOING_AUTOSAVE)
return;
if (!isset($_POST['staff_meta_box_nonce']) || !wp_verify_nonce($_POST['staff_meta_box_nonce'], 'staff_meta_box_nonce'))
return;
if (!current_user_can('edit_post'))
$allowed = array(
'a' => array(
'href' => array()
)
);
if (isset($_POST['position']))
update_post_meta($post_id, 'position', wp_kses($_POST['position'], $allowed));
if (isset($_POST['facebook']))
update_post_meta($post_id, 'facebook', wp_kses($_POST['facebook'], $allowed));
if (isset($_POST['twitter']))
update_post_meta($post_id, 'twitter', wp_kses($_POST['twitter'], $allowed));
if (isset($_POST['gplus']))
update_post_meta($post_id, 'gplus', wp_kses($_POST['gplus'], $allowed));
if (isset($_POST['linkedin']))
update_post_meta($post_id, 'linkedin', wp_kses($_POST['linkedin'], $allowed));
}
From now you will be able to display the meta values via get_post_meta
.
Upvotes: 0