Upgrade to Pro — share decks privately, control downloads, hide ads and more …

Extending WordPress with Custom Content Types

Extending WordPress with Custom Content Types

An overview of custom content/post types and custom fields in WordPress: when and why to use them, tools to make them easier to create.

Robin Cornett

April 04, 2017
Tweet

More Decks by Robin Cornett

Other Decks in Technology

Transcript

  1. WHAT IS A CUSTOM CONTENT TYPE? A custom post [content]

    type is nothing more than a regular post with a different post_type value in the database. The post type of regular posts is post, pages use page, attachments use attachment and so on. --Smashing Magazine (11/8/2012)
  2. WHY USE CUSTOM CONTENT TYPES? ▪ Organization ▪ Prevent yourself

    from making hacky edits to display certain categories and/or pages in a certain manner, or hide certain posts from your blog feed ▪ Set up detailed templates to ensure that your end user creates content the way it’s supposed to look ▪ To make life better for your end user, client, intern, etc.
  3. #DOINGITWRONG <div id="bio_thumbs“><div id="curt_mcdaniel" class="bio_thumbs active_bio_thumb“><p>Curt McDaniel<br />Interim Senior Pastor</p></div><div

    id="stu_austin" class="bio_thumbs“><p>Stu Austin<br />Associate Pastor</p></div><div id="phil_chandler" class="bio_thumbs“><p>Phil Chandler<br /><a href="http://www.newhopefortmyers.org/music/">Music Director</a></p></div><div id="krissy_bashor" class="bio_thumbs“><p>Krissy Bashor<br />Assistant Director<br /><a href="http://www.newhopefortmyers.org/students/">Student Ministries</a></p></div><div id="jeannie_mcgalliard" class="bio_thumbs“><p>Jeannie McGalliard<br /><a href="http://www.newhopefortmyers.org/women/">Women in Ministry</a></p></div><div id="debbie_norris" class="bio_thumbs“><p>Debbie Norris<br /><a href="http://www.newhopefortmyers.org/children/">Children's Ministries</a></p></div><div id="pam_poland" class="bio_thumbs“><p>Pam Poland<br /><a href="http://www.newhopefortmyers.org/early-childhood/">Early Childhood</a></p></div><div id="jim_parkinson" class="bio_thumbs“><p>Jim Parkinson<br /><a href="http://www.newhopefortmyers.org/music/">Contemporary Music</a></p></div><div id="claire_faasse" class="bio_thumbs“><p>Claire Faasse<br /><a href="http://www.newhopefortmyers.org/music/">Organist</a></p></div><div id="elizabeth_daniel" class="bio_thumbs“><p>Elizabeth Daniel<br />Director of Mission and Equipping</p></div><div id="terry_bower" class="bio_thumbs“><p>Terry Bower<br />Lay Pastor</p></div><div id="trish_emler" class="bio_thumbs“><p>Trish Emler<br />Business Administrator</p></div><div id="cecilia_drake" class="bio_thumbs“><p>Cecilia Drake<br />Administration<br />Publication</p></div><div id="kathy_lansberry" class="bio_thumbs“><p>Kathy Lansberry<br />Administration</p></div><div id="marilyn_frayer" class="bio_thumbs“><p>Marilyn Frayer<br />Finance</p></div><div id="robin_cornett" class="bio_thumbs“><p>Robin Cornett<br />Webmaster</p></div><div id="ed_harger" class="bio_thumbs“><p>Ed Harger<br />Facilities</p></div><div id="jc_laird" class="bio_thumbs“><p>JC Laird<br />Facilities</p></div></div><div> At New Hope, we are thankful for our faithful and dedicated paid staff and volunteer staff. Click on the thumbnail images to the left to see more about each staff member below. <div id="curt_mcdaniel_desc" class="bio_desc active_desc“><h3>Curt McDaniel</h3><h4>Interim Senior Pastor</h4><img src="http://www.newhopefortmyers.org/wp- content/themes/newhope_2012/images/bios/curt_mcdaniel.jpg" alt="Curt McDaniel" />Curt is passionate about the clear preaching and teaching of God's Word and about leading God's people in spiritual growth and maturity, mission, outreach and community impact. For more than 27 years, he has served as a pastor or a founding pastor for churches in Alabama, Tennessee, Florida and western Pennsylvania. A native of Chesterfield County, VA, Curt has four earned degrees including a Doctor of Ministry (D.Min.) from Fuller Theological Seminary in Pasadena, CA and most recently a Doctor of Philosophy (Ph.D.) from Duquesne University in Pittsburgh, PA. He is the author of <em>When I Need God the Most</em> (Xulon Press, 2001) and co-author with his wife Karen of the three book series <em>Life Brighteners</em> (Waterbrook Press, 2004). He and Karen enjoy a marriage of over 29 years and have three children: Curtis III (a PCA pastor in Savannah, GA), Megan (a gifted soloist and customer support specialist with a leading cellular phone company) and Heather (a speech-language therapist). Curt enjoys gardening, golf, fishing, history, sports of all kinds (especially MLB) and Bichon Frise dogs.</div> <div id="stu_austin_desc" class="bio_desc" style="display: none;“><h3>Stu Austin</h3><h4>Associate Pastor</h4><img src="http://www.newhopefortmyers.org/wp- content/themes/newhope_2012/images/bios/stu_austin.jpg" alt="Stu Austin" />Stu is proud to serve as New Hope's associate pastor, overseeing pastoral care and outreach ministries. Stu has served in Fort Myers since November of 2002 when he, Ethelanne, and their son Sean moved from Kansas City, Missouri. Stu and Ethelanne are graduates of Gordon-Conwell Theological Seminary and Stu also has an M.B.A. from the University of Texas at Arlington. He is a self confessed adrenalin junkie and has engaged in activities such as white water kayaking, serving as a Navy Chaplain with a Marine Artillery Battalion, sky diving, flying in an acrobatic bi-plane, and most recently, driving a NASCAR race car at the Richard Petty Driving experience. However, his most thrilling experience is seeing men and women identify their Kingdom roles and serve in their area of giftedness. Stu has a special burden for the HIV/AIDS pandemic in southern Africa.</div>
  4. #DOINGITWRONG ▪ It’s not easy enough for a Male Chimp.

    ▪ Everything is locked into one page. ▪ Each thumbnail has its own CSS. Truly. ▪ All images (thumbnails and larger images) are in the theme folder. ▪ The thumbnails are a double image made in Photoshop—one full opacity, one at 50%. ▪ I didn’t make a Custom Post Type or use the Loop.
  5. WHAT WOULD MAKE THIS BETTER? ▪ Each staff person would

    be his own separate post, so easy for the Male Chimp to add/update/remove. ▪ I’d simplify the d@$# CSS. ▪ I’d let the Male Chimp upload images through the Media Uploader. ▪ The thumbnail and the larger image would be the same image. ▪ I’d create a Custom Post Type. And use the Loop.
  6. #DOINGITBETTER All the Male Chimp has to enter is: 1.

    Name. 2. Content. 3. Featured Image.
  7. USE CUSTOM CONTENT TYPES: ▪ Portfolio ▪ Products ▪ Testimonials

    ▪ People/Staff ▪ Events ▪ Galleries ▪ Slides/Sliders
  8. A BASIC CPT add_action( 'init', 'codex_custom_init' ); function codex_custom_init() {

    $args = array( 'public' => true, 'label' => 'Books' ); register_post_type( 'book', $args ); } (http://codex.wordpress.org/Function_Reference/register_post_type#Example)
  9. THEME OR PLUGIN? Build your Custom Post Type within a

    plugin for longer lasting freshness. Why use a plugin instead of a theme? 1. Troubleshooting 2. Editing 3. Future proofing 4. Reusable code 5. Not locking your user into one theme
  10. A complex project can be simplified for the end user

    by setting up custom post meta for content types. Look for Patterns
  11. USE CUSTOM FIELDS: ▪ Social media links (people) ▪ External

    URL (resources) ▪ Dates (events, sermons) ▪ MP3 (sermons, albums) ▪ Related Files ▪ Related Posts (or People, etc.) ▪ For anything that needs to always be within certain parameters
  12. RESOURCES ▪ http://codex.wordpress.org/Post_Types Custom Post Type Generator ▪ http://generatewp.com/post-type/ Custom

    Post Type Plugins ▪ https://wordpress.org/plugins/pods/ ▪ https://wordpress.org/plugins/custom-post-type-ui/
  13. EXAMPLES Custom Metaboxes/Fields ▪ https://github.com/robincornett/simple-listings-genesis Custom Archive Page ▪ https://github.com/robincornett/simple-staff

    Boilerplate ▪ https://github.com/devinsays/portfolio-post-type ▪ https://github.com/tommcfarlin/WordPress-Plugin-Boilerplate