Return to Snippet

Revision: 31209
at September 1, 2010 06:11 by norcross


Initial Code
function custom_event_type_css() {
	// This makes sure that the posinioning is also good for right-to-left languages
	$x = ( 'rtl' == get_bloginfo( 'text_direction' ) ) ? 'left' : 'right';

	echo "
	<style type='text/css'>
	#future .form-table td input{margin:0 5px 0 10px;}
	#time .form-table .timestuff {float:left; width:65%;}
	#time .form-table tr span.timefield {float:left;}
	#time .form-table tr span.timeselect {float:right;}
	#date .form-table .timestuff {float:left; width:85%;}
	#date .form-table tr span.timefield {float:left;}
	#date .form-table tr span.timeselect {float:right;}
	#date .form-table tr span.timeselect select {width:130px;} 
	}
	</style>
	";
}

add_action('admin_head', 'custom_event_type_css');



add_action( 'init', 'headline_post_types' );

function headline_post_types() {
	register_post_type( 'headline_list',
		array(
			'labels' => array(
				'name' => __( 'Headlines' ),
				'singular_name' => __( 'Headline' ),
				'add_new' => __( 'Add New' ),
				'add_new_item' => __( 'Add New Headline' ),
				'edit' => __( 'Edit' ),
				'edit_item' => __( 'Edit Headline' ),
				'new_item' => __( 'New Headline' ),
				'view' => __( 'View Headline' ),
				'view_item' => __( 'View Headline' ),
				'search_items' => __( 'Search Headline' ),
				'not_found' => __( 'No headline found' ),
				'not_found_in_trash' => __( 'No headline found in Trash' ),
			),
			'public' => true,
			'show_ui' => true,
			'publicly_queryable' => true,
			'exclude_from_search' => false,
			'menu_position' => 5,
			'capability_type' => 'post',			
			'menu_icon' => get_stylesheet_directory_uri() . '/images/headlines_menu.png',
			'query_var' => true,
			'rewrite' => array( 'slug' => 'headline', 'with_front' => false ),
			'supports' => array('title', 'author', 'revisions', 'custom-fields' ),
		)
	);
}



$meta_boxes = array();

//Headline Info Box


$meta_boxes[] = array(
    'id' => 'summary',
    'title' => 'Headline Summary',
    'pages' => array('headline_list'), // custom post type
	'context' => 'normal',
    'priority' => 'high',
    'fields' => array(
        array(
            'name' => 'Headline Summary',
            'desc' => 'Enter a brief summary of the headline',
            'id' => $prefix . 'headline_summ',
            'type' => 'textarea_sm',
            'std' => ''
        )
    )
);

// Headline Link Box
$meta_boxes[] = array(
    'id' => 'link',
    'title' => 'Headline Link',
    'pages' => array('headline_list'), // custom post type
	'context' => 'normal',
    'priority' => 'high',
    'fields' => array(
        array(
            'name' => 'Headline Link',
            'desc' => 'Enter the link to the headline',
            'id' => $prefix . 'event_link',
            'type' => 'text',
            'std' => ''
        )
    )
);

// Headline Source Box
$meta_boxes[] = array(
    'id' => 'source',
    'title' => 'Headline Source',
    'pages' => array('headline_list'), // custom post type
	'context' => 'normal',
    'priority' => 'high',
    'fields' => array(
        array(
            'name' => 'Headline Source',
            'desc' => 'Enter the source of the headline (i.e. newspaper, magazine, etc)',
            'id' => $prefix . 'event_source',
            'type' => 'text',
            '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="10" style="width:97%">', $meta ? $meta : $field['std'], '</textarea>',
						'<br />', $field['desc'];
					break;
				case 'textarea_sm':
					echo '<textarea name="', $field['id'], '" id="', $field['id'], '" cols="60" rows="2" style="width:97%">', $meta ? $meta : $field['std'], '</textarea>',
						'<br />', $field['desc'];
					break;					
				case 'selecttime':
					echo '<div class="timestuff">','<span class="timefield">',$field['desc'],'</span>', '<span class="timeselect">', '<select name="', $field['id'], '" id="', $field['id'], '">','</span>','</div>';
					foreach ($field['options'] as $option) {
						echo '<option', $meta == $option ? ' selected="selected"' : '', '>', $option, '</option>';
					}
					echo '</select>';
					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"' : '', ' />', $field['desc'];
					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);
			}
		}
	}
}

?>

Initial URL

                                

Initial Description

                                

Initial Title
Custom Post Type

Initial Tags

                                

Initial Language
PHP