SlideShare a Scribd company logo
Custom Layouts Without
Using Page Templates
Chip Bennett (@chip_bennett)
WordCamp Columbus 2013, 03 August 2013 Custom Layouts Without Using Page TemplatesPage 1 of 24
Overview
• What's the problem?
– Custom Content, Custom Layouts
– How to Have Both
• Process
– Define Post Custom Meta Data
– Modify Template Files
– Define CSS
• Putting it into Practice
– Twenty Twelve Child Theme
• Out of Presentation Scope
– Post Custom Meta Implementation
WordCamp Columbus 2013, 03 August 2013 Custom Layouts Without Using Page TemplatesPage 2 of 24
What's the Problem?
• Purpose of Custom Page Templates
– Custom content!
●
Archives
●
Linkroll
●
Sitemap
●
Contact form
●
Custom queries
• Most Common use of Custom Page Templates
– Layouts
– Full Width
– Different Sidebars
– Etc.
WordCamp Columbus 2013, 03 August 2013 Custom Layouts Without Using Page TemplatesPage 3 of 24
What's the Problem?
• Custom Page Templates for both custom
content and custom layout?
– More templates?
●
Sitemap, Sitemap Full-Width, Sitemap Three-
Column
●
Archives, Archives Full-Width, Archives Three-
Column
●
Linkroll, Linkroll Full-Width, Linkroll Three-Column
• See where this is going?
WordCamp Columbus 2013, 03 August 2013 Custom Layouts Without Using Page TemplatesPage 4 of 24
• Benefits
– Per-page
– Regardless of Page Template
● Page template itself is custom post meta data
● _wp_page_template
• Extra Benefits?
– Can be made to work with (almost) any Theme
– Via Child Theme, Some coding/CSS required
– Expand to other post-types
● Blog Posts, Attachments, Custom Post Types
– Plugin-defined page templates
– Expand/Dovetail with default layout options
WordCamp Columbus 2013, 03 August 2013 Custom Layouts Without Using Page TemplatesPage 5 of 24
Alternative: Custom Post Meta Data
• Let's see an example
• Add custom layouts to Twenty Twelve
• Via Child Theme
• Child Theme Files
– style.css
– functions.php
– sidebar.php
WordCamp Columbus 2013, 03 August 2013 Custom Layouts Without Using Page TemplatesPage 6 of 24
Practical Exercise: Twenty Twelve
Twenty Twelve Live
Example
(Child Theme is available for download)
WordCamp Columbus 2013, 03 August 2013 Custom Layouts Without Using Page TemplatesPage 7 of 24
Implementation
• Define Layout Custom Post Meta Data
– Default layout is two-column
– Add a "Full Width" layout option
• Modify Template According to Layout
– Full Width layout removes the sidebar
– Content takes up resultant space
• Modify CSS According to Layout
– Modify main-content width CSS rule
WordCamp Columbus 2013, 03 August 2013 Custom Layouts Without Using Page TemplatesPage 8 of 24
Define Layout Custom Post Meta
• Recommended Nomenclature:
– _themename_layout
●
Underscore: hide from custom fields UI
●
Namespacing: avoid conflicts, per-Theme
• Reminder:
– Custom Post Meta code is out of presentation scope
– Working example will be provided
WordCamp Columbus 2013, 03 August 2013 Custom Layouts Without Using Page TemplatesPage 9 of 24
Custom Function to Get Layout
• Define a function to get the current layout
function themename_get_layout() {
$layout = 'default';
global $post;
$post_layout = get_post_meta( $post->ID,
'_themename_layout', true );
if ( '' != $post_layout ) {
$layout = $post_layout;
}
return $layout;
}
• We'll use this in a couple places, so DRY
WordCamp Columbus 2013, 03 August 2013 Custom Layouts Without Using Page TemplatesPage 10 of 24
Modify the Template
• Typical template file:
<?php
// Header
get_header();
// Loop
get_template_part( 'loop' );
// Sidebar
get_sidebar();
// Footer
get_footer();
?>
• index.php, archive.php, etc.
• Child Theme: let's just modify sidebar.php
WordCamp Columbus 2013, 03 August 2013 Custom Layouts Without Using Page TemplatesPage 11 of 24
Modify the Template
• Modifying sidebar.php
– Before:
<?php
// Sidebar HTML Markup & PHP code
?>
– After:
<?php
if ( 'full' == themename_get_layout() ) {
return;
}
// Sidebar HTML Markup & PHP code
?>
• Adapt as needed
WordCamp Columbus 2013, 03 August 2013 Custom Layouts Without Using Page TemplatesPage 12 of 24
Modify CSS
• Add Layout-Based Classes to <body> tag
– Use body_class filter:
function themename_filter_body_class( $classes ) {
$classes[] = 'layout-' . themename_get_layout();
return $classes;
}
add_filter( 'body_class',
'themename_filter_body_class' );
• Result:
<body class="page template-default layout-full ...">
• Child Theme: add layout-based CSS rules:
body.layout-full #content {
width: 100%;
}
WordCamp Columbus 2013, 03 August 2013 Custom Layouts Without Using Page TemplatesPage 13 of 24
Links and Resources
• Downloads
– WCCbus13 TwentyTwelve Child Theme
https://github.com/chipbennett/wccbus13-twentytwelve-child
– Oenology Theme (advanced layout example)
https://github.com/chipbennett/oenology
• Codex References
– http://codex.wordpress.org/Pages#Page_Templates
– http://codex.wordpress.org/Function_Reference/get_post_meta
– http://codex.wordpress.org/Function_Reference/update_post_meta
– http://codex.wordpress.org/Function_Reference/add_meta_box
– http://codex.wordpress.org/Plugin_API/Filter_Reference/body_class
• Presentation Slides
– http://www.slideshare.net/chipbennett
WordCamp Columbus 2013, 03 August 2013 Custom Layouts Without Using Page TemplatesPage 14 of 24
Feedback
• Questions or comments?
WordCamp Columbus 2013, 03 August 2013 Custom Layouts Without Using Page TemplatesPage 15 of 24
Bonus Presentation:
Default Theme Options: You're
(probably) _doing_it_wrong()
Chip Bennett (@chip_bennett)
WordCamp Columbus 2013, 03 August 2013 Custom Layouts Without Using Page TemplatesPage 16 of 24
What's the Problem?
• Unnecessary wp_options DB entries
• Poor UX
• Out-of-the-box Theme output broken
• Difficult to add new options
WordCamp Columbus 2013, 03 August 2013 Custom Layouts Without Using Page TemplatesPage 17 of 24
• Benefits
– No DB entries required
– No Theme activation redirects
– Theme works out of the box
– Easy to add new options
• Extra Benefits?
– Theme looks awesome in WPORG preview
– Easy to add custom filter (extend options via Child
Theme)
WordCamp Columbus 2013, 03 August 2013 Custom Layouts Without Using Page TemplatesPage 18 of 24
Alternative: Sane Defaults
Implementation
• Define option defaults as an array
– Wrap in a custom function
– PROTIP: add a custom filter
• Parse option defaults array with get_option()
– Wrap in a custom function
• Call custom function in the template instead
of get_option() directly
• Profit
WordCamp Columbus 2013, 03 August 2013 Custom Layouts Without Using Page TemplatesPage 19 of 24
Define Option Defaults as an Array
• Define option defaults as an array
function themename_get_option_defaults() {
$defaults = array(
'option_a' => 'value_a',
'option_b' => 'value_b'
);
return $defaults;
}
• Flexible, Extensible
– Wrap in a custom function
– PROTIP: add a custom filter
return apply_filters( 'themename_option_defaults',
$defaults );
WordCamp Columbus 2013, 03 August 2013 Custom Layouts Without Using Page TemplatesPage 20 of 24
Parse defaults with get_option()
• Wrap in a custom function
function themename_get_options() {
$defaults = themename_get_option_defaults();
$options = wp_parse_args(
get_option(
'theme_themename_options',
array()
),
$option_defaults
);
return $options;
}
– The magic: wp_parse_args()
– Note: pass array() as second arg to get_option()
WordCamp Columbus 2013, 03 August 2013 Custom Layouts Without Using Page TemplatesPage 21 of 24
Call Custom Function in Template
• Call custom function instead of get_option()
– Before:
<?php
global $themename_options;
$themename_options =
get_option( 'theme_themename_options' );
?>
– After:
<?php
global $themename_options;
$themename_options = themename_get_options();
?>
• Adapt as needed
WordCamp Columbus 2013, 03 August 2013 Custom Layouts Without Using Page TemplatesPage 22 of 24
Profit
• Theme works out-of-the-box
– No options DB entry? No problem!
– Theme activation? Ain't nobody got time fo' that!
– WPORG preview? Knows default options!
• Change/Add Defaults
– Modify themename_get_option_defaults()
– Bonus: WPORG will show the updated defaults
• Child Theme change/add defaults
– add_filter(
'themename_option_defaults',
'childtheme_function'
);
WordCamp Columbus 2013, 03 August 2013 Custom Layouts Without Using Page TemplatesPage 23 of 24
Links and Resources
• Codex References
– http://codex.wordpress.org/Function_Reference/wp_parse_args
– http://codex.wordpress.org/Function_Reference/get_option
– http://codex.wordpress.org/Function_Reference/apply_filters
– http://codex.wordpress.org/Function_Reference/add_filter
WordCamp Columbus 2013, 03 August 2013 Custom Layouts Without Using Page TemplatesPage 24 of 24

