Forums

The forums ran from 2008-2020 and are now closed and viewable here as an archive.

Home Forums Back End [WP] – Custom Post Types Custom Fields Re: [WP] – Custom Post Types Custom Fields

#79886

I figured out how to have a different meta box for each post type. I just need to figure out how to rewrite the permalink…

here is the code:

Code:
// Create New Post Types

add_action(‘init’, ‘create_message_post_type’);
add_action(‘init’, ‘create_game_post_type’);
add_action(‘init’, ‘create_player_post_type’);

function create_message_post_type() {
register_post_type (
‘messages’, array (
‘labels’ => array (
‘name’ => ‘Messages’,
‘singular_name’ => ‘Message’,
‘add_new’ => ‘Add New Message’,
‘add_new_item’ => ‘Add New Message’,
‘edit’ => ‘Edit Message’,
‘edit_item’ => ‘Edit Message’,
‘new_item’ => ‘New Message’,
‘view’ => ‘View Message’,
‘view_item’ => ‘View Message’,
‘search_items’ => ‘Search Messages’,
‘not_found’ => ‘No messages found’,
‘not_found_in_trash’ => ‘No messages found in Trash’,
),
‘supports’ => array (
‘title’,
‘editor’,
‘thumbnail’,
‘excerpts’,
‘comments’,
‘revisions’
),
‘public’ => true,
‘rewrite’ => false,
‘query_var’ => true,
‘menu_position’ => 4
)
);
}

function create_game_post_type() {
register_post_type (
‘games’, array (
‘labels’ => array (
‘name’ => ‘Games’,
‘singular_name’ => ‘Game’,
‘add_new’ => ‘Add New Game’,
‘add_new_item’ => ‘Add New Game’,
‘edit’ => ‘Edit Games’,
‘edit_item’ => ‘Edit Game’,
‘new_item’ => ‘New Game’,
‘view’ => ‘View Game’,
‘view_item’ => ‘View Game’,
‘search_items’ => ‘Search Games’,
‘not_found’ => ‘No games found’,
‘not_found_in_trash’ => ‘No games found in Trash’,
),
‘supports’ => array (
‘title’,
‘editor’,
‘thumbnail’,
‘excerpts’,
‘comments’,
‘revisions’
),
‘public’ => true,
‘rewrite’ => false,
‘query_var’ => true,
‘menu_position’ => 5
)
);
}

function create_player_post_type() {
register_post_type (
‘players’, array (
‘labels’ => array (
‘name’ => ‘Roster’,
‘singular_name’ => ‘Player’,
‘add_new’ => ‘Add New Player’,
‘add_new_item’ => ‘Add New Player’,
‘edit’ => ‘Edit Roster’,
‘edit_item’ => ‘Edit Player’,
‘new_item’ => ‘New Player’,
‘view’ => ‘View Player’,
‘view_item’ => ‘View Player’,
‘search_items’ => ‘Search Players’,
‘not_found’ => ‘No players found’,
‘not_found_in_trash’ => ‘No players found in Trash’,
),
‘supports’ => array (

),
‘public’ => true,
‘rewrite’ => false,
‘query_var’ => true,
‘menu_position’ => 6
)
);
}

// Create Taximonies
add_action( ‘init’, ‘create_team_taxonomies’ );
function create_team_taxonomies() {
register_taxonomy (
‘teams’,
array( ‘messages’,’games’,’players’ ),
array (
‘labels’ => array (
‘name’ => ‘Teams’,
‘singular_name’ => ‘Team’,
‘search_items’ => ‘Search Teams’,
‘popular_items’ => ‘Popular Teams’,
‘all_items’ => ‘All Teams’,
‘parent_item’ => ‘Parent Teams’,
‘parent_item_colon’ => ‘Parent Teams:’,
‘edit_item’ => ‘Edit Team’,
‘update_item’ => ‘Update Team’,
‘add_new_item’ => ‘Add New Team’,
‘new_item_name’ => ‘New Team Name’
),
‘hierarchical’ => true,
‘public’ => true,
‘show_ui’ => true,
‘query_var’ => ”,
‘show_tagcloud’ => true,
)
);
}

//Create Meta Boxes

$prefix = ‘dbt_’;

$meta_boxes = array();

