wordcamp montreal 2015: combining custom post types, fields, and meta boxes to do the impossible...

45
COMBINING CUSTOM POST TYPES, FIELDS, AND META BOXES TO DO THE IMPOSSIBLE WITH WORDPRESS

Upload: allilevine

Post on 13-Aug-2015

234 views

Category:

Technology


0 download

TRANSCRIPT

Page 1: WordCamp Montreal 2015: Combining Custom Post Types, Fields, and Meta Boxes to do the Impossible with WordPress

COMBINING CUSTOMPOST TYPES, FIELDS,AND META BOXES TODO THE IMPOSSIBLEWITH WORDPRESS

Page 2: WordCamp Montreal 2015: Combining Custom Post Types, Fields, and Meta Boxes to do the Impossible with WordPress

 

Page 3: WordCamp Montreal 2015: Combining Custom Post Types, Fields, and Meta Boxes to do the Impossible with WordPress

 

Page 4: WordCamp Montreal 2015: Combining Custom Post Types, Fields, and Meta Boxes to do the Impossible with WordPress
Page 5: WordCamp Montreal 2015: Combining Custom Post Types, Fields, and Meta Boxes to do the Impossible with WordPress
Page 6: WordCamp Montreal 2015: Combining Custom Post Types, Fields, and Meta Boxes to do the Impossible with WordPress
Page 7: WordCamp Montreal 2015: Combining Custom Post Types, Fields, and Meta Boxes to do the Impossible with WordPress
Page 8: WordCamp Montreal 2015: Combining Custom Post Types, Fields, and Meta Boxes to do the Impossible with WordPress
Page 9: WordCamp Montreal 2015: Combining Custom Post Types, Fields, and Meta Boxes to do the Impossible with WordPress
Page 10: WordCamp Montreal 2015: Combining Custom Post Types, Fields, and Meta Boxes to do the Impossible with WordPress

function create_custom_post_type() {

register_post_type( 'sem_event', everything else );

}

Page 11: WordCamp Montreal 2015: Combining Custom Post Types, Fields, and Meta Boxes to do the Impossible with WordPress

array(

'labels' => array( 'name' => __( 'Events', 'SEM' ), ...),'rewrite' => array( 'slug' => 'event'),'has_archive' => 'events','supports' => array( 'title', 'editor', 'thumbnail', 'custom-fields')

)

Page 12: WordCamp Montreal 2015: Combining Custom Post Types, Fields, and Meta Boxes to do the Impossible with WordPress

add_action( 'init', 'create_custom_post_type' );

Page 13: WordCamp Montreal 2015: Combining Custom Post Types, Fields, and Meta Boxes to do the Impossible with WordPress

function create_custom_taxonomy() {

register_taxonomy( 'special-event-category', everythingelse)

}

Page 14: WordCamp Montreal 2015: Combining Custom Post Types, Fields, and Meta Boxes to do the Impossible with WordPress

'hierarchical' => true

'hierarchical' => false

Page 15: WordCamp Montreal 2015: Combining Custom Post Types, Fields, and Meta Boxes to do the Impossible with WordPress

add_action( 'init', 'create_custom_taxonomy' );

add_action( 'init', 'create_custom_post_type' );

Page 16: WordCamp Montreal 2015: Combining Custom Post Types, Fields, and Meta Boxes to do the Impossible with WordPress

register_post_type(

'taxonomies' => array('special-event-category', 'special-event-tag')

)

Page 17: WordCamp Montreal 2015: Combining Custom Post Types, Fields, and Meta Boxes to do the Impossible with WordPress
Page 18: WordCamp Montreal 2015: Combining Custom Post Types, Fields, and Meta Boxes to do the Impossible with WordPress
Page 19: WordCamp Montreal 2015: Combining Custom Post Types, Fields, and Meta Boxes to do the Impossible with WordPress
Page 20: WordCamp Montreal 2015: Combining Custom Post Types, Fields, and Meta Boxes to do the Impossible with WordPress

function create_custom_meta_box() {

add_meta_box('special_event_meta', 'Event Date &Time', 'render_special_event_meta_box', 'sem_event','side', 'high');

}

Page 21: WordCamp Montreal 2015: Combining Custom Post Types, Fields, and Meta Boxes to do the Impossible with WordPress

function render_special_event_meta_box() {

require_once plugin_dir_path( __FILE__ ) .'partials/special-event-manager-admin-display.php';

}

Page 22: WordCamp Montreal 2015: Combining Custom Post Types, Fields, and Meta Boxes to do the Impossible with WordPress

<li> <label>Start Date</label> <input name="sem_events_startdate" id="startPicker" value="<?php echo $clean_sd; ?>"</li><li> <label>Start Time</label> <input name="sem_events_starttime" value="<?php echo $clean_st; ?>"</li>

Start Date Sat, Jul 4th, 2015

Start Time 2:00 pm

Page 23: WordCamp Montreal 2015: Combining Custom Post Types, Fields, and Meta Boxes to do the Impossible with WordPress

wp_create_nonceecho '<input type="hidden" name="SEM-events-nonce" id="SEM-events-nonce" wp_create_nonce( 'SEM-events-nonce' ) . '">';

Page 24: WordCamp Montreal 2015: Combining Custom Post Types, Fields, and Meta Boxes to do the Impossible with WordPress

$custom = get_post_custom($post->ID);

isset($custom["sem_events_startdate"][0]) ?

Page 25: WordCamp Montreal 2015: Combining Custom Post Types, Fields, and Meta Boxes to do the Impossible with WordPress