More Related Content

Viewers also liked

Disruptive strategies
Disruptive strategies Disruptive strategies
Disruptive strategies
ricochet
 
Studio Peter Van Riet // Portfolio 2013
Studio Peter Van Riet // Portfolio 2013Studio Peter Van Riet // Portfolio 2013
Studio Peter Van Riet // Portfolio 2013
Peter Van Riet
 
Disruptive strategies
Disruptive strategies Disruptive strategies
Disruptive strategies
ricochet
 
Sanitation Research_Outline_Final
Sanitation Research_Outline_FinalSanitation Research_Outline_Final
Sanitation Research_Outline_Final
The Potty Project
 
Design Trends 2013
Design Trends 2013Design Trends 2013
Design Trends 2013
THE MEME Design
 
QÃ?! – Uma Jornada em Qualidade de Software
QÃ?! – Uma Jornada em Qualidade de SoftwareQÃ?! – Uma Jornada em Qualidade de Software
QÃ?! – Uma Jornada em Qualidade de Software
m Peixoto
 
Bootcampbootleg2010v2slim 1
Bootcampbootleg2010v2slim 1Bootcampbootleg2010v2slim 1
Bootcampbootleg2010v2slim 1
ricochet
 
Stop, look and listen v2
Stop, look and listen v2Stop, look and listen v2
Stop, look and listen v2
ricochet
 
