Return to Snippet

Revision: 60287
at October 30, 2012 11:53 by eridesigns


Initial Code
<?php

function add_post_type($name, $args = array()) {
	add_action('init', function() use($name, $args) {
	
		    $upper = ucwords($name);
			$name = strtolower(str_replace(' ','_',$name));

			$args = array_merge(
			
			array(
			'public'=> true,
			'label' => "All $upper" . 's',
			'labels' => array('add_new_item' => "Add New $upper"),
			'support' => array('title', 'editor', 'author', 'thumbnail', 'excerpt', 'comments', 'custom-fields'),
			'taxonomies' => array('post_tag','category')
			),
			
			$args
			
			);
			
			register_post_type('$name', $args);
		});
			
}


//now we create and register a taxonomy
function add_taxonomy($name, $post_type, $args = array()) {
	$name = strtolower($name);
	
	add_action('init', function() use($name, $post_type, $args) {
			$args = array_merge(
				array(
				'label' => ucwords($name),
				
				),
				$args
			);
				register_taxonomy($name, $post_type, $args);
	}); 
}

/************************************************************
  Now we add the names of the custom post type and taxonomies
*************************************************************/


add_post_type('snippet', array(
			'supports' => array('title', 'editor', 'author', 'thumbnail', 'excerpt', 'comments', 'custom-fields'),
			'taxonomies' => array('post_tag')
));


add_taxonomy('language', 'snippet');

/************************************************************
  Creating Metaboxes
*************************************************************/

add_action('add_meta_boxes', function() {
	add_meta_box(
		'er_snippet_info',
		'Snippet Info',
		'er_snippet_info_cb',
		'snippet',
		'normal',
		'high'
	);
});

function er_snippet_info_cb() {
	global $post;
	$url = get_post_custom($post->ID); 
	 
	?>
	
	<label for="er_associated_url">Associated URL: </label>
	<input type="text" name="er_associated_url" id="er_associated_url" value="<?php echo $url; ?>" />
	
	<?php
}
	add_action('save_post', function () {
		global $post; 
		if (defined('DOING_AUTOSAVE') && DOING_AUTOSAVE ) return;
		//security check 
		if ($_POST && !wp_verify_nonce($_POST['er_nonce'], _FILE_)) {
				if ( isset ($_POST['er_associated_url']) ) {
			update_post_meta($post->ID, 'er_associated_url', $_POST['er_associated_url']);  
			}
	   }
		
	});
?>

Initial URL


Initial Description
Does not work at the moment. So don't copy it.

Initial Title
Custom Post Type with a Metabox field

Initial Tags
post

Initial Language
PHP