SlideShare ist ein Scribd-Unternehmen logo
1 von 74
Downloaden Sie, um offline zu lesen
Streamlining Your Template
Structures When Building Themes
Cameron Jones
Streamlining Your Template Structures
When Building Themes
I’ve been working with WordPress and I’ve encountered a
number of themes that are built in a really inefficient way.
I’m going to present an approach to building themes that I
believe is more efficient and easier to maintain than most of
what I’ve seen.
Streamlining Your Template Structures
When Building Themes
It will help if you:
● Know a little bit of PHP
● Have made some changes to a theme or child theme
● Understand different content types in WordPress
(posts, pages, categories etc)
WordPress Template Hierarchy
WordPress Template Hierarchy
Confusing, huh?
WordPress Template Hierarchy
Isn’t this a bit like the good ole days of having to create a
new HTML file for each new page???
That seems a little inefficient...
WordPress Template Hierarchy
To help mitigate this, WordPress has some reusable
template functions such as:
● get_header
● get_footer
● get_sidebar
Without these, you’d be adding the header, footer and
sidebar to every single page
WordPress Template Hierarchy
So a website will usually look something like this:
WordPress Template Hierarchy
But this seems a bit backwards to me.
We’re using the rather complicated template hierarchy...
WordPress Template Hierarchy
… just to change this bit
WordPress Template Hierarchy
One of the fundamental rules of programming is to write
DRY code.
Don’t Repeat Yourself
Needing to call the functions like get_header() on lots of
different templates isn’t really DRY code is it?
WordPress Template Hierarchy
Wouldn’t it make more sense if it looked more like this?
WordPress Template Hierarchy
But how are we going to get the template hierarchy to work
with our new approach?
Global Theme Render Function
Global Theme Render Function
Remember how everything in the template hierarchy flowed
back to index.php?
We’re going to only use index.php from the template
hierarchy and control all the logic inside where the dynamic
content goes
Global Theme Render Function
So it’ll look something like this
Global Theme Render Function
How do we achieve this with code?
functions.php
index.php
Global Theme Render Function
Now we don’t have to worry about any new page type not
having a header, footer or sidebar
Global Theme Render Function
But if we’re adding ALL of our conditional logic to our
render function, this one function will get really
complicated...
Global Theme Render Function
Let’s apply the template hierarchy logic to our render
function, but on a smaller level.
Global Theme Render Function
It’ll look something like this
Global Theme Render Function
Now instead of having to build out a whole page template
for each different type of page, we only need to change the
small part of the page that will be different.
But how?
Introducing get_template_part()
Introducing get_template_part()
The get_template_part function allows us to pull in
specific template partials from the theme.
It will include the required template part, and can have
modifiers to change which template to get.
Searches the child theme first allowing for theme specific
overrides
get_template_part()
get_template_part()
get_template_part()
get_template_part()
Introducing get_template_part()
Now how do we use this with our render function?
Template Part Router
Template Part Router
To help manage which template parts to pull in, we’re going
to use a router.
This will effectively replicate the logic of the template
hierarchy.
The Loop
Template Part Router
Types of content we need to cater for
● Loop has posts
○ Single post or page
○ Archive (category, tag, author, post type)
○ Blog (behaves like an archive but actually isn’t)
○ Search (also kinda like an archive)
● Loop doesn’t have posts
○ 404 page
○ Empty archive
○ Empty blog
○ Empty search results
Pro tip: is_listing()
Template Part Router
Types of content we need to cater for
● Loop has posts
○ Single post or page
○ Listing (archive, blog, search)
● Loop doesn’t have posts
○ 404 page
○ Empty listing
functions.php - Theme render function
content.php - Template part router
Template Part Router
Our folder structure:
● template-parts
○ single.php
○ single-post.php
○ listing.php
○ listing-post.php
○ 404.php
○ no-posts.php
● index.php
● functions.php
● style.css
Template Part Router
Now we have a working template part router that can scale
as we introduce new post types and taxonomies
Template Part Router
But the template hierarchy is much more complex.
What if you want to customise the template for a certain
page?
If you have a page at example.com/mypage, you can
create a custom template at page-mypage.php. You can’t
really do this now.
What we have is better because we don’t need to build a
whole template just for a small change, but what can we do
instead?
Template Part Router
1. Make our template part router more complicated
not really ideal
2. Put conditional logic in our template parts
really good for small changes
3. Use hooks
excellent idea!
Introducing The Hook System
Introducing The Hook System
WordPress has a great hook system that allows you to
jump in and change things at different places on your site.
Introducing The Hook System
There are two types of hooks: actions and filters
Actions are like a chopping board. You can place whatever
you like on it.
Filters are like a sieve. You’re given a value, it passes
through the filter and comes out the other side changed.
Introducing The Hook System
You’re probably already using hooks, even if you don’t
realise it.
The wp_enqueue_scripts action is used for the theme
and plugins to add CSS and JavaScript to the site.
The the_content filter is used to update the post content.
WordPress core uses this to autocorrect “wordpress” to
“WordPress”.
add_action
add_filter
Introducing The Hook System
Hooks are often better than using templates because it
guarantees more consistency between different content
types and as plugins and themes are updated.
Introducing The Hook System
We can use hooks to make small amendments based on
the conditions that the template hierarchy is based off
What are some examples?
Introducing The Hook System
Useful actions:
● loop_start - runs at the start of the loop
● loop_end - runs at the end of the loop
● loop_no_results - runs when the loop is empty
● template_redirect - runs just before the page
starts rendering
● wp_head - runs in the <head> tag
● wp_footer - runs right at the bottom of the page
Add pagination to listing pages
Add header image as a hero
Add search form to search results
Introducing The Hook System
But what about archives or searches with no results?
loop_start and loop_end won’t run if the loop is empty.
Add pagination to listing pages even
when empty
Introducing The Hook System
But this could get really complicated and repetitive if you
have lots of hooks onto loop_no_results.
Remember, Don’t Repeat Yourself.
Introducing The Hook System
We can add our own hooks, or call existing hooks at new
times
Run loop_start and loop_end on empty loops
A new custom action
Introducing The Hook System
Useful filters:
● the_content - Change the post or page contents
● the_title - Change the post or page title
● body_class - Add or remove CSS classes on the
body tag
● register_post_type_args - Useful for changing
custom post types that plugins introduce
Post author badge in the comments
Post author badge in the comments
Change the post title on international talk
like a pirate day (Sep 19)
Resources
Resources
Some examples of this methodology in the wild include:
Resources
● Simple example theme:
https://github.com/cameronjonesweb/streamlining-tem
plating
● Slides: will be posted in the meetup group after tonight
In Summary
● Don’t Repeat Yourself
● There is no right or wrong way to build themes
● Use hooks where you can
Cameron Jones
cameronjonesweb.com.au
@cameronjonesweb
Digital Makers
We are a full-service agency, busy designing and
building beautiful digital products, platforms, and
experiments.
digitalmakers.io
Thank You!