// first meta box
$meta_boxes[] = array(
‘id’ => ‘player_details’,
‘title’ => ‘Player Details’,
‘pages’ => array(‘players’), // multiple post types
‘context’ => ‘normal’,
‘priority’ => ‘high’,
‘fields’ => array(
array(
‘name’ => ‘First Name’,
‘desc’ => ”,
‘id’ => $prefix . ‘text’,
‘type’ => ‘text’, // text box
‘std’ => ”
),
array(
‘name’ => ‘Last Name’,
‘desc’ => ”,
‘id’ => $prefix . ‘last_name’,
‘type’ => ‘text’, // text box
‘std’ => ”
),
array(
‘name’ => ‘Height’,
‘desc’ => ”,
‘id’ => $prefix . ‘height’,
‘type’ => ‘text’, // text box
‘std’ => ”
),
array(
‘name’ => ‘Weight’,
‘desc’ => ”,
‘id’ => $prefix . ‘weight’,
‘type’ => ‘text’, // text box
‘std’ => ”
),
array(
‘name’ => ‘Number’,
‘desc’ => ”,
‘id’ => $prefix . ‘number’,
‘type’ => ‘text’, // text box
‘std’ => ”
),
array(
‘name’ => ‘Position’,
‘desc’ => ”,
‘id’ => $prefix . ‘position’,
‘type’ => ‘text’, // text box
‘std’ => ”
),
array(
‘name’ => ‘Grade’,
‘id’ => $prefix . ‘select’,
‘type’ => ‘select’, // select box
‘options’ => array(‘Please select grade’, ‘Freshman’, ‘Sophomore’, ‘Junior’, ‘Senior’, ‘Graduate’) // array of options for select box
),
array(
‘name’ => ‘Bio’,
‘desc’ => ”,
‘id’ => $prefix . ‘bio’,
‘type’ => ‘textarea’, // text area
‘std’ => ‘Insert players biography here...’
),
array(
‘name’ => ‘Photo’,
‘desc’ => ”,
‘id’ => $prefix . ‘photo’,
‘type’ => ‘image’ // image upload
),

)
);

// second meta box
$meta_boxes[] = array(
‘id’ => ‘game_details’,
‘title’ => ‘Game Details’,
‘pages’ => array(‘games’), // custom post types, since WordPress 3.0
‘context’ => ‘normal’,
‘priority’ => ‘high’,
‘fields’ => array(
array(
‘name’ => ‘Opponent’,
‘desc’ => ”,
‘id’ => $prefix . ‘opponent’,
‘type’ => ‘text’, // text box
‘std’ => ”
),
array(
‘name’ => ‘Game Date’,
‘desc’ => ”,
‘id’ => $prefix . ‘date’,
‘type’ => ‘text’, // text box
‘std’ => ”
),
array(
‘name’ => ‘Time’,
‘desc’ => ”,
‘id’ => $prefix . ‘time’,
‘type’ => ‘text’, // text box
‘std’ => ”
),
array(
‘name’ => ‘Home / Away’,
‘id’ => $prefix . ‘home_away’,
‘type’ => ‘radio’, // radio box
‘options’ => array( // array of name, value pairs for radio options
array(‘name’ => ‘Home’, ‘value’ => ‘Home’),
array(‘name’ => ‘Away’, ‘value’ => ‘Away’)
)
),
array(
‘name’ => ‘City’,
‘desc’ => ”,
‘id’ => $prefix . ‘city’,
‘type’ => ‘text’, // text box
‘std’ => ”
),
array(
‘name’ => ‘State’,
‘desc’ => ”,
‘id’ => $prefix . ‘state’,
‘type’ => ‘text’, // text box
‘std’ => ”
),
array(
‘name’ => ‘Oak’s Score’,
‘desc’ => ”,
‘id’ => $prefix . ‘oak_score’,
‘type’ => ‘text’, // text box
‘std’ => ”
),
array(
‘name’ => ‘Opponent’s Score’,
‘desc’ => ”,
‘id’ => $prefix . ‘opp_score’,
‘type’ => ‘text’, // text box
‘std’ => ”
),
)
);

/*********************************

You should not edit the code below

*********************************/

foreach ($meta_boxes as $meta_box) {
$my_box = new My_meta_box($meta_box);
}