MON, JUN 29TH, 2015date("D, M d, Y", the date);

2:31 PMget_option('time_format');

date($time_format, the date);

Page 26: WordCamp Montreal 2015: Combining Custom Post Types, Fields, and Meta Boxes to do the Impossible with WordPress

function custom_meta_box_save() {

if ( !wp_verify_nonce( $_POST['SEM-events-nonce'], 'SEM-events-nonce' )) { return $post->ID; }if ( !current_user_can( 'edit_post', $post->ID )) return$post->ID;

}

Page 27: WordCamp Montreal 2015: Combining Custom Post Types, Fields, and Meta Boxes to do the Impossible with WordPress

function custom_meta_box_save() {

if(!isset($_POST["sem_events_startdate"])):return $post;endif;$updatestartdate = strtotime ($_POST["sem_events_startdate"] .$_POST["sem_events_starttime"] );update_post_meta($post->ID, "sem_events_startdate",$updatestartdate );

}

Page 28: WordCamp Montreal 2015: Combining Custom Post Types, Fields, and Meta Boxes to do the Impossible with WordPress

add_action( 'add_meta_boxes', 'create_custom_meta_box');

add_action( 'save_post', 'custom_meta_box_save' );

Page 29: WordCamp Montreal 2015: Combining Custom Post Types, Fields, and Meta Boxes to do the Impossible with WordPress
Page 30: WordCamp Montreal 2015: Combining Custom Post Types, Fields, and Meta Boxes to do the Impossible with WordPress

wp_enqueue_script( 'moment', plugin_dir_url( __FILE__ ) .'js/moment.js', array( ), 1.0, false );

wp_enqueue_script( 'pikaday', plugin_dir_url( __FILE__ ) .'js/pikaday.js', array( 'moment' ), 1.0, false );

wp_enqueue_script( special-event-manager, plugin_dir_url(__FILE__ ) . 'js/special-event-manager-admin.js', array('pikaday' ), 1.0, false );

Page 31: WordCamp Montreal 2015: Combining Custom Post Types, Fields, and Meta Boxes to do the Impossible with WordPress

wp_enqueue_style( special-events-manager, plugin_dir_url(__FILE__ ) . 'css/special-event-manager-admin.css', array(),1.0, 'all' );

wp_enqueue_style( 'pikadaycss', plugin_dir_url( __FILE__ ) .'css/pikaday.css', array(), 1.0, 'all' );

Page 32: WordCamp Montreal 2015: Combining Custom Post Types, Fields, and Meta Boxes to do the Impossible with WordPress
Page 33: WordCamp Montreal 2015: Combining Custom Post Types, Fields, and Meta Boxes to do the Impossible with WordPress

single-sem_event.php

single.php

index.php

Page 34: WordCamp Montreal 2015: Combining Custom Post Types, Fields, and Meta Boxes to do the Impossible with WordPress

$custom_fields = get_post_custom($post_id);

the date = $custom_fields["sem_events_startdate"][0];

the formatted date = date("D, M d, Y", the date);

Page 35: WordCamp Montreal 2015: Combining Custom Post Types, Fields, and Meta Boxes to do the Impossible with WordPress

if there's no end date or time, then { July 4th at 2pm} else if there is an end time, but start date = end date {

July 4th from 2pm - 3pm} else if start date != end date {

July 4th at 2pm to July 5th at 5pm }

Page 36: WordCamp Montreal 2015: Combining Custom Post Types, Fields, and Meta Boxes to do the Impossible with WordPress

$src = wp_get_attachment_image_src(get_post_thumbnail_id($post->ID), array( 250,255 ), false, '');

<img src="<?php $src[0] ?>">

<div style="background:url('<?php $src[0] ?>')"></div>

Page 37: WordCamp Montreal 2015: Combining Custom Post Types, Fields, and Meta Boxes to do the Impossible with WordPress

$today6am = strtotime('today 6:00') + ( get_option('gmt_offset' ) * 3600 );

Page 38: WordCamp Montreal 2015: Combining Custom Post Types, Fields, and Meta Boxes to do the Impossible with WordPress

if ( $post_event_time > $today6am ) {

if( function_exists( 'ninja_forms_display_form' ) ){ninja_forms_display_form( 6 ); }

}

Page 39: WordCamp Montreal 2015: Combining Custom Post Types, Fields, and Meta Boxes to do the Impossible with WordPress
Page 40: WordCamp Montreal 2015: Combining Custom Post Types, Fields, and Meta Boxes to do the Impossible with WordPress

archive-sem_event.php

archive.php

index.php

Page 41: WordCamp Montreal 2015: Combining Custom Post Types, Fields, and Meta Boxes to do the Impossible with WordPress

$query->set('orderby', 'meta_value_num');

$query->set('meta_key', 'sem_events_startdate');

$query->set('order', 'ASC');

Page 42: WordCamp Montreal 2015: Combining Custom Post Types, Fields, and Meta Boxes to do the Impossible with WordPress
Page 43: WordCamp Montreal 2015: Combining Custom Post Types, Fields, and Meta Boxes to do the Impossible with WordPress
Page 44: WordCamp Montreal 2015: Combining Custom Post Types, Fields, and Meta Boxes to do the Impossible with WordPress
Page 45: WordCamp Montreal 2015: Combining Custom Post Types, Fields, and Meta Boxes to do the Impossible with WordPress

#WCMTL / @allilevine

git.io/semplugin