O que tu queres?
O que tu queres? O que tu queres?
O que tu queres?
m Peixoto
 

Viewers also liked (9)

Disruptive strategies
Disruptive strategies Disruptive strategies
Disruptive strategies
 
Studio Peter Van Riet // Portfolio 2013
Studio Peter Van Riet // Portfolio 2013Studio Peter Van Riet // Portfolio 2013
Studio Peter Van Riet // Portfolio 2013
 
Disruptive strategies
Disruptive strategies Disruptive strategies
Disruptive strategies
 
Sanitation Research_Outline_Final
Sanitation Research_Outline_FinalSanitation Research_Outline_Final
Sanitation Research_Outline_Final
 
Design Trends 2013
Design Trends 2013Design Trends 2013
Design Trends 2013
 
QÃ?! – Uma Jornada em Qualidade de Software
QÃ?! – Uma Jornada em Qualidade de SoftwareQÃ?! – Uma Jornada em Qualidade de Software
QÃ?! – Uma Jornada em Qualidade de Software
 
Bootcampbootleg2010v2slim 1
Bootcampbootleg2010v2slim 1Bootcampbootleg2010v2slim 1
Bootcampbootleg2010v2slim 1
 
Stop, look and listen v2
Stop, look and listen v2Stop, look and listen v2
Stop, look and listen v2
 
O que tu queres?
O que tu queres? O que tu queres?
O que tu queres?
 

Similar to WordCamp Columbus 2013: Custom Layouts Without Using Page Templates

WordCamp Nashville 2013 - Custom Layouts Without Custom Page Templates
WordCamp Nashville 2013 - Custom Layouts Without Custom Page TemplatesWordCamp Nashville 2013 - Custom Layouts Without Custom Page Templates
WordCamp Nashville 2013 - Custom Layouts Without Custom Page Templates
Chip Bennett
 
Writing an extensible web testing framework ready for the cloud slide share
Writing an extensible web testing framework ready for the cloud   slide shareWriting an extensible web testing framework ready for the cloud   slide share
Writing an extensible web testing framework ready for the cloud slide share
Mike Ensor
 
Blackboard DevCon 2011 - Performance Considerations for Custom Theme Development
Blackboard DevCon 2011 - Performance Considerations for Custom Theme DevelopmentBlackboard DevCon 2011 - Performance Considerations for Custom Theme Development
Blackboard DevCon 2011 - Performance Considerations for Custom Theme Development
Noriaki Tatsumi
 
Introduction Django
Introduction DjangoIntroduction Django
Introduction Django
Wade Austin
 
WordPress Accessibility: WordCamp Chicago
WordPress Accessibility: WordCamp ChicagoWordPress Accessibility: WordCamp Chicago
WordPress Accessibility: WordCamp Chicago
Joseph Dolson
 
IBM Connect 2016 - AD1548 - Building Responsive XPages Applications
IBM Connect 2016 - AD1548 - Building Responsive XPages ApplicationsIBM Connect 2016 - AD1548 - Building Responsive XPages Applications
IBM Connect 2016 - AD1548 - Building Responsive XPages Applications
beglee
 
Odoo Experience 2018 - From a Web Controller to a Full CMS
Odoo Experience 2018 - From a Web Controller to a Full CMSOdoo Experience 2018 - From a Web Controller to a Full CMS
Odoo Experience 2018 - From a Web Controller to a Full CMS
ElínAnna Jónasdóttir
 
Compile your Style
Compile your StyleCompile your Style
Compile your Style
Ragnar Kurm
 
Bb Tour ANZ 2017 - Workshop - Enhancing Learn CSS
Bb Tour ANZ 2017 - Workshop - Enhancing Learn CSSBb Tour ANZ 2017 - Workshop - Enhancing Learn CSS
Bb Tour ANZ 2017 - Workshop - Enhancing Learn CSS
Blackboard APAC
 
Stupid Index Block Tricks
Stupid Index Block TricksStupid Index Block Tricks
Stupid Index Block Tricks
hannonhill
 
javaScript.ppt
javaScript.pptjavaScript.ppt
javaScript.ppt
sentayehu
 
2017 - TYPO3 CertiFUNcation: Peter Kraume - Showing a useful TYPO3 Backend
2017 - TYPO3 CertiFUNcation: Peter Kraume - Showing a useful TYPO3 Backend2017 - TYPO3 CertiFUNcation: Peter Kraume - Showing a useful TYPO3 Backend
2017 - TYPO3 CertiFUNcation: Peter Kraume - Showing a useful TYPO3 Backend
TYPO3 CertiFUNcation
 
TYPO3 best practice - showing a useful TYPO3 backend
TYPO3 best practice - showing a useful TYPO3 backendTYPO3 best practice - showing a useful TYPO3 backend
TYPO3 best practice - showing a useful TYPO3 backend
Peter Kraume
 
Django introduction @ UGent
Django introduction @ UGentDjango introduction @ UGent
Django introduction @ UGent
kevinvw
 
Building Responsive Applications Using XPages
Building Responsive Applications Using XPagesBuilding Responsive Applications Using XPages
Building Responsive Applications Using XPages
Teamstudio
 
Advanced Thesis Techniques and Tricks
Advanced Thesis Techniques and TricksAdvanced Thesis Techniques and Tricks
Advanced Thesis Techniques and Tricks
Brad Williams
 
