Create Custom Post Type Plugin
-
Upload
jan-wilson -
Category
Technology
-
view
9.568 -
download
1
description
Transcript of Create Custom Post Type Plugin
![Page 1: Create Custom Post Type Plugin](https://reader034.fdocuments.net/reader034/viewer/2022042522/559950821a28abe76e8b4714/html5/thumbnails/1.jpg)
WORDPRESS CUSTOM POSTS
USING POST META BOXES
CHICAGO NORTHSIDE WORDPRESS MEETUP,2/4/14
This presentation at:http://bigwdesign.com/presentations/wp-custpost-coded.pdf
![Page 2: Create Custom Post Type Plugin](https://reader034.fdocuments.net/reader034/viewer/2022042522/559950821a28abe76e8b4714/html5/thumbnails/2.jpg)
WHAT IS A CUSTOM POST?
NOTHING BUT AN ENTRY IN THE WP_POSTSTABLE WITH A SPECIAL POST_TYPE TODISTINGUISH IT FROM OTHER POST TYPES.
And in our case, some associated wp_postmeta entries.
![Page 3: Create Custom Post Type Plugin](https://reader034.fdocuments.net/reader034/viewer/2022042522/559950821a28abe76e8b4714/html5/thumbnails/3.jpg)
WHY USE CUSTOM POSTS?
You need a post type other than post or page.
Structured way for editors/users to consistently enter data.
Format posts consistently using a custom template.
You'd like to classify and access posts using some custom taxonomy.
![Page 4: Create Custom Post Type Plugin](https://reader034.fdocuments.net/reader034/viewer/2022042522/559950821a28abe76e8b4714/html5/thumbnails/4.jpg)
EXAMPLE: GRILLS, INC.
Wants to implement a special product catalog site.
4 products now; 400 in a few years.
Site admin could create HTML and copy it indefinitely.
Prone to error, requires good HTML skills, hard for users.
Need simple, easy-to-use, structured input method, and a commonlook.
Decision: Custom Post and template.
![Page 5: Create Custom Post Type Plugin](https://reader034.fdocuments.net/reader034/viewer/2022042522/559950821a28abe76e8b4714/html5/thumbnails/5.jpg)
CODING APPROACH
Use of meta boxes for custom data ... as opposed to custom fields.(Ease of creation vs. performance?)
It all ends up in wp_postmeta anyhow
Use add_action hooks into Wordpress: Hook a function on to aspecific WP action.
Use add_filters: Hook a function to a specific filter (runbefore/modify a WP action).
![Page 6: Create Custom Post Type Plugin](https://reader034.fdocuments.net/reader034/viewer/2022042522/559950821a28abe76e8b4714/html5/thumbnails/6.jpg)
PRESENTATION CODING TASKS
New plugin directory in wp-content/plugins (e.g., custom_grills)
A grills.php plugin file - nothing but a collection of functions
Admin post metaboxes to enter and save structured data
A single post template to display posts, single-grills.php
A hook into categories
Multi post template for categories, category-grills.php
![Page 7: Create Custom Post Type Plugin](https://reader034.fdocuments.net/reader034/viewer/2022042522/559950821a28abe76e8b4714/html5/thumbnails/7.jpg)
BASIC PLUGIN STRUCTURE
Plugin Header - looks like comments
An add_action hook to run custom post registration function
Function to run register_post_type
Various function parameters
<?php/*Plugin Name: GrillsDescription: Create and maintain custom grill posts.Version: 1.0Author: Terry B.*/add_action( 'init', 'create_grill'); // executes create_grill whenever any page is generatedfunction create_grill() { register_post_type( 'grills', // tell WP we have this new post type // etc - parameters here
![Page 8: Create Custom Post Type Plugin](https://reader034.fdocuments.net/reader034/viewer/2022042522/559950821a28abe76e8b4714/html5/thumbnails/8.jpg)
COMPLETE CUSTOM POST TYPE CREATION
<?php/* Plugin Name: Grills Description: Create and maintain custom grill posts. Version: 1.0 Author: Terry B.*/
add_action( 'init', 'create_grill'); function create_grill() { register_post_type( 'grills', array( 'labels' => array( 'name' => 'Grills', 'post type general name', 'singular_name' => 'Grills', 'post type singular name', 'add_new' => 'Add New', 'add_new_item' => 'Add New Grill', 'edit_item' => 'Edit Grill', 'new_item' => 'New Grill', 'all_items' => 'All Grills', 'view_item' => 'View Grill', 'search_items' => 'Search Grills', 'not_found' => 'No grills found', 'not_found_in_trash' => 'No grills in the Trash', 'parent_item_colon' => '', 'parent' => 'Grills' ), 'show_in_nav_menus' => TRUE, 'hierarchical' => true, 'description' => 'Holds our products and product specific data', 'public' => true, 'menu_position' => 5, 'supports' => array( 'title', 'editor', 'page-attributes' ), 'has_archive' => true ) );} // end function?>
![Page 9: Create Custom Post Type Plugin](https://reader034.fdocuments.net/reader034/viewer/2022042522/559950821a28abe76e8b4714/html5/thumbnails/9.jpg)
DEMO 1. BASIC CUSTOM POST PLUGIN
Create the directory
Create grills.php
Add the basic grills.php code to wp-plugins
Add & activate plugin in WP back end
Create a custom post for grill #1, & test
Outcome: That's somewhat underwhelming.
![Page 10: Create Custom Post Type Plugin](https://reader034.fdocuments.net/reader034/viewer/2022042522/559950821a28abe76e8b4714/html5/thumbnails/10.jpg)
ADD META BOXES TO BACK END
1. Admin_init function to add meta boxes to admin2. Add function & HTML to display meta boxes3. Add action to save contents of meta boxes
![Page 11: Create Custom Post Type Plugin](https://reader034.fdocuments.net/reader034/viewer/2022042522/559950821a28abe76e8b4714/html5/thumbnails/11.jpg)
ADDING META BOXES
1. ADMIN_INIT FUNCTION TO ADD META BOXES
// action will fire on init of adminadd_action( 'admin_init', 'my_admin' );function my_admin() { add_meta_box( 'grill_meta_box', 'Grill Details', 'display_custom_meta_box', 'grills', 'normal', 'high' );}
![Page 12: Create Custom Post Type Plugin](https://reader034.fdocuments.net/reader034/viewer/2022042522/559950821a28abe76e8b4714/html5/thumbnails/12.jpg)
ADDING META BOXES
2. ADD FUNCTION AND HTML TO DISPLAYMETA BOXES
// function will display meta-boxes on back endfunction display_custom_meta_box( $grill ) { // Retrieve various fields based on ID $custom_series = esc_html( get_post_meta( $grill->ID, 'series_name', true ) ); $custom_model = esc_html( get_post_meta( $grill->ID, 'model_name', true ) ); //etc
<table> <tr> <td style="width: 100%">Series</td> <td><input type="text" size="80" name="series_name" value="<?php echo $custom_series; ?>" </tr> <tr> <td style="width: 100%">Model</td> <td><input type="text" size="80" name="model_name" value="<?php echo $custom_model; ?>" </tr>
![Page 13: Create Custom Post Type Plugin](https://reader034.fdocuments.net/reader034/viewer/2022042522/559950821a28abe76e8b4714/html5/thumbnails/13.jpg)
COMPLETE ADMIN METABOX FUNCTION
// function will display meta-boxes on back endfunction display_custom_meta_box( $grill ) { // Retrieve various fileds based on ID $custom_series = esc_html( get_post_meta( $grill->ID, 'series_name', true ) ); $custom_model = esc_html( get_post_meta( $grill->ID, 'model_name', true ) ); $titlegraphic = esc_html( get_post_meta( $grill->ID, 'titlegraphic', true ) ); $custom_image = esc_html( get_post_meta( $grill->ID, 'custom_image', true ) ); $image_caption = esc_html( get_post_meta( $grill->ID, 'image_caption', true ) ); $cooking_surface = esc_html( get_post_meta( $grill->ID, 'cooking_surface', true ) ); $btu_primary = esc_html( get_post_meta( $grill->ID, 'btu_primary', true ) ); $btu_secondary = esc_html( get_post_meta( $grill->ID, 'btu_secondary', true ) ); $cut_out = esc_html( get_post_meta( $grill->ID, 'cut_out', true ) ); $sideburner = esc_html( get_post_meta( $grill->ID, 'sideburner', true ) ); $excerpt = esc_html( get_post_meta( $grill->ID, 'excerpt', true ) ); ?> <table> <tr> <td style="width: 100%">Series</td> <td><input type="text" size="80" name="series_name" value="<?php echo $custom_series; ?>" </tr> <tr> <td style="width: 100%">Model</td> <td><input type="text" size="80" name="model_name" value="<?php echo $custom_model; ?>" </tr> <tr> <td style="width: 100%">Title Graphic</td> <td><input type="text" size="80" name="titlegraphic" value="<?php echo $titlegraphic; ?>" </tr> <tr>
![Page 14: Create Custom Post Type Plugin](https://reader034.fdocuments.net/reader034/viewer/2022042522/559950821a28abe76e8b4714/html5/thumbnails/14.jpg)
ADDING META BOXES
3. ADD ACTION TO SAVE METABOXES/FIELDS
// function will add data to wp_postmeta on save_post, IF post_type = grillsadd_action( 'save_post', 'add_grill_fields', 10, 2 ); // priority & argsfunction add_grill_fields( $grill_id, $grill ) { // Check post type for grills if ( $grill->post_type == 'grills' ) { // Store data in post meta table if present in post data if ( isset( $_POST['series_name'] ) && $_POST['series_name'] != '' ) { update_post_meta( $grill_id, 'series_name', $_POST['series_name'] ); } // etc
![Page 15: Create Custom Post Type Plugin](https://reader034.fdocuments.net/reader034/viewer/2022042522/559950821a28abe76e8b4714/html5/thumbnails/15.jpg)
COMPLETE META BOX/FIELD SAVEFUNCTION
// add save_post action to add grill fields into post_metaadd_action( 'save_post', 'add_grill_fields', 10, 2 );function add_grill_fields( $grill_id, $grill ) { // Check post type for grills if ( $grill->post_type == 'grills' ) { // Store data in post meta table if present in post data if ( isset( $_POST['series_name'] ) && $_POST['series_name'] != '' ) { update_post_meta( $grill_id, 'series_name', $_POST['series_name'] ); } if ( isset( $_POST['model_name'] ) && $_POST['model_name'] != '' ) { update_post_meta( $grill_id, 'model_name', $_POST['model_name'] ); } if ( isset( $_POST['titlegraphic'] ) && $_POST['titlegraphic'] != '' ) { update_post_meta( $grill_id, 'titlegraphic', $_POST['titlegraphic'] ); } if ( isset( $_POST['custom_image'] ) && $_POST['custom_image'] != '' ) { update_post_meta( $grill_id, 'custom_image', $_POST['custom_image'] ); } if ( isset( $_POST['image_caption'] ) && $_POST['image_caption'] != '' ) { update_post_meta( $grill_id, 'image_caption', $_POST['image_caption'] ); } if ( isset( $_POST['cooking_surface'] ) && $_POST['cooking_surface'] != '' ) { update_post_meta( $grill_id, 'cooking_surface', $_POST['cooking_surface'] ); } if ( isset( $_POST['btu_primary'] ) && $_POST['btu_primary'] != '' ) { update_post_meta( $grill_id, 'btu_primary', $_POST['btu_primary'] ); } if ( isset( $_POST['btu_secondary'] ) && $_POST['btu_secondary'] != '' ) { update_post_meta( $grill_id, 'btu_secondary', $_POST['btu_secondary'] ); } if ( isset( $_POST['cut_out'] ) && $_POST['cut_out'] != '' ) { update_post_meta( $grill_id, 'cut_out', $_POST['cut_out'] ); } if ( isset( $_POST['sideburner'] ) && $_POST['sideburner'] != '' ) { update_post_meta( $grill_id, 'sideburner', $_POST['sideburner'] ); } if ( isset( $_POST['excerpt'] ) && $_POST['excerpt'] != '' ) { update_post_meta( $grill_id, 'excerpt', $_POST['excerpt'] ); } }}
![Page 16: Create Custom Post Type Plugin](https://reader034.fdocuments.net/reader034/viewer/2022042522/559950821a28abe76e8b4714/html5/thumbnails/16.jpg)
DEMO 2. METABOXES
Add the three Meta Box functions to grills.php
Add some data to grill #1
Create grill #2 and add data to it as well
Outcome: Metaboxes added to back end, and we are saving custompost data
![Page 17: Create Custom Post Type Plugin](https://reader034.fdocuments.net/reader034/viewer/2022042522/559950821a28abe76e8b4714/html5/thumbnails/17.jpg)
CUSTOM SINGLE TEMPLATE
1. Add action to register special style sheet2. Add filter to use special template3. Create single item template
![Page 18: Create Custom Post Type Plugin](https://reader034.fdocuments.net/reader034/viewer/2022042522/559950821a28abe76e8b4714/html5/thumbnails/18.jpg)
CREATING CUSTOM SINGLE TEMPLATE
1. ADD ACTION TO REGISTER SPECIAL STYLESHEET
// register the css we are going to use with our custom templateadd_action( 'wp_enqueue_scripts', 'register_grill_styles' );function register_grill_styles() { wp_register_style( 'custom_grills', plugins_url( '/custom_grills/css/grill-style.css' ) ); wp_enqueue_style( 'custom_grills' );}
![Page 19: Create Custom Post Type Plugin](https://reader034.fdocuments.net/reader034/viewer/2022042522/559950821a28abe76e8b4714/html5/thumbnails/19.jpg)
CREATING CUSTOM SINGLE TEMPLATE
2. ADD FILTER TO USE SPECIAL TEMPLATE
// force use of dedicated templateadd_filter( 'single_template', 'get_custom_post_type_template' );function get_custom_post_type_template($single_template) { global $post;
if ($post->post_type == 'grills') { $single_template = dirname( __FILE__ ) . '/single-grills.php'; } // echo "$single_template"; // die(); return $single_template;}
![Page 20: Create Custom Post Type Plugin](https://reader034.fdocuments.net/reader034/viewer/2022042522/559950821a28abe76e8b4714/html5/thumbnails/20.jpg)
CREATING CUSTOM SINGLE TEMPLATE
3. CREATE SINGLE ITEM TEMPLATE - COPY &MODIFY THEME'S SINGLE.PHP
(JUST LIKE BUILDING A STANDARD WORDPRESSTEMPLATE, BUT WITH YOUR CUSTOM LAYOUT ANDDATA)
<?php
get_header(); ?>
<div id="primary"> <div id="content" role="main"> <?php // $mypost = array( 'post_type' => 'custom_products', ); // $loop = new WP_Query( $mypost ); ?> <?php // while ( $loop->have_posts() ) : $loop->the_post(); while ( have_posts() ) : the_post(); ?>// etc
![Page 21: Create Custom Post Type Plugin](https://reader034.fdocuments.net/reader034/viewer/2022042522/559950821a28abe76e8b4714/html5/thumbnails/21.jpg)
DEMO 3. CUSTOM TEMPLATE
Add the code to grills.php, create single-grills.php
Note that our custom post now shows in a custom template
![Page 22: Create Custom Post Type Plugin](https://reader034.fdocuments.net/reader034/viewer/2022042522/559950821a28abe76e8b4714/html5/thumbnails/22.jpg)
MIGHT WANT TO VIEW MORE THAN ONEPRODUCT FROM MENU SELECTION
Create a Custom taxonomy (another day)
Add custom post type to existing Category system
![Page 23: Create Custom Post Type Plugin](https://reader034.fdocuments.net/reader034/viewer/2022042522/559950821a28abe76e8b4714/html5/thumbnails/23.jpg)
ADDING A CUSTOM POST TYPE TO EXISTINGCATEGORY SYSTEM
1. Modify register_post to include taxonomy Category2. Add pre_get_posts action that will add our post type to main
query3. Create category-grills.php template to display multiple posts
![Page 24: Create Custom Post Type Plugin](https://reader034.fdocuments.net/reader034/viewer/2022042522/559950821a28abe76e8b4714/html5/thumbnails/24.jpg)
ADDING CUSTOM POST TYPE TOCATEGORIES
1. MODIFY REGISTER_POST TO INCLUDETAXONOMY CATEGORY
ADD ONE LINE (TAXONOMIES) TO BOTTOM OFFUNCTION CREATE_GRILL
// etc etc 'menu_position' => 5, 'supports' => array( 'title', 'editor', 'page-attributes' ), 'has_archive' => true, 'taxonomies' => array('category') )
![Page 25: Create Custom Post Type Plugin](https://reader034.fdocuments.net/reader034/viewer/2022042522/559950821a28abe76e8b4714/html5/thumbnails/25.jpg)
ADDING CUSTOM POST TYPE TOCATEGORIES
2. ADD PRE_GET_POSTS ACTION THAT WILLADD POST TYPE TO MAIN QUERY
NEW FUNCTION ADDED TO GRILLS.PHP
// Show posts of 'post', 'page' and 'grills' post types on home pageadd_action( 'pre_get_posts', 'add_grill_post_type_to_query' );
function add_grill_post_type_to_query( $query ) { if ( $query->is_main_query() ) $query->set( 'post_type', array( 'post', 'page', 'grills' ) ); return $query;}
![Page 26: Create Custom Post Type Plugin](https://reader034.fdocuments.net/reader034/viewer/2022042522/559950821a28abe76e8b4714/html5/thumbnails/26.jpg)
ADDING CUSTOM POST TYPE TOCATEGORIES
3. CREATE CATEGORY-GRILLS.PHP TEMPLATETO DISPLAY MULTIPLE POSTS
Copy category.php from main template to our plugin dir, and modifyas needed
Add new filter on archive_template to force use of our archivetemplate
Add some Wordpress categories in backend, assign posts, make menuitems!
// force use of dedicated category templateadd_filter( 'category_template', 'get_grills_category_template' );function get_grills_category_template($category_template) { global $post; if ($post->post_type == 'grills') { $category_template = dirname( __FILE__ ) . '/category-grills.php'; } return $category_template;}
![Page 27: Create Custom Post Type Plugin](https://reader034.fdocuments.net/reader034/viewer/2022042522/559950821a28abe76e8b4714/html5/thumbnails/27.jpg)
DEMO 4. CUSTOM POSTS BY CATEGORY
Modify main function to add taxonomy
Add function to include grills in posts query
Add function to force use of category-grills template
Add some special grill categories to back end and assignposts
Menu items to those categories
Outcome: Menu items display categories of custom posts
![Page 28: Create Custom Post Type Plugin](https://reader034.fdocuments.net/reader034/viewer/2022042522/559950821a28abe76e8b4714/html5/thumbnails/28.jpg)
THE END
Presentation originally developed from tutorials at:
See also Wordpress codex on post_types (has many other links):
This presentation:
http://wp.tutsplus.com/tutorials/plugins/a-guide-to-wordpress-custom-post-types-creation-display-and-meta-boxes/http://wp.tutsplus.com/tutorials/plugins/a-guide-to-wordpress-custom-post-types-taxonomies-admin-columns-filters-and-archives/
http://codex.wordpress.org/Post_Types
http://bigwdesign.com/presentations/wp-custpost-coded.pdf
![Page 29: Create Custom Post Type Plugin](https://reader034.fdocuments.net/reader034/viewer/2022042522/559950821a28abe76e8b4714/html5/thumbnails/29.jpg)
![Page 30: Create Custom Post Type Plugin](https://reader034.fdocuments.net/reader034/viewer/2022042522/559950821a28abe76e8b4714/html5/thumbnails/30.jpg)
![Page 31: Create Custom Post Type Plugin](https://reader034.fdocuments.net/reader034/viewer/2022042522/559950821a28abe76e8b4714/html5/thumbnails/31.jpg)
![Page 32: Create Custom Post Type Plugin](https://reader034.fdocuments.net/reader034/viewer/2022042522/559950821a28abe76e8b4714/html5/thumbnails/32.jpg)
![Page 33: Create Custom Post Type Plugin](https://reader034.fdocuments.net/reader034/viewer/2022042522/559950821a28abe76e8b4714/html5/thumbnails/33.jpg)
![Page 34: Create Custom Post Type Plugin](https://reader034.fdocuments.net/reader034/viewer/2022042522/559950821a28abe76e8b4714/html5/thumbnails/34.jpg)
![Page 35: Create Custom Post Type Plugin](https://reader034.fdocuments.net/reader034/viewer/2022042522/559950821a28abe76e8b4714/html5/thumbnails/35.jpg)
![Page 36: Create Custom Post Type Plugin](https://reader034.fdocuments.net/reader034/viewer/2022042522/559950821a28abe76e8b4714/html5/thumbnails/36.jpg)
![Page 37: Create Custom Post Type Plugin](https://reader034.fdocuments.net/reader034/viewer/2022042522/559950821a28abe76e8b4714/html5/thumbnails/37.jpg)
![Page 38: Create Custom Post Type Plugin](https://reader034.fdocuments.net/reader034/viewer/2022042522/559950821a28abe76e8b4714/html5/thumbnails/38.jpg)
![Page 39: Create Custom Post Type Plugin](https://reader034.fdocuments.net/reader034/viewer/2022042522/559950821a28abe76e8b4714/html5/thumbnails/39.jpg)
![Page 40: Create Custom Post Type Plugin](https://reader034.fdocuments.net/reader034/viewer/2022042522/559950821a28abe76e8b4714/html5/thumbnails/40.jpg)
![Page 41: Create Custom Post Type Plugin](https://reader034.fdocuments.net/reader034/viewer/2022042522/559950821a28abe76e8b4714/html5/thumbnails/41.jpg)
![Page 42: Create Custom Post Type Plugin](https://reader034.fdocuments.net/reader034/viewer/2022042522/559950821a28abe76e8b4714/html5/thumbnails/42.jpg)
![Page 43: Create Custom Post Type Plugin](https://reader034.fdocuments.net/reader034/viewer/2022042522/559950821a28abe76e8b4714/html5/thumbnails/43.jpg)
![Page 44: Create Custom Post Type Plugin](https://reader034.fdocuments.net/reader034/viewer/2022042522/559950821a28abe76e8b4714/html5/thumbnails/44.jpg)
![Page 45: Create Custom Post Type Plugin](https://reader034.fdocuments.net/reader034/viewer/2022042522/559950821a28abe76e8b4714/html5/thumbnails/45.jpg)
![Page 46: Create Custom Post Type Plugin](https://reader034.fdocuments.net/reader034/viewer/2022042522/559950821a28abe76e8b4714/html5/thumbnails/46.jpg)
![Page 47: Create Custom Post Type Plugin](https://reader034.fdocuments.net/reader034/viewer/2022042522/559950821a28abe76e8b4714/html5/thumbnails/47.jpg)
![Page 48: Create Custom Post Type Plugin](https://reader034.fdocuments.net/reader034/viewer/2022042522/559950821a28abe76e8b4714/html5/thumbnails/48.jpg)
![Page 49: Create Custom Post Type Plugin](https://reader034.fdocuments.net/reader034/viewer/2022042522/559950821a28abe76e8b4714/html5/thumbnails/49.jpg)
![Page 50: Create Custom Post Type Plugin](https://reader034.fdocuments.net/reader034/viewer/2022042522/559950821a28abe76e8b4714/html5/thumbnails/50.jpg)
![Page 51: Create Custom Post Type Plugin](https://reader034.fdocuments.net/reader034/viewer/2022042522/559950821a28abe76e8b4714/html5/thumbnails/51.jpg)
![Page 52: Create Custom Post Type Plugin](https://reader034.fdocuments.net/reader034/viewer/2022042522/559950821a28abe76e8b4714/html5/thumbnails/52.jpg)
![Page 53: Create Custom Post Type Plugin](https://reader034.fdocuments.net/reader034/viewer/2022042522/559950821a28abe76e8b4714/html5/thumbnails/53.jpg)
![Page 54: Create Custom Post Type Plugin](https://reader034.fdocuments.net/reader034/viewer/2022042522/559950821a28abe76e8b4714/html5/thumbnails/54.jpg)
![Page 55: Create Custom Post Type Plugin](https://reader034.fdocuments.net/reader034/viewer/2022042522/559950821a28abe76e8b4714/html5/thumbnails/55.jpg)
![Page 56: Create Custom Post Type Plugin](https://reader034.fdocuments.net/reader034/viewer/2022042522/559950821a28abe76e8b4714/html5/thumbnails/56.jpg)