Weitere ähnliche Inhalte

Was ist angesagt?

Word press templates
Word press templatesWord press templates
Word press templates
Dan Phiffer
 
WordPress Child Themes
WordPress Child ThemesWordPress Child Themes
WordPress Child Themes
rfair404
 
WordPress Developers Israel Meetup #1
WordPress Developers Israel Meetup #1WordPress Developers Israel Meetup #1
WordPress Developers Israel Meetup #1
Yoav Farhi
 
How does get template part works in twenty ten theme
How does get template part works in twenty ten themeHow does get template part works in twenty ten theme
How does get template part works in twenty ten theme
mohd rozani abd ghani
 

Was ist angesagt? (20)

Creating Customizable Widgets for Unpredictable Needs
Creating Customizable Widgets for Unpredictable NeedsCreating Customizable Widgets for Unpredictable Needs
Creating Customizable Widgets for Unpredictable Needs
 
Word press templates
Word press templatesWord press templates
Word press templates
 
Introduction to WordPress Theme Development
Introduction to WordPress Theme DevelopmentIntroduction to WordPress Theme Development
Introduction to WordPress Theme Development
 
WordPress plugins
WordPress pluginsWordPress plugins
WordPress plugins
 
Building the basics (WordPress Ottawa 2014)
Building the basics (WordPress Ottawa 2014)Building the basics (WordPress Ottawa 2014)
Building the basics (WordPress Ottawa 2014)
 
The Way to Theme Enlightenment
The Way to Theme EnlightenmentThe Way to Theme Enlightenment
The Way to Theme Enlightenment
 
Shortcodes vs Widgets: Which one and how?
Shortcodes vs Widgets: Which one and how?Shortcodes vs Widgets: Which one and how?
Shortcodes vs Widgets: Which one and how?
 
Cain & Obenland — Episode 4
Cain & Obenland — Episode 4Cain & Obenland — Episode 4
Cain & Obenland — Episode 4
 
Responsive Theme Workshop - WordCamp Columbus 2015
Responsive Theme Workshop - WordCamp Columbus 2015Responsive Theme Workshop - WordCamp Columbus 2015
Responsive Theme Workshop - WordCamp Columbus 2015
 
Web 101 intro to html
Web 101  intro to htmlWeb 101  intro to html
Web 101 intro to html
 
The Genesis Framework: Hooks & Filters for Theme Development
The Genesis Framework: Hooks & Filters for Theme DevelopmentThe Genesis Framework: Hooks & Filters for Theme Development
The Genesis Framework: Hooks & Filters for Theme Development
 
The Future Of WordPress Presentation
The Future Of WordPress PresentationThe Future Of WordPress Presentation
The Future Of WordPress Presentation
 
WordPress Theme Development
WordPress Theme DevelopmentWordPress Theme Development
WordPress Theme Development
 
WordPress Child Themes
WordPress Child ThemesWordPress Child Themes
WordPress Child Themes
 
WordPress Developers Israel Meetup #1
WordPress Developers Israel Meetup #1WordPress Developers Israel Meetup #1
WordPress Developers Israel Meetup #1
 
Newspapers with WordPress
Newspapers with WordPressNewspapers with WordPress
Newspapers with WordPress
 
How does get template part works in twenty ten theme
How does get template part works in twenty ten themeHow does get template part works in twenty ten theme
How does get template part works in twenty ten theme
 
What is (not) WordPress
What is (not) WordPressWhat is (not) WordPress
What is (not) WordPress
 
Theming 101
Theming 101Theming 101
Theming 101
 
Child Themes in WordPress
Child Themes in WordPressChild Themes in WordPress
Child Themes in WordPress
 

Ähnlich wie Streamlining Your Template Structures When Building Themes

Slavin-Dodson Piece, With Code.
Slavin-Dodson Piece, With Code.Slavin-Dodson Piece, With Code.
Slavin-Dodson Piece, With Code.
ALATechSource
 
The WordPress University 2012
The WordPress University 2012The WordPress University 2012
The WordPress University 2012
Stephanie Leary
 
Theme development essentials columbus oh word camp 2012
Theme development essentials   columbus oh word camp 2012Theme development essentials   columbus oh word camp 2012
Theme development essentials columbus oh word camp 2012
Joe Querin
 

Ähnlich wie Streamlining Your Template Structures When Building Themes (20)

Advanced Intro to Wordpress
Advanced Intro to WordpressAdvanced Intro to Wordpress
Advanced Intro to Wordpress
 
Child Themes (WordCamp Dublin 2017) with notes
Child Themes (WordCamp Dublin 2017) with notesChild Themes (WordCamp Dublin 2017) with notes
Child Themes (WordCamp Dublin 2017) with notes
 
Slavin-Dodson Piece, With Code.
Slavin-Dodson Piece, With Code.Slavin-Dodson Piece, With Code.
Slavin-Dodson Piece, With Code.
 
WordCamp Raleigh 2018 - Beginner's Guide to Wordpress
WordCamp Raleigh 2018 - Beginner's Guide to WordpressWordCamp Raleigh 2018 - Beginner's Guide to Wordpress
WordCamp Raleigh 2018 - Beginner's Guide to Wordpress
 
Wordpress(css,php,js,ajax)
Wordpress(css,php,js,ajax)Wordpress(css,php,js,ajax)
Wordpress(css,php,js,ajax)
 
Every Artist needs a Great Website: Getting Started with WordPress
Every Artist needs a Great Website: Getting Started with WordPressEvery Artist needs a Great Website: Getting Started with WordPress
Every Artist needs a Great Website: Getting Started with WordPress
 
Plugin development demystified 2017
Plugin development demystified 2017Plugin development demystified 2017
Plugin development demystified 2017
 
Boston WordPress Meetup June 2017 Utility Plugins
Boston WordPress Meetup June 2017 Utility PluginsBoston WordPress Meetup June 2017 Utility Plugins
Boston WordPress Meetup June 2017 Utility Plugins
 