OroCommerce Storefront Design. Non-standard Layout Customisation.
OroCommerce Storefront Design. Non-standard Layout Customisation.OroCommerce Storefront Design. Non-standard Layout Customisation.
OroCommerce Storefront Design. Non-standard Layout Customisation.
Andrew Yatsenko
 
Writing extensible Word press-plugins
Writing extensible Word press-pluginsWriting extensible Word press-plugins
Writing extensible Word press-plugins
AllenSnook
 
Module 3 - Intro to Bootstrap
Module 3 - Intro to BootstrapModule 3 - Intro to Bootstrap
Module 3 - Intro to Bootstrap
Katherine McCurdy-Lapierre, R.G.D.
 
WordPress Theme and Plugin Optimization - WordPress Arvika March '14
WordPress Theme and Plugin Optimization - WordPress Arvika March '14WordPress Theme and Plugin Optimization - WordPress Arvika March '14
WordPress Theme and Plugin Optimization - WordPress Arvika March '14
slobodanmanic
 

Similar to WordCamp Columbus 2013: Custom Layouts Without Using Page Templates (20)

WordCamp Nashville 2013 - Custom Layouts Without Custom Page Templates
WordCamp Nashville 2013 - Custom Layouts Without Custom Page TemplatesWordCamp Nashville 2013 - Custom Layouts Without Custom Page Templates
WordCamp Nashville 2013 - Custom Layouts Without Custom Page Templates
 
Writing an extensible web testing framework ready for the cloud slide share
Writing an extensible web testing framework ready for the cloud   slide shareWriting an extensible web testing framework ready for the cloud   slide share
Writing an extensible web testing framework ready for the cloud slide share
 
Blackboard DevCon 2011 - Performance Considerations for Custom Theme Development
Blackboard DevCon 2011 - Performance Considerations for Custom Theme DevelopmentBlackboard DevCon 2011 - Performance Considerations for Custom Theme Development
Blackboard DevCon 2011 - Performance Considerations for Custom Theme Development
 
Introduction Django
Introduction DjangoIntroduction Django
Introduction Django
 
WordPress Accessibility: WordCamp Chicago
WordPress Accessibility: WordCamp ChicagoWordPress Accessibility: WordCamp Chicago
WordPress Accessibility: WordCamp Chicago
 
IBM Connect 2016 - AD1548 - Building Responsive XPages Applications
IBM Connect 2016 - AD1548 - Building Responsive XPages ApplicationsIBM Connect 2016 - AD1548 - Building Responsive XPages Applications
IBM Connect 2016 - AD1548 - Building Responsive XPages Applications
 
Odoo Experience 2018 - From a Web Controller to a Full CMS
Odoo Experience 2018 - From a Web Controller to a Full CMSOdoo Experience 2018 - From a Web Controller to a Full CMS
Odoo Experience 2018 - From a Web Controller to a Full CMS
 
Compile your Style
Compile your StyleCompile your Style
Compile your Style
 
Bb Tour ANZ 2017 - Workshop - Enhancing Learn CSS
Bb Tour ANZ 2017 - Workshop - Enhancing Learn CSSBb Tour ANZ 2017 - Workshop - Enhancing Learn CSS
Bb Tour ANZ 2017 - Workshop - Enhancing Learn CSS
 
Stupid Index Block Tricks
Stupid Index Block TricksStupid Index Block Tricks
Stupid Index Block Tricks
 
javaScript.ppt
javaScript.pptjavaScript.ppt
javaScript.ppt
 
2017 - TYPO3 CertiFUNcation: Peter Kraume - Showing a useful TYPO3 Backend
2017 - TYPO3 CertiFUNcation: Peter Kraume - Showing a useful TYPO3 Backend2017 - TYPO3 CertiFUNcation: Peter Kraume - Showing a useful TYPO3 Backend
2017 - TYPO3 CertiFUNcation: Peter Kraume - Showing a useful TYPO3 Backend
 
TYPO3 best practice - showing a useful TYPO3 backend
TYPO3 best practice - showing a useful TYPO3 backendTYPO3 best practice - showing a useful TYPO3 backend
TYPO3 best practice - showing a useful TYPO3 backend
 
Django introduction @ UGent
Django introduction @ UGentDjango introduction @ UGent
Django introduction @ UGent
 
Building Responsive Applications Using XPages
Building Responsive Applications Using XPagesBuilding Responsive Applications Using XPages
Building Responsive Applications Using XPages
 
Advanced Thesis Techniques and Tricks
Advanced Thesis Techniques and TricksAdvanced Thesis Techniques and Tricks
Advanced Thesis Techniques and Tricks
 
OroCommerce Storefront Design. Non-standard Layout Customisation.
OroCommerce Storefront Design. Non-standard Layout Customisation.OroCommerce Storefront Design. Non-standard Layout Customisation.
OroCommerce Storefront Design. Non-standard Layout Customisation.
 
Writing extensible Word press-plugins
Writing extensible Word press-pluginsWriting extensible Word press-plugins
Writing extensible Word press-plugins
 
Module 3 - Intro to Bootstrap
Module 3 - Intro to BootstrapModule 3 - Intro to Bootstrap
Module 3 - Intro to Bootstrap
 
WordPress Theme and Plugin Optimization - WordPress Arvika March '14
WordPress Theme and Plugin Optimization - WordPress Arvika March '14WordPress Theme and Plugin Optimization - WordPress Arvika March '14
WordPress Theme and Plugin Optimization - WordPress Arvika March '14
 

Recently uploaded

Christine's Product Research Presentation.pptx
Christine's Product Research Presentation.pptxChristine's Product Research Presentation.pptx
Christine's Product Research Presentation.pptx
christinelarrosa
 
GraphRAG for LifeSciences Hands-On with the Clinical Knowledge Graph
GraphRAG for LifeSciences Hands-On with the Clinical Knowledge GraphGraphRAG for LifeSciences Hands-On with the Clinical Knowledge Graph
GraphRAG for LifeSciences Hands-On with the Clinical Knowledge Graph
Neo4j
 
"Scaling RAG Applications to serve millions of users", Kevin Goedecke
"Scaling RAG Applications to serve millions of users",  Kevin Goedecke"Scaling RAG Applications to serve millions of users",  Kevin Goedecke
"Scaling RAG Applications to serve millions of users", Kevin Goedecke
Fwdays
 
Freshworks Rethinks NoSQL for Rapid Scaling & Cost-Efficiency
Freshworks Rethinks NoSQL for Rapid Scaling & Cost-EfficiencyFreshworks Rethinks NoSQL for Rapid Scaling & Cost-Efficiency
Freshworks Rethinks NoSQL for Rapid Scaling & Cost-Efficiency
ScyllaDB
 
Northern Engraving | Nameplate Manufacturing Process - 2024
Northern Engraving | Nameplate Manufacturing Process - 2024Northern Engraving | Nameplate Manufacturing Process - 2024
Northern Engraving | Nameplate Manufacturing Process - 2024
Northern Engraving
 
"What does it really mean for your system to be available, or how to define w...
"What does it really mean for your system to be available, or how to define w..."What does it really mean for your system to be available, or how to define w...
"What does it really mean for your system to be available, or how to define w...
Fwdays
 
"NATO Hackathon Winner: AI-Powered Drug Search", Taras Kloba
"NATO Hackathon Winner: AI-Powered Drug Search",  Taras Kloba"NATO Hackathon Winner: AI-Powered Drug Search",  Taras Kloba
"NATO Hackathon Winner: AI-Powered Drug Search", Taras Kloba
Fwdays
 
Session 1 - Intro to Robotic Process Automation.pdf
Session 1 - Intro to Robotic Process Automation.pdfSession 1 - Intro to Robotic Process Automation.pdf
Session 1 - Intro to Robotic Process Automation.pdf
UiPathCommunity
 
"Frontline Battles with DDoS: Best practices and Lessons Learned", Igor Ivaniuk
"Frontline Battles with DDoS: Best practices and Lessons Learned",  Igor Ivaniuk"Frontline Battles with DDoS: Best practices and Lessons Learned",  Igor Ivaniuk
"Frontline Battles with DDoS: Best practices and Lessons Learned", Igor Ivaniuk
Fwdays
 
[OReilly Superstream] Occupy the Space: A grassroots guide to engineering (an...
[OReilly Superstream] Occupy the Space: A grassroots guide to engineering (an...[OReilly Superstream] Occupy the Space: A grassroots guide to engineering (an...
[OReilly Superstream] Occupy the Space: A grassroots guide to engineering (an...
Jason Yip
 
Astute Business Solutions | Oracle Cloud Partner |
Astute Business Solutions | Oracle Cloud Partner |Astute Business Solutions | Oracle Cloud Partner |
Astute Business Solutions | Oracle Cloud Partner |
AstuteBusiness
 
Nordic Marketo Engage User Group_June 13_ 2024.pptx
Nordic Marketo Engage User Group_June 13_ 2024.pptxNordic Marketo Engage User Group_June 13_ 2024.pptx
Nordic Marketo Engage User Group_June 13_ 2024.pptx
MichaelKnudsen27
 
Day 2 - Intro to UiPath Studio Fundamentals
Day 2 - Intro to UiPath Studio FundamentalsDay 2 - Intro to UiPath Studio Fundamentals
Day 2 - Intro to UiPath Studio Fundamentals
UiPathCommunity
 
Leveraging the Graph for Clinical Trials and Standards
Leveraging the Graph for Clinical Trials and StandardsLeveraging the Graph for Clinical Trials and Standards
Leveraging the Graph for Clinical Trials and Standards
Neo4j
 
inQuba Webinar Mastering Customer Journey Management with Dr Graham Hill
inQuba Webinar Mastering Customer Journey Management with Dr Graham HillinQuba Webinar Mastering Customer Journey Management with Dr Graham Hill
inQuba Webinar Mastering Customer Journey Management with Dr Graham Hill
LizaNolte
 
How to Interpret Trends in the Kalyan Rajdhani Mix Chart.pdf
How to Interpret Trends in the Kalyan Rajdhani Mix Chart.pdfHow to Interpret Trends in the Kalyan Rajdhani Mix Chart.pdf
How to Interpret Trends in the Kalyan Rajdhani Mix Chart.pdf
Chart Kalyan
 
QA or the Highway - Component Testing: Bridging the gap between frontend appl...
QA or the Highway - Component Testing: Bridging the gap between frontend appl...QA or the Highway - Component Testing: Bridging the gap between frontend appl...
QA or the Highway - Component Testing: Bridging the gap between frontend appl...
zjhamm304
 
High performance Serverless Java on AWS- GoTo Amsterdam 2024
High performance Serverless Java on AWS- GoTo Amsterdam 2024High performance Serverless Java on AWS- GoTo Amsterdam 2024
High performance Serverless Java on AWS- GoTo Amsterdam 2024
Vadym Kazulkin
 
PRODUCT LISTING OPTIMIZATION PRESENTATION.pptx
PRODUCT LISTING OPTIMIZATION PRESENTATION.pptxPRODUCT LISTING OPTIMIZATION PRESENTATION.pptx
PRODUCT LISTING OPTIMIZATION PRESENTATION.pptx
christinelarrosa
 
Harnessing the Power of NLP and Knowledge Graphs for Opioid Research
Harnessing the Power of NLP and Knowledge Graphs for Opioid ResearchHarnessing the Power of NLP and Knowledge Graphs for Opioid Research
Harnessing the Power of NLP and Knowledge Graphs for Opioid Research
Neo4j
 

Recently uploaded (20)

Christine's Product Research Presentation.pptx
Christine's Product Research Presentation.pptxChristine's Product Research Presentation.pptx
Christine's Product Research Presentation.pptx
 
GraphRAG for LifeSciences Hands-On with the Clinical Knowledge Graph
GraphRAG for LifeSciences Hands-On with the Clinical Knowledge GraphGraphRAG for LifeSciences Hands-On with the Clinical Knowledge Graph
GraphRAG for LifeSciences Hands-On with the Clinical Knowledge Graph
 
"Scaling RAG Applications to serve millions of users", Kevin Goedecke
"Scaling RAG Applications to serve millions of users",  Kevin Goedecke"Scaling RAG Applications to serve millions of users",  Kevin Goedecke
"Scaling RAG Applications to serve millions of users", Kevin Goedecke
 
Freshworks Rethinks NoSQL for Rapid Scaling & Cost-Efficiency
Freshworks Rethinks NoSQL for Rapid Scaling & Cost-EfficiencyFreshworks Rethinks NoSQL for Rapid Scaling & Cost-Efficiency
Freshworks Rethinks NoSQL for Rapid Scaling & Cost-Efficiency
 
Northern Engraving | Nameplate Manufacturing Process - 2024
Northern Engraving | Nameplate Manufacturing Process - 2024Northern Engraving | Nameplate Manufacturing Process - 2024
Northern Engraving | Nameplate Manufacturing Process - 2024
 
"What does it really mean for your system to be available, or how to define w...
"What does it really mean for your system to be available, or how to define w..."What does it really mean for your system to be available, or how to define w...
"What does it really mean for your system to be available, or how to define w...
 
"NATO Hackathon Winner: AI-Powered Drug Search", Taras Kloba
"NATO Hackathon Winner: AI-Powered Drug Search",  Taras Kloba"NATO Hackathon Winner: AI-Powered Drug Search",  Taras Kloba
"NATO Hackathon Winner: AI-Powered Drug Search", Taras Kloba
 
Session 1 - Intro to Robotic Process Automation.pdf
Session 1 - Intro to Robotic Process Automation.pdfSession 1 - Intro to Robotic Process Automation.pdf
Session 1 - Intro to Robotic Process Automation.pdf
 
"Frontline Battles with DDoS: Best practices and Lessons Learned", Igor Ivaniuk
"Frontline Battles with DDoS: Best practices and Lessons Learned",  Igor Ivaniuk"Frontline Battles with DDoS: Best practices and Lessons Learned",  Igor Ivaniuk
"Frontline Battles with DDoS: Best practices and Lessons Learned", Igor Ivaniuk
 
[OReilly Superstream] Occupy the Space: A grassroots guide to engineering (an...
[OReilly Superstream] Occupy the Space: A grassroots guide to engineering (an...[OReilly Superstream] Occupy the Space: A grassroots guide to engineering (an...
[OReilly Superstream] Occupy the Space: A grassroots guide to engineering (an...
 
Astute Business Solutions | Oracle Cloud Partner |
Astute Business Solutions | Oracle Cloud Partner |Astute Business Solutions | Oracle Cloud Partner |
Astute Business Solutions | Oracle Cloud Partner |
 
Nordic Marketo Engage User Group_June 13_ 2024.pptx
Nordic Marketo Engage User Group_June 13_ 2024.pptxNordic Marketo Engage User Group_June 13_ 2024.pptx
Nordic Marketo Engage User Group_June 13_ 2024.pptx
 
Day 2 - Intro to UiPath Studio Fundamentals
Day 2 - Intro to UiPath Studio FundamentalsDay 2 - Intro to UiPath Studio Fundamentals
Day 2 - Intro to UiPath Studio Fundamentals
 
Leveraging the Graph for Clinical Trials and Standards
Leveraging the Graph for Clinical Trials and StandardsLeveraging the Graph for Clinical Trials and Standards
Leveraging the Graph for Clinical Trials and Standards
 
inQuba Webinar Mastering Customer Journey Management with Dr Graham Hill
inQuba Webinar Mastering Customer Journey Management with Dr Graham HillinQuba Webinar Mastering Customer Journey Management with Dr Graham Hill
inQuba Webinar Mastering Customer Journey Management with Dr Graham Hill
 
How to Interpret Trends in the Kalyan Rajdhani Mix Chart.pdf
How to Interpret Trends in the Kalyan Rajdhani Mix Chart.pdfHow to Interpret Trends in the Kalyan Rajdhani Mix Chart.pdf
How to Interpret Trends in the Kalyan Rajdhani Mix Chart.pdf
 
QA or the Highway - Component Testing: Bridging the gap between frontend appl...
QA or the Highway - Component Testing: Bridging the gap between frontend appl...QA or the Highway - Component Testing: Bridging the gap between frontend appl...
QA or the Highway - Component Testing: Bridging the gap between frontend appl...
 
High performance Serverless Java on AWS- GoTo Amsterdam 2024
High performance Serverless Java on AWS- GoTo Amsterdam 2024High performance Serverless Java on AWS- GoTo Amsterdam 2024
High performance Serverless Java on AWS- GoTo Amsterdam 2024
 
PRODUCT LISTING OPTIMIZATION PRESENTATION.pptx
PRODUCT LISTING OPTIMIZATION PRESENTATION.pptxPRODUCT LISTING OPTIMIZATION PRESENTATION.pptx
PRODUCT LISTING OPTIMIZATION PRESENTATION.pptx
 
Harnessing the Power of NLP and Knowledge Graphs for Opioid Research
Harnessing the Power of NLP and Knowledge Graphs for Opioid ResearchHarnessing the Power of NLP and Knowledge Graphs for Opioid Research
Harnessing the Power of NLP and Knowledge Graphs for Opioid Research
 

WordCamp Columbus 2013: Custom Layouts Without Using Page Templates

  • 1. Custom Layouts Without Using Page Templates Chip Bennett (@chip_bennett) WordCamp Columbus 2013, 03 August 2013 Custom Layouts Without Using Page TemplatesPage 1 of 24
  • 2. Overview • What's the problem? – Custom Content, Custom Layouts – How to Have Both • Process – Define Post Custom Meta Data – Modify Template Files – Define CSS • Putting it into Practice – Twenty Twelve Child Theme • Out of Presentation Scope – Post Custom Meta Implementation WordCamp Columbus 2013, 03 August 2013 Custom Layouts Without Using Page TemplatesPage 2 of 24
  • 3. What's the Problem? • Purpose of Custom Page Templates – Custom content! ● Archives ● Linkroll ● Sitemap ● Contact form ● Custom queries • Most Common use of Custom Page Templates – Layouts – Full Width – Different Sidebars – Etc. WordCamp Columbus 2013, 03 August 2013 Custom Layouts Without Using Page TemplatesPage 3 of 24
  • 4. What's the Problem? • Custom Page Templates for both custom content and custom layout? – More templates? ● Sitemap, Sitemap Full-Width, Sitemap Three- Column ● Archives, Archives Full-Width, Archives Three- Column ● Linkroll, Linkroll Full-Width, Linkroll Three-Column • See where this is going? WordCamp Columbus 2013, 03 August 2013 Custom Layouts Without Using Page TemplatesPage 4 of 24
  • 5. • Benefits – Per-page – Regardless of Page Template ● Page template itself is custom post meta data ● _wp_page_template • Extra Benefits? – Can be made to work with (almost) any Theme – Via Child Theme, Some coding/CSS required – Expand to other post-types ● Blog Posts, Attachments, Custom Post Types – Plugin-defined page templates – Expand/Dovetail with default layout options WordCamp Columbus 2013, 03 August 2013 Custom Layouts Without Using Page TemplatesPage 5 of 24 Alternative: Custom Post Meta Data
  • 6. • Let's see an example • Add custom layouts to Twenty Twelve • Via Child Theme • Child Theme Files – style.css – functions.php – sidebar.php WordCamp Columbus 2013, 03 August 2013 Custom Layouts Without Using Page TemplatesPage 6 of 24 Practical Exercise: Twenty Twelve
  • 7. Twenty Twelve Live Example (Child Theme is available for download) WordCamp Columbus 2013, 03 August 2013 Custom Layouts Without Using Page TemplatesPage 7 of 24
  • 8. Implementation • Define Layout Custom Post Meta Data – Default layout is two-column – Add a "Full Width" layout option • Modify Template According to Layout – Full Width layout removes the sidebar – Content takes up resultant space • Modify CSS According to Layout – Modify main-content width CSS rule WordCamp Columbus 2013, 03 August 2013 Custom Layouts Without Using Page TemplatesPage 8 of 24
  • 9. Define Layout Custom Post Meta • Recommended Nomenclature: – _themename_layout ● Underscore: hide from custom fields UI ● Namespacing: avoid conflicts, per-Theme • Reminder: – Custom Post Meta code is out of presentation scope – Working example will be provided WordCamp Columbus 2013, 03 August 2013 Custom Layouts Without Using Page TemplatesPage 9 of 24
  • 10. Custom Function to Get Layout • Define a function to get the current layout function themename_get_layout() { $layout = 'default'; global $post; $post_layout = get_post_meta( $post->ID, '_themename_layout', true ); if ( '' != $post_layout ) { $layout = $post_layout; } return $layout; } • We'll use this in a couple places, so DRY WordCamp Columbus 2013, 03 August 2013 Custom Layouts Without Using Page TemplatesPage 10 of 24
  • 11. Modify the Template • Typical template file: <?php // Header get_header(); // Loop get_template_part( 'loop' ); // Sidebar get_sidebar(); // Footer get_footer(); ?> • index.php, archive.php, etc. • Child Theme: let's just modify sidebar.php WordCamp Columbus 2013, 03 August 2013 Custom Layouts Without Using Page TemplatesPage 11 of 24
  • 12. Modify the Template • Modifying sidebar.php – Before: <?php // Sidebar HTML Markup & PHP code ?> – After: <?php if ( 'full' == themename_get_layout() ) { return; } // Sidebar HTML Markup & PHP code ?> • Adapt as needed WordCamp Columbus 2013, 03 August 2013 Custom Layouts Without Using Page TemplatesPage 12 of 24
  • 13. Modify CSS • Add Layout-Based Classes to <body> tag – Use body_class filter: function themename_filter_body_class( $classes ) { $classes[] = 'layout-' . themename_get_layout(); return $classes; } add_filter( 'body_class', 'themename_filter_body_class' ); • Result: <body class="page template-default layout-full ..."> • Child Theme: add layout-based CSS rules: body.layout-full #content { width: 100%; } WordCamp Columbus 2013, 03 August 2013 Custom Layouts Without Using Page TemplatesPage 13 of 24
  • 14. Links and Resources • Downloads – WCCbus13 TwentyTwelve Child Theme https://github.com/chipbennett/wccbus13-twentytwelve-child – Oenology Theme (advanced layout example) https://github.com/chipbennett/oenology • Codex References – http://codex.wordpress.org/Pages#Page_Templates – http://codex.wordpress.org/Function_Reference/get_post_meta – http://codex.wordpress.org/Function_Reference/update_post_meta – http://codex.wordpress.org/Function_Reference/add_meta_box – http://codex.wordpress.org/Plugin_API/Filter_Reference/body_class • Presentation Slides – http://www.slideshare.net/chipbennett WordCamp Columbus 2013, 03 August 2013 Custom Layouts Without Using Page TemplatesPage 14 of 24
  • 15. Feedback • Questions or comments? WordCamp Columbus 2013, 03 August 2013 Custom Layouts Without Using Page TemplatesPage 15 of 24
  • 16. Bonus Presentation: Default Theme Options: You're (probably) _doing_it_wrong() Chip Bennett (@chip_bennett) WordCamp Columbus 2013, 03 August 2013 Custom Layouts Without Using Page TemplatesPage 16 of 24
  • 17. What's the Problem? • Unnecessary wp_options DB entries • Poor UX • Out-of-the-box Theme output broken • Difficult to add new options WordCamp Columbus 2013, 03 August 2013 Custom Layouts Without Using Page TemplatesPage 17 of 24
  • 18. • Benefits – No DB entries required – No Theme activation redirects – Theme works out of the box – Easy to add new options • Extra Benefits? – Theme looks awesome in WPORG preview – Easy to add custom filter (extend options via Child Theme) WordCamp Columbus 2013, 03 August 2013 Custom Layouts Without Using Page TemplatesPage 18 of 24 Alternative: Sane Defaults
  • 19. Implementation • Define option defaults as an array – Wrap in a custom function – PROTIP: add a custom filter • Parse option defaults array with get_option() – Wrap in a custom function • Call custom function in the template instead of get_option() directly • Profit WordCamp Columbus 2013, 03 August 2013 Custom Layouts Without Using Page TemplatesPage 19 of 24
  • 20. Define Option Defaults as an Array • Define option defaults as an array function themename_get_option_defaults() { $defaults = array( 'option_a' => 'value_a', 'option_b' => 'value_b' ); return $defaults; } • Flexible, Extensible – Wrap in a custom function – PROTIP: add a custom filter return apply_filters( 'themename_option_defaults', $defaults ); WordCamp Columbus 2013, 03 August 2013 Custom Layouts Without Using Page TemplatesPage 20 of 24
  • 21. Parse defaults with get_option() • Wrap in a custom function function themename_get_options() { $defaults = themename_get_option_defaults(); $options = wp_parse_args( get_option( 'theme_themename_options', array() ), $option_defaults ); return $options; } – The magic: wp_parse_args() – Note: pass array() as second arg to get_option() WordCamp Columbus 2013, 03 August 2013 Custom Layouts Without Using Page TemplatesPage 21 of 24
  • 22. Call Custom Function in Template • Call custom function instead of get_option() – Before: <?php global $themename_options; $themename_options = get_option( 'theme_themename_options' ); ?> – After: <?php global $themename_options; $themename_options = themename_get_options(); ?> • Adapt as needed WordCamp Columbus 2013, 03 August 2013 Custom Layouts Without Using Page TemplatesPage 22 of 24
  • 23. Profit • Theme works out-of-the-box – No options DB entry? No problem! – Theme activation? Ain't nobody got time fo' that! – WPORG preview? Knows default options! • Change/Add Defaults – Modify themename_get_option_defaults() – Bonus: WPORG will show the updated defaults • Child Theme change/add defaults – add_filter( 'themename_option_defaults', 'childtheme_function' ); WordCamp Columbus 2013, 03 August 2013 Custom Layouts Without Using Page TemplatesPage 23 of 24
  • 24. Links and Resources • Codex References – http://codex.wordpress.org/Function_Reference/wp_parse_args – http://codex.wordpress.org/Function_Reference/get_option – http://codex.wordpress.org/Function_Reference/apply_filters – http://codex.wordpress.org/Function_Reference/add_filter WordCamp Columbus 2013, 03 August 2013 Custom Layouts Without Using Page TemplatesPage 24 of 24