johnbocook
9/29/2013 - 10:06 PM

Registers a wordpress Custom Post Type for Events and create's a metabox for event time.

Registers a wordpress Custom Post Type for Events and create's a metabox for event time.

<?php
/*
Plugin Name: City Events
Plugin URI: http://JohnBocook.com
Description: Registers a Custom Post Type for Events and create's a metabox for event time.
Version: 1.0
Author: John Bocook
Author URI: http://johnbocook.com
License: Public Domain
*/



/* Register & Setup Event Post Type */

function jbocook_event_posttype() {

  $labels = array(
		'name'                => _x( 'Events', 'Post Type General Name', 'text_domain' ),
		'singular_name'       => _x( 'Event', 'Post Type Singular Name', 'text_domain' ),
		'menu_name'           => __( 'Events', 'text_domain' ),
		'parent_item_colon'   => __( 'Parent Event', 'text_domain' ),
		'all_items'           => __( 'All Events', 'text_domain' ),
		'view_item'           => __( 'View Event', 'text_domain' ),
		'add_new_item'        => __( 'Add New Event', 'text_domain' ),
		'add_new'             => __( 'New Event', 'text_domain' ),
		'edit_item'           => __( 'Edit Event', 'text_domain' ),
		'update_item'         => __( 'Update Event', 'text_domain' ),
		'search_items'        => __( 'Search Events', 'text_domain' ),
		'not_found'           => __( 'No Events Found', 'text_domain' ),
		'not_found_in_trash'  => __( 'No Events in Trash', 'text_domain' ),
	);
	$rewrite = array(
		'slug'                => 'custom-slug',
		'with_front'          => true,
		'pages'               => true,
		'feeds'               => true,
	);
	$args = array(
		'label'               => __( 'event', 'text_domain' ),
		'description'         => __( 'City Events', 'text_domain' ),
		'labels'              => $labels,
		'supports'            => array( 'title', 'author', 'thumbnail', 'revisions', ),
		'taxonomies'          => array( 'event' ),
		'hierarchical'        => false,
		'public'              => true,
		'show_ui'             => true,
		'show_in_menu'        => true,
		'show_in_nav_menus'   => true,
		'show_in_admin_bar'   => true,
		'menu_position'       => 5,
		'menu_icon'           => '/eventicon.png',
		'can_export'          => true,
		'has_archive'         => true,
		'exclude_from_search' => false,
		'publicly_queryable'  => true,
		'query_var'           => 'gcevents',
		'rewrite'             => $rewrite,
		'capability_type'     => 'page',
	);
	register_post_type( 'jbocook_post_event', $args );

}

// Hook into the 'init' action
add_action( 'init', 'jbocook_event_posttype', 0 );




/* Add Custom Meta Boxes */
// important: note the priority of 99, the js needs to be placed after tinymce loads

add_action( 'add_meta_boxes', 'jbocook_event_post_meta_add' );
function jbocook_event_post_meta_add()
{
	add_meta_box( 'jbocook-event-post-id', 'Event Information', 'jbocook_event_post_meta', 'jbocook_post_event', 'normal', 'high' );
}

function jbocook_event_post_meta( $post )
{
	$values = get_post_custom( $post->ID );
	$eventloc = isset( $values['jbocook_event_loc'] ) ? esc_attr( $values['jbocook_event_loc'][0] ) : '';
	$eventvenue = isset( $values['jbocook_event_venue'] ) ? esc_attr( $values['jbocook_event_venue'][0] ) : '';
	$eventlink = isset( $values['jbocook_event_link'] ) ? esc_attr( $values['jbocook_event_link'][0] ) : '';
	$eventcost = isset( $values['jbocook_event_cost'] ) ? esc_attr( $values['jbocook_event_cost'][0] ) : '';
	$eventdesc = isset( $values['jbocook_event_desc'] ) ? esc_attr( $values['jbocook_event_desc'][0] ) : '';
	wp_nonce_field( 'jbocook_event_post_nonce', 'meta_box_nonce' );
	?>
	<style>
	#jbocook_events input {width:200px; font-size:14px;}
	#visibility {display:none;}
	.misc-yoast {display:none;}

	</style>

	<div id="jbocook_events" style="display:inline-block;width:45%;vertical-align:top;">
		<p>
			<input type="text" name="jbocook_event_cost" id="jbocook_event_cost" value="<?php echo $eventcost; ?>" placeholder="Optional: Event Cost"/>
		</p>
		<p>
			<input type="text" name="jbocook_event_venue" id="jbocook_event_venue" value="<?php echo $eventvenue; ?>" placeholder="Venue Name"/>
		</p>
		<p>
			<input type="text" name="jbocook_event_loc" id="jbocook_event_loc" value="<?php echo $eventloc; ?>" placeholder="Location/Address"/>
		</p>
		<p>
			<input type="text" name="jbocook_event_link" id="jbocook_event_link" value="<?php echo $eventlink; ?>" placeholder="Optional: Offsite Event Link"/>
		</p>
		
	</div>
	<div style="display:inline-block;width:45%;">
		<p>
			
			<textarea name="jbocook_event_desc" id="jbocook_event_desc" value="<?php echo $eventdesc; ?>" placeholder="Event Description" rows="7" style="width:100%; font-size:14px;"><?php echo $eventdesc; ?></textarea></div>
		</p>
	</div>

	<script>
	jQuery( document ).ready(function() {
		jQuery( "#submitdiv h3.hndle span" ).replaceWith('Event Date/Time');

		setTimeout(function(){ 
        jQuery('#submitdiv #timestamp').replaceWith('<span id="timestamp" style="font-weight:bold; color:red;">Set Day and Time</span>')}, 2000); 


	});
	</script>
	<?php	
}


add_action( 'save_post', 'jbocook_event_post_meta_save' );
function jbocook_event_post_meta_save( $post_id )
{
	// Bail if we're doing an auto save
	if( defined( 'DOING_AUTOSAVE' ) && DOING_AUTOSAVE ) return;
	
	// if our nonce isn't there, or we can't verify it, bail
	if( !isset( $_POST['meta_box_nonce'] ) || !wp_verify_nonce( $_POST['meta_box_nonce'], 'jbocook_event_post_nonce' ) ) return;
	
	// if our current user can't edit this post, bail
	if( !current_user_can( 'edit_post' ) ) return;
	
	// now we can actually save the data
	$allowed = array( 
		'a' => array( // on allow a tags
			'href' => array() // and those anchords can only have href attribute
		)
	);
	
	// Probably a good idea to make sure your data is set
	if( isset( $_POST['jbocook_event_cost'] ) )
		update_post_meta( $post_id, 'jbocook_event_cost', wp_kses( $_POST['jbocook_event_cost'], $allowed ) );

	if( isset( $_POST['jbocook_event_venue'] ) )
		update_post_meta( $post_id, 'jbocook_event_venue', wp_kses( $_POST['jbocook_event_venue'], $allowed ) );

	if( isset( $_POST['jbocook_event_loc'] ) )
		update_post_meta( $post_id, 'jbocook_event_loc', wp_kses( $_POST['jbocook_event_loc'], $allowed ) );

	if( isset( $_POST['jbocook_event_link'] ) )
		update_post_meta( $post_id, 'jbocook_event_link', wp_kses( $_POST['jbocook_event_link'], $allowed ) );

	if( isset( $_POST['jbocook_event_desc'] ) )
		update_post_meta( $post_id, 'jbocook_event_desc', wp_kses( $_POST['jbocook_event_desc'], $allowed ) );

}

/* Style Custom Post Type Edit Screen Layout */



?>