The WordPress University 2012
The WordPress University 2012The WordPress University 2012
The WordPress University 2012
 
Don't Fear the Custom Theme: How to build a custom WordPress theme with only ...
Don't Fear the Custom Theme: How to build a custom WordPress theme with only ...Don't Fear the Custom Theme: How to build a custom WordPress theme with only ...
Don't Fear the Custom Theme: How to build a custom WordPress theme with only ...
 
Newbies guide to customizing word press themes 25
Newbies guide to customizing word press themes 25Newbies guide to customizing word press themes 25
Newbies guide to customizing word press themes 25
 
How to create your own WordPress plugin
How to create your own WordPress pluginHow to create your own WordPress plugin
How to create your own WordPress plugin
 
Word camp Raleigh 2017 - Wordpress for Beginners
Word camp Raleigh 2017 - Wordpress for BeginnersWord camp Raleigh 2017 - Wordpress for Beginners
Word camp Raleigh 2017 - Wordpress for Beginners
 
The Way to Theme Enlightenment 2017
The Way to Theme Enlightenment 2017The Way to Theme Enlightenment 2017
The Way to Theme Enlightenment 2017
 
Modern Theming & The Future of WordPress- Working with Full Site Editing and ...
Modern Theming & The Future of WordPress- Working with Full Site Editing and ...Modern Theming & The Future of WordPress- Working with Full Site Editing and ...
Modern Theming & The Future of WordPress- Working with Full Site Editing and ...
 
How to annotate_with_wordpress
How to annotate_with_wordpressHow to annotate_with_wordpress
How to annotate_with_wordpress
 
Theme development essentials columbus oh word camp 2012
Theme development essentials   columbus oh word camp 2012Theme development essentials   columbus oh word camp 2012
Theme development essentials columbus oh word camp 2012
 
Test ss 1
Test ss 1Test ss 1
Test ss 1
 
WordCamp Raleigh 2019 - Beginner's Guide to Wordpress
WordCamp Raleigh 2019 - Beginner's Guide to WordpressWordCamp Raleigh 2019 - Beginner's Guide to Wordpress
WordCamp Raleigh 2019 - Beginner's Guide to Wordpress
 
Developing Custom WordPress Themes for Clients
Developing Custom WordPress Themes for ClientsDeveloping Custom WordPress Themes for Clients
Developing Custom WordPress Themes for Clients
 

Kürzlich hochgeladen

Architecting Cloud Native Applications
Architecting Cloud Native ApplicationsArchitecting Cloud Native Applications
Architecting Cloud Native Applications
WSO2
 
+971581248768>> SAFE AND ORIGINAL ABORTION PILLS FOR SALE IN DUBAI AND ABUDHA...
+971581248768>> SAFE AND ORIGINAL ABORTION PILLS FOR SALE IN DUBAI AND ABUDHA...+971581248768>> SAFE AND ORIGINAL ABORTION PILLS FOR SALE IN DUBAI AND ABUDHA...
+971581248768>> SAFE AND ORIGINAL ABORTION PILLS FOR SALE IN DUBAI AND ABUDHA...
?#DUbAI#??##{{(☎️+971_581248768%)**%*]'#abortion pills for sale in dubai@
 

Kürzlich hochgeladen (20)

AXA XL - Insurer Innovation Award Americas 2024
AXA XL - Insurer Innovation Award Americas 2024AXA XL - Insurer Innovation Award Americas 2024
AXA XL - Insurer Innovation Award Americas 2024
 
Architecting Cloud Native Applications
Architecting Cloud Native ApplicationsArchitecting Cloud Native Applications
Architecting Cloud Native Applications
 
Manulife - Insurer Transformation Award 2024
Manulife - Insurer Transformation Award 2024Manulife - Insurer Transformation Award 2024
Manulife - Insurer Transformation Award 2024
 
Emergent Methods: Multi-lingual narrative tracking in the news - real-time ex...
Emergent Methods: Multi-lingual narrative tracking in the news - real-time ex...Emergent Methods: Multi-lingual narrative tracking in the news - real-time ex...
Emergent Methods: Multi-lingual narrative tracking in the news - real-time ex...
 
ICT role in 21st century education and its challenges
ICT role in 21st century education and its challengesICT role in 21st century education and its challenges
ICT role in 21st century education and its challenges
 
Apidays New York 2024 - The value of a flexible API Management solution for O...
Apidays New York 2024 - The value of a flexible API Management solution for O...Apidays New York 2024 - The value of a flexible API Management solution for O...
Apidays New York 2024 - The value of a flexible API Management solution for O...
 
Navi Mumbai Call Girls 🥰 8617370543 Service Offer VIP Hot Model
Navi Mumbai Call Girls 🥰 8617370543 Service Offer VIP Hot ModelNavi Mumbai Call Girls 🥰 8617370543 Service Offer VIP Hot Model
Navi Mumbai Call Girls 🥰 8617370543 Service Offer VIP Hot Model
 
Strategies for Unlocking Knowledge Management in Microsoft 365 in the Copilot...
Strategies for Unlocking Knowledge Management in Microsoft 365 in the Copilot...Strategies for Unlocking Knowledge Management in Microsoft 365 in the Copilot...
Strategies for Unlocking Knowledge Management in Microsoft 365 in the Copilot...
 
GenAI Risks & Security Meetup 01052024.pdf
GenAI Risks & Security Meetup 01052024.pdfGenAI Risks & Security Meetup 01052024.pdf
GenAI Risks & Security Meetup 01052024.pdf
 
A Beginners Guide to Building a RAG App Using Open Source Milvus
A Beginners Guide to Building a RAG App Using Open Source MilvusA Beginners Guide to Building a RAG App Using Open Source Milvus
A Beginners Guide to Building a RAG App Using Open Source Milvus
 
Apidays Singapore 2024 - Building Digital Trust in a Digital Economy by Veron...
Apidays Singapore 2024 - Building Digital Trust in a Digital Economy by Veron...Apidays Singapore 2024 - Building Digital Trust in a Digital Economy by Veron...
Apidays Singapore 2024 - Building Digital Trust in a Digital Economy by Veron...
 
Axa Assurance Maroc - Insurer Innovation Award 2024
Axa Assurance Maroc - Insurer Innovation Award 2024Axa Assurance Maroc - Insurer Innovation Award 2024
Axa Assurance Maroc - Insurer Innovation Award 2024
 
+971581248768>> SAFE AND ORIGINAL ABORTION PILLS FOR SALE IN DUBAI AND ABUDHA...
+971581248768>> SAFE AND ORIGINAL ABORTION PILLS FOR SALE IN DUBAI AND ABUDHA...+971581248768>> SAFE AND ORIGINAL ABORTION PILLS FOR SALE IN DUBAI AND ABUDHA...
+971581248768>> SAFE AND ORIGINAL ABORTION PILLS FOR SALE IN DUBAI AND ABUDHA...
 
Automating Google Workspace (GWS) & more with Apps Script
Automating Google Workspace (GWS) & more with Apps ScriptAutomating Google Workspace (GWS) & more with Apps Script
Automating Google Workspace (GWS) & more with Apps Script
 
DBX First Quarter 2024 Investor Presentation
DBX First Quarter 2024 Investor PresentationDBX First Quarter 2024 Investor Presentation
DBX First Quarter 2024 Investor Presentation
 
Corporate and higher education May webinar.pptx
Corporate and higher education May webinar.pptxCorporate and higher education May webinar.pptx
Corporate and higher education May webinar.pptx
 
Strategize a Smooth Tenant-to-tenant Migration and Copilot Takeoff
Strategize a Smooth Tenant-to-tenant Migration and Copilot TakeoffStrategize a Smooth Tenant-to-tenant Migration and Copilot Takeoff
Strategize a Smooth Tenant-to-tenant Migration and Copilot Takeoff
 
Artificial Intelligence Chap.5 : Uncertainty
Artificial Intelligence Chap.5 : UncertaintyArtificial Intelligence Chap.5 : Uncertainty
Artificial Intelligence Chap.5 : Uncertainty
 
Web Form Automation for Bonterra Impact Management (fka Social Solutions Apri...
Web Form Automation for Bonterra Impact Management (fka Social Solutions Apri...Web Form Automation for Bonterra Impact Management (fka Social Solutions Apri...
Web Form Automation for Bonterra Impact Management (fka Social Solutions Apri...
 
Data Cloud, More than a CDP by Matt Robison
Data Cloud, More than a CDP by Matt RobisonData Cloud, More than a CDP by Matt Robison
Data Cloud, More than a CDP by Matt Robison
 

Streamlining Your Template Structures When Building Themes