class My_meta_box {

protected $_meta_box;

// create meta box based on given data
function __construct($meta_box) {
if (!is_admin()) return;

$this->_meta_box = $meta_box;

// fix upload bug: http://www.hashbangcode.com/blog/add-enctype-wordpress-post-and-page-forms-471.html
$current_page = substr(strrchr($_SERVER[’PHP_SELF’], ‘/’), 1, -4);
if ($current_page == ‘page’ || $current_page == ‘page-new’ || $current_page == ‘post’ || $current_page == ‘post-new’) {
add_action(‘admin_head’, array(&$this, ‘add_post_enctype’));
}

add_action(‘admin_menu’, array(&$this, ‘add’));

add_action(‘save_post’, array(&$this, ‘save’));
}

function add_post_enctype() {
echo ‘
<script type="text/javascript">
jQuery(document).ready(function(){
jQuery("#post").attr("enctype", "multipart/form-data");
jQuery("#post").attr("encoding", "multipart/form-data");
});
</script>’;
}

/// Add meta box for multiple post types
function add() {
foreach ($this->_meta_box[’pages’] as $page) {
add_meta_box($this->_meta_box[’id’], $this->_meta_box[’title’], array(&$this, ‘show’), $page, $this->_meta_box[’context’], $this->_meta_box[’priority’]);
}
}

// Callback function to show fields in meta box
function show() {
global $post;

// Use nonce for verification
echo ‘<input type="hidden" name="mytheme_meta_box_nonce" value="’, wp_create_nonce(basename(__FILE__)), ‘" />’;

echo ‘<table class="form-table">’;

foreach ($this->_meta_box[’fields’] as $field) {
// get current post meta data
$meta = get_post_meta($post->ID, $field[’id’], true);

echo ‘<tr>’,
‘<th style="width:20%"><label for="’, $field[’id’], ‘">’, $field[’name’], ‘</label></th>’,
‘<td>’;
switch ($field[’type’]) {
case ‘text’:
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 ‘textarea’:
echo ‘<textarea name="’, $field[’id’], ‘" id="’, $field[’id’], ‘" cols="60" rows="4" style="width:97%">’, $meta ? $meta : $field[’std’], ‘</textarea>’,
‘<br />’, $field[’desc’];
break;
case ‘select’:
echo ‘<select name="’, $field[’id’], ‘" id="’, $field[’id’], ‘">’;
foreach ($field[’options’] as $option) {
echo ‘<option’, $meta == $option ? ‘ selected="selected"’ : ”, ‘>’, $option, ‘</option>’;
}
echo ‘</select>’;
break;
case ‘radio’:
foreach ($field[’options’] as $option) {
echo ‘<input type="radio" name="’, $field[’id’], ‘" value="’, $option[’value’], ‘"’, $meta == $option[’value’] ? ‘ checked="checked"’ : ”, ‘ />’, $option[’name’];
}
break;
case ‘checkbox’:
echo ‘<input type="checkbox" name="’, $field[’id’], ‘" id="’, $field[’id’], ‘"’, $meta ? ‘ checked="checked"’ : ”, ‘ />’;
break;
case ‘file’:
echo $meta ? "$meta<br />" : ”, ‘<input type="file" name="’, $field[’id’], ‘" id="’, $field[’id’], ‘" />’,
‘<br />’, $field[’desc’];
break;
case ‘image’:
echo $meta ? "<img src="$meta" width="150" height="150" /><br />$meta<br />" : ”, ‘<input type="file" name="’, $field[’id’], ‘" id="’, $field[’id’], ‘" />’,
‘<br />’, $field[’desc’];
break;
}
echo ‘<td>’,
‘</tr>’;
}

echo ‘</table>’;
}

// Save data from meta box
function save($post_id) {
// verify nonce
if (!wp_verify_nonce($_POST[’mytheme_meta_box_nonce’], basename(__FILE__))) {
return $post_id;
}

// check autosave
if (defined(‘DOING_AUTOSAVE’) && DOING_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 ($this->_meta_box[’fields’] as $field) {
$name = $field[’id’];

$old = get_post_meta($post_id, $name, true);
$new = $_POST[$field[’id’]];

if ($field[’type’] == ‘file’ || $field[’type’] == ‘image’) {
$file = wp_handle_upload($_FILES[$name], array(‘test_form’ => false));
$new = $file[’url’];
}

if ($new && $new != $old) {
update_post_meta($post_id, $name, $new);
} elseif (” == $new && $old && $field[’type’] != ‘file’ && $field[’type’] != ‘image’) {
delete_post_meta($post_id, $name, $old);
}
}
}
}

Oh and can I change "Enter Title Here" in the title box, to "Enter Headline Here" for a specific post type?