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 );
}
array(
'labels' => array( 'name' => __( 'Events', 'SEM' ), ...),
'rewrite' => array( 'slug' => 'event'),
'has_archive' => ...
add_action( 'init', 'create_custom_post_type' );
function create_custom_taxonomy() {
register_taxonomy( 'special-event-category', everything
else)
}
'hierarchical' => true
'hierarchical' => false
add_action( 'init', 'create_custom_taxonomy' );
add_action( 'init', 'create_custom_post_type' );
register_post_type(
'taxonomies' => array('special-event-category', 'special-
event-tag')
)
function create_custom_meta_box() {
add_meta_box('special_event_meta', 'Event Date &
Time', 'render_special_event_meta_box...
function render_special_event_meta_box() {
require_once plugin_dir_path( __FILE__ ) .
'partials/special-event-manager-admi...
<li>
<label>Start Date</label>
<input name="sem_events_startdate" id="startPicker" value="<?php echo
</li>
<li>
<label>Sta...
wp_create_nonce
echo '<input type="hidden" name="SEM-events-nonce" id="SEM-events-nonce"
wp_create_nonce( 'SEM-events-nonc...
$custom = get_post_custom($post->ID);
isset($custom["sem_events_startdate"][0]) ?
MON, JUN 29TH, 2015
date("D, M d, Y", the date);
2:31 PM
get_option('time_format');
date($time_format, the date);
function custom_meta_box_save() {
if ( !wp_verify_nonce( $_POST['SEM-events-nonce'], 'SEM-
events-nonce' )) { return $post...
function custom_meta_box_save() {
if(!isset($_POST["sem_events_startdate"])):
return $post;
endif;
$updatestartdate = strt...
add_action( 'add_meta_boxes', 'create_custom_meta_box'
);
add_action( 'save_post', 'custom_meta_box_save' );
wp_enqueue_script( 'moment', plugin_dir_url( __FILE__ ) .
'js/moment.js', array( ), 1.0, false );
wp_enqueue_script( 'pika...
wp_enqueue_style( special-events-manager, plugin_dir_url(
__FILE__ ) . 'css/special-event-manager-admin.css', array(),
1.0...
single-sem_event.php
single.php
index.php
$custom_fields = get_post_custom($post_id);
the date = $custom_fields["sem_events_startdate"][0];
the formatted date = dat...
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 4t...
$src = wp_get_attachment_image_src(
get_post_thumbnail_id($post->ID), array( 250,255 ), false, ''
);
<img src="<?php $src[...
$today6am = strtotime('today 6:00') + ( get_option(
'gmt_offset' ) * 3600 );
if ( $post_event_time > $today6am ) {
if( function_exists( 'ninja_forms_display_form' ) ){
ninja_forms_display_form( 6 ); ...
archive-sem_event.php
archive.php
index.php
$query->set('orderby', 'meta_value_num');
$query->set('meta_key', 'sem_events_startdate');
$query->set('order', 'ASC');
#WCMTL / @allilevine
git.io/semplugin
WordCamp Montreal 2015: Combining Custom Post Types, Fields, and Meta Boxes to do the Impossible with WordPress
WordCamp Montreal 2015: Combining Custom Post Types, Fields, and Meta Boxes to do the Impossible with WordPress
WordCamp Montreal 2015: Combining Custom Post Types, Fields, and Meta Boxes to do the Impossible with WordPress
WordCamp Montreal 2015: Combining Custom Post Types, Fields, and Meta Boxes to do the Impossible with WordPress
WordCamp Montreal 2015: Combining Custom Post Types, Fields, and Meta Boxes to do the Impossible with WordPress
WordCamp Montreal 2015: Combining Custom Post Types, Fields, and Meta Boxes to do the Impossible with WordPress
WordCamp Montreal 2015: Combining Custom Post Types, Fields, and Meta Boxes to do the Impossible with WordPress
WordCamp Montreal 2015: Combining Custom Post Types, Fields, and Meta Boxes to do the Impossible with WordPress
WordCamp Montreal 2015: Combining Custom Post Types, Fields, and Meta Boxes to do the Impossible with WordPress
WordCamp Montreal 2015: Combining Custom Post Types, Fields, and Meta Boxes to do the Impossible with WordPress
WordCamp Montreal 2015: Combining Custom Post Types, Fields, and Meta Boxes to do the Impossible with WordPress
WordCamp Montreal 2015: Combining Custom Post Types, Fields, and Meta Boxes to do the Impossible with WordPress
WordCamp Montreal 2015: Combining Custom Post Types, Fields, and Meta Boxes to do the Impossible with WordPress
WordCamp Montreal 2015: Combining Custom Post Types, Fields, and Meta Boxes to do the Impossible with WordPress
WordCamp Montreal 2015: Combining Custom Post Types, Fields, and Meta Boxes to do the Impossible with WordPress
Upcoming SlideShare
Loading in …5
×

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

957 views

Published on

Custom post types, fields, and meta boxes all offer powerful ways to transform WordPress sites, but when you use all three together, the impossible becomes possible. In this talk I’ll explore just how custom you can go by combining WordPress’ custom functions to solve a real-world problem. I’ll also cover custom taxonomies, custom templates, and leveraging some basic PHP, featured images, and existing plugin functionality to take your custom post type even further.

Published in: Technology
0 Comments
1 Like
Statistics
Notes
  • Be the first to comment

No Downloads
Views
Total views
957
On SlideShare
0
From Embeds
0
Number of Embeds
56
Actions
Shares
0
Downloads
16
Comments
0
Likes
1
Embeds 0
No embeds

No notes for slide

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

  1. 1. COMBINING CUSTOM POST TYPES, FIELDS, AND META BOXES TO DO THE IMPOSSIBLE WITH WORDPRESS
  2. 2.  
  3. 3.  
  4. 4. function create_custom_post_type() { register_post_type( 'sem_event', everything else ); }
  5. 5. array( 'labels' => array( 'name' => __( 'Events', 'SEM' ), ...), 'rewrite' => array( 'slug' => 'event'), 'has_archive' => 'events', 'supports' => array( 'title', 'editor', 'thumbnail', 'custom- fields') )
  6. 6. add_action( 'init', 'create_custom_post_type' );
  7. 7. function create_custom_taxonomy() { register_taxonomy( 'special-event-category', everything else) }
  8. 8. 'hierarchical' => true 'hierarchical' => false
  9. 9. add_action( 'init', 'create_custom_taxonomy' ); add_action( 'init', 'create_custom_post_type' );
  10. 10. register_post_type( 'taxonomies' => array('special-event-category', 'special- event-tag') )
  11. 11. function create_custom_meta_box() { add_meta_box('special_event_meta', 'Event Date & Time', 'render_special_event_meta_box', 'sem_event', 'side', 'high'); }
  12. 12. function render_special_event_meta_box() { require_once plugin_dir_path( __FILE__ ) . 'partials/special-event-manager-admin-display.php'; }
  13. 13. <li> <label>Start Date</label> <input name="sem_events_startdate" id="startPicker" value="<?php echo </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
  14. 14. wp_create_nonce echo '<input type="hidden" name="SEM-events-nonce" id="SEM-events-nonce" wp_create_nonce( 'SEM-events-nonce' ) . '">';
  15. 15. $custom = get_post_custom($post->ID); isset($custom["sem_events_startdate"][0]) ?
  16. 16. MON, JUN 29TH, 2015 date("D, M d, Y", the date); 2:31 PM get_option('time_format'); date($time_format, the date);
  17. 17. 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; }
  18. 18. 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 ); }
  19. 19. add_action( 'add_meta_boxes', 'create_custom_meta_box' ); add_action( 'save_post', 'custom_meta_box_save' );
  20. 20. 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 );
  21. 21. 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' );
  22. 22. single-sem_event.php single.php index.php
  23. 23. $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);
  24. 24. 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 }
  25. 25. $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>
  26. 26. $today6am = strtotime('today 6:00') + ( get_option( 'gmt_offset' ) * 3600 );
  27. 27. if ( $post_event_time > $today6am ) { if( function_exists( 'ninja_forms_display_form' ) ){ ninja_forms_display_form( 6 ); } }
  28. 28. archive-sem_event.php archive.php index.php
  29. 29. $query->set('orderby', 'meta_value_num'); $query->set('meta_key', 'sem_events_startdate'); $query->set('order', 'ASC');
  30. 30. #WCMTL / @allilevine git.io/semplugin

×