/ Published in: PHP
Expand |
Embed | Plain Text
<?php add_action( 'init', 'create_product' ); function create_product() { 'name' => _x('Products', 'post type general name'), 'singular_name' => _x('Product', 'post type singular name'), 'add_new' => _x('Add New', 'Product'), 'add_new_item' => __('Add New Product'), 'edit_item' => __('Edit Product'), 'new_item' => __('New Product'), 'view_item' => __('View Product'), 'search_items' => __('Search Products'), 'not_found' => __('No Products found'), 'not_found_in_trash' => __('No Products found in Trash'), 'parent_item_colon' => '' ); register_post_type( 'product', 'labels' => $labels, 'public' => true, 'supports' => $supports ) ); } ?> <?php 'id' => 'my-meta-box', 'title' => 'Product Options', 'page' => 'product', 'context' => 'normal', 'priority' => 'high', 'name' => 'Price', 'desc' => 'Enter the price of the product starting with $.', 'id' => 'price', 'type' => 'text', 'std' => '' ), 'name' => 'Dimensions', 'desc' => 'Enter the dimensions of the product.', 'id' => 'dimensions', 'type' => 'text', 'std' => '' ), 'name' => 'Colors', 'desc' => 'Enter to colors seperating each value.', 'id' => 'colors', 'type' => 'text', 'std' => '' ), 'name' => 'Description', 'desc' => 'Enter the description of the product here, to enter the details, write it in the large text box underneath the title.', 'id' => 'description', 'type' => 'textarea', 'std' => '' ) ) ); add_action('admin_menu', 'mytheme_add_box'); // Add meta box function mytheme_add_box() { global $meta_box; add_meta_box($meta_box['id'], $meta_box['title'], 'mytheme_show_box', $meta_box['page'], $meta_box['context'], $meta_box['priority']); } // Callback function to show fields in meta box function mytheme_show_box() { // Use nonce for verification echo '<table class="form-table">'; foreach ($meta_box['fields'] as $field) { // get current post meta data $meta = get_post_meta($post->ID, $field['id'], true); '<th style="width:20%"><label for="', $field['id'], '">', $field['name'], '</label></th>', '<td>'; switch ($field['type']) { case 'price': echo '<input type="text" name="', $field['id'], '" id="', $field['id'], '" value="', $meta ? $meta : $field['std'], '" size="30" style="width:97%" />', '<br />', $field['desc']; break; case 'dimensions': echo '<input type="text" name="', $field['id'], '" id="', $field['id'], '" value="', $meta ? $meta : $field['std'], '" size="30" style="width:97%" />', '<br />', $field['desc']; break; case 'colors': echo '<input type="text" name="', $field['id'], '" id="', $field['id'], '" value="', $meta ? $meta : $field['std'], '" size="30" style="width:97%" />', '<br />', $field['desc']; break; case 'description': echo '<textarea name="', $field['id'], '" id="', $field['id'], '" cols="60" rows="4" style="width:97%">', $meta ? $meta : $field['std'], '</textarea>', '<br />', $field['desc']; break; } '</tr>'; } echo '</table>'; } add_action('save_post', 'mytheme_save_data'); // Save data from meta box function mytheme_save_data($post_id) { global $meta_box; // verify nonce return $post_id; } // check autosave return $post_id; } // check permissions if ('page' == $_POST['post_type']) { if (!current_user_can('edit_page', $post_id)) { return $post_id; } } elseif (!current_user_can('edit_post', $post_id)) { return $post_id; } foreach ($meta_box['fields'] as $field) { $old = get_post_meta($post_id, $field['id'], true); $new = $_POST[$field['id']]; if ($new && $new != $old) { update_post_meta($post_id, $field['id'], $new); } elseif ('' == $new && $old) { delete_post_meta($post_id, $field['id'], $old); } } } ?>
You need to login to post a comment.
