SlideShare a Scribd company logo
1 of 22
Download to read offline
An MVC approach to
WordPress theme
development
Bio.
● Mircea Tihu
● Timisoara, Romania
● Lead Developer @ Dream Production AG
● mircea@dreamproduction.com
What is MVC?
● Architectural pattern based on 3 components:
○ Model - represents the data
○ View - represents the presentation layer
○ Controller - represents the logic
Why MVC?
● Based on the Separation of concerns
● More structured code
● Reduced code complexity
● Decoupled code
Timber.
Timber separates your theme’s code:
● the PHP files focus only on supplying the data
and logic
● A templating engine is used for the
presentation: Twig
Twig.
● PHP template engine
<!DOCTYPE html>
<html>
<head>
<title>Title</title>
</head>
<body>
<ul id="list">
{% for item in list %}
<li><a href="{{ item.href }}">{{ item.label }}</a></li>
{% endfor %}
</ul>
</body>
</html>
WordPress as MVC.
Model Controller View
index.php
<?php
$ctx = Timber::get_context();
$ctx['foo'] = 'Bar!';
$ctx['post'] = new Timber/Post();
Timber::render('single.twig', $ctx);
?>
index.twig
{% extends "base.twig" %}
{% block content %}
<h1 class="big-title">{{foo}}</h1>
<h2>{{post.title}}</h2>
<img src="{{post.thumbnail.src}}" />
<div class="body">
{{post.content}}
</div>
{% endblock %}
ACF.
● Allows the users to create extra fields in a very
user-friendly manner
● Has a lot of different field types to be
configured
The normal approach.
● The content manager / back-end developer configures the
ACF fields
● The back-end developer queries all the necessary data and
appends it to the context variable
● The front-end developer uses the data from the context
variable in the twig template, creates the markup and styles
it
Our approach.
● We’re trying to remove the back-end developer
from this equation
● Do to this we automatically map all the ACF
fields defined into the twig context variable
Our approach.
● Timber has objects defined for each WordPress entity:
○ Posts, Pages, Custom Post Types - TimberPost
○ Categories, Tags, Custom taxonomies - TimberTerm
○ Users - TimberUser
○ Image - TimberImage
Our approach.
● Before a Timber object is instantiated we go through all the
defined fields and verify if they apply to the current object
● On the Timber object we add a new key called fields, where
all the fields that match the criteria for that object are
appended
● All the fields added as theme options are stored on a special
key called options
The fields.
● Text, Textarea, Wysiwyg, Select, Radio, Checkbox - their actual values are added
● For other field types you get the entire set of data
○ Image, Gallery - mapped as TimberImage
○ Post Object - mapped as TimberPost
○ Taxonomy - mapped as TimberTerm
○ User - mapped as TimberUser
○ Repeater, Flexible Content - mapped as arrays containing actual values or
Timber objects
The fields. Text. Wysiwyg.
array(2) {
["text_field"]=> string(26) "Lorem ipsum
dolor sit amet"
["wysiwyg_field"]=> string(328) "<p>Lorem
ipsum dolor sit amet, consectetur adipiscing
elit. Suspendisse lobortis, tortor ut
elementum imperdiet, turpis sem ultricies
erat, quis mattis nibh mauris nec ante. Nunc
quis viverra dolor.</p>
<p>Mauris aliquam magna a convallis pretium.
Duis ultricies viverra ornare. Donec
suscipit risus lacinia luctus iaculis.</p>"
}
The fields. Image.
array(1) {
["image_field"]=>
object(TimberImage)#1008 (47) {
["object_type"]=> string(5) "image"
["post_title"]=> string(4) "test"
["id"]=> int(222)
["sizes"]=> array(3) {
...
}
...
// Additional methods to get the src and
// the srcset
}
}
The fields. Post Object.
Fields defined on the New post post
array(2) {
["text_field"]=> string(18) "Text field
content"
["post_field"]=>
object(TimberPost)#968 (86) {
["ID"]=> int(119)
["post_content"]=> string(0) ""
["post_title"]=> string(8) "New post"
["post_type"]=> string(4) "post"
["fields"]=> array(2) {
["text_field"]=> string(26) "Lorem
ipsum dolor sit amet"
["post_field"]=> NULL
}
}
}
The fields. Repeater.
array(1) {
["repeater_field"]=> array(3) {
[0]=> array(2) {
["text_field_1"]=> string(26) "Lorem ipsum
dolor sit amet"
["text_field_2"]=> string(27) "consectetur
adipiscing elit"
}
[1]=> array(2) {
["text_field_1"]=> string(25) "Quisque
posuere dui velit"
["text_field_2"]=> string(29) "quis egestas
libero malesuada"
}
[2]=> array(2) {
["text_field_1"]=> string(19) "Mauris
auctor augue"
["text_field_2"]=> string(22) "vitae diam
consectetur"
}
}
}
Challenges. Field caching.
While processing the fields for a certain object we need to go
through all the field groups defined in ACF.
Solution: Store the ids of the fields for each WordPress entity.
Challenges. Infinite Loops.
If Post A has a reference to Post B and Post B has a reference
back to Post A, while retrieving the fields we would find
ourselves in an infinite loop.
Solution: If an object is already appended to the data variable,
we append an empty array instead of the actual object.
Summary.
● Timber
● ACF
● For basic website templates the work of the back-end
developers can be almost completely replaced by the work
of content managers and front-end developers
Questions?
Thank you!

More Related Content

Similar to An MVC Approach to WordPress Theme Development

Mongo db勉強会20110730
Mongo db勉強会20110730Mongo db勉強会20110730
Mongo db勉強会20110730Akihiro Okuno
 
EA User Group London 2018 - Extending EA with custom scripts to cater for spe...
EA User Group London 2018 - Extending EA with custom scripts to cater for spe...EA User Group London 2018 - Extending EA with custom scripts to cater for spe...
EA User Group London 2018 - Extending EA with custom scripts to cater for spe...Guillaume Finance
 
The Ring programming language version 1.5.1 book - Part 43 of 180
The Ring programming language version 1.5.1 book - Part 43 of 180The Ring programming language version 1.5.1 book - Part 43 of 180
The Ring programming language version 1.5.1 book - Part 43 of 180Mahmoud Samir Fayed
 
An Event Apart Seattle - New CSS Layout Meets the Real World
An Event Apart Seattle - New CSS Layout Meets the Real WorldAn Event Apart Seattle - New CSS Layout Meets the Real World
An Event Apart Seattle - New CSS Layout Meets the Real WorldRachel Andrew
 
Learning .NET Attributes
Learning .NET AttributesLearning .NET Attributes
Learning .NET AttributesPooja Gaikwad
 
Learn dot net attributes
Learn dot net attributesLearn dot net attributes
Learn dot net attributessonia merchant
 
slideset 7 structure and union (1).pdf
slideset 7 structure and union (1).pdfslideset 7 structure and union (1).pdf
slideset 7 structure and union (1).pdfHimanshuKansal22
 
Introducing DataWave
Introducing DataWaveIntroducing DataWave
Introducing DataWaveData Works MD
 
Practical Google App Engine Applications In Py
Practical Google App Engine Applications In PyPractical Google App Engine Applications In Py
Practical Google App Engine Applications In PyEric ShangKuan
 
New CSS Layout Meets the Real World
New CSS Layout Meets the Real WorldNew CSS Layout Meets the Real World
New CSS Layout Meets the Real WorldRachel Andrew
 
JavaOne 2013: Memory Efficient Java
JavaOne 2013: Memory Efficient JavaJavaOne 2013: Memory Efficient Java
JavaOne 2013: Memory Efficient JavaChris Bailey
 
Graph db as metastore
Graph db as metastoreGraph db as metastore
Graph db as metastoreHaris Khan
 
Data Migration with Spark to Hive
Data Migration with Spark to HiveData Migration with Spark to Hive
Data Migration with Spark to HiveDatabricks
 
Synapse india reviews on drupal 7 entities (stanford)
Synapse india reviews on drupal 7 entities (stanford)Synapse india reviews on drupal 7 entities (stanford)
Synapse india reviews on drupal 7 entities (stanford)Tarunsingh198
 
Creating and Theming Custom Content Types
Creating and Theming Custom Content TypesCreating and Theming Custom Content Types
Creating and Theming Custom Content Typesheatherrumd
 
A Rusty introduction to Apache Arrow and how it applies to a time series dat...
A Rusty introduction to Apache Arrow and how it applies to a  time series dat...A Rusty introduction to Apache Arrow and how it applies to a  time series dat...
A Rusty introduction to Apache Arrow and how it applies to a time series dat...Andrew Lamb
 
Arrays, Structures And Enums
Arrays, Structures And EnumsArrays, Structures And Enums
Arrays, Structures And EnumsBhushan Mulmule
 
Mongo and Harmony
Mongo and HarmonyMongo and Harmony
Mongo and HarmonySteve Smith
 

Similar to An MVC Approach to WordPress Theme Development (20)

Mongo db勉強会20110730
Mongo db勉強会20110730Mongo db勉強会20110730
Mongo db勉強会20110730
 
EA User Group London 2018 - Extending EA with custom scripts to cater for spe...
EA User Group London 2018 - Extending EA with custom scripts to cater for spe...EA User Group London 2018 - Extending EA with custom scripts to cater for spe...
EA User Group London 2018 - Extending EA with custom scripts to cater for spe...
 
The Ring programming language version 1.5.1 book - Part 43 of 180
The Ring programming language version 1.5.1 book - Part 43 of 180The Ring programming language version 1.5.1 book - Part 43 of 180
The Ring programming language version 1.5.1 book - Part 43 of 180
 
An Event Apart Seattle - New CSS Layout Meets the Real World
An Event Apart Seattle - New CSS Layout Meets the Real WorldAn Event Apart Seattle - New CSS Layout Meets the Real World
An Event Apart Seattle - New CSS Layout Meets the Real World
 
Intake 37 ef2
Intake 37 ef2Intake 37 ef2
Intake 37 ef2
 
Learning .NET Attributes
Learning .NET AttributesLearning .NET Attributes
Learning .NET Attributes
 
Learn dot net attributes
Learn dot net attributesLearn dot net attributes
Learn dot net attributes
 
slideset 7 structure and union (1).pdf
slideset 7 structure and union (1).pdfslideset 7 structure and union (1).pdf
slideset 7 structure and union (1).pdf
 
Introducing DataWave
Introducing DataWaveIntroducing DataWave
Introducing DataWave
 
Practical Google App Engine Applications In Py
Practical Google App Engine Applications In PyPractical Google App Engine Applications In Py
Practical Google App Engine Applications In Py
 
New CSS Layout Meets the Real World
New CSS Layout Meets the Real WorldNew CSS Layout Meets the Real World
New CSS Layout Meets the Real World
 
JavaOne 2013: Memory Efficient Java
JavaOne 2013: Memory Efficient JavaJavaOne 2013: Memory Efficient Java
JavaOne 2013: Memory Efficient Java
 
Graph db as metastore
Graph db as metastoreGraph db as metastore
Graph db as metastore
 
Data Migration with Spark to Hive
Data Migration with Spark to HiveData Migration with Spark to Hive
Data Migration with Spark to Hive
 
Synapse india reviews on drupal 7 entities (stanford)
Synapse india reviews on drupal 7 entities (stanford)Synapse india reviews on drupal 7 entities (stanford)
Synapse india reviews on drupal 7 entities (stanford)
 
Data formats
Data formatsData formats
Data formats
 
Creating and Theming Custom Content Types
Creating and Theming Custom Content TypesCreating and Theming Custom Content Types
Creating and Theming Custom Content Types
 
A Rusty introduction to Apache Arrow and how it applies to a time series dat...
A Rusty introduction to Apache Arrow and how it applies to a  time series dat...A Rusty introduction to Apache Arrow and how it applies to a  time series dat...
A Rusty introduction to Apache Arrow and how it applies to a time series dat...
 
Arrays, Structures And Enums
Arrays, Structures And EnumsArrays, Structures And Enums
Arrays, Structures And Enums
 
Mongo and Harmony
Mongo and HarmonyMongo and Harmony
Mongo and Harmony
 

Recently uploaded

04-2024-HHUG-Sales-and-Marketing-Alignment.pptx
04-2024-HHUG-Sales-and-Marketing-Alignment.pptx04-2024-HHUG-Sales-and-Marketing-Alignment.pptx
04-2024-HHUG-Sales-and-Marketing-Alignment.pptxHampshireHUG
 
Swan(sea) Song – personal research during my six years at Swansea ... and bey...
Swan(sea) Song – personal research during my six years at Swansea ... and bey...Swan(sea) Song – personal research during my six years at Swansea ... and bey...
Swan(sea) Song – personal research during my six years at Swansea ... and bey...Alan Dix
 
Kotlin Multiplatform & Compose Multiplatform - Starter kit for pragmatics
Kotlin Multiplatform & Compose Multiplatform - Starter kit for pragmaticsKotlin Multiplatform & Compose Multiplatform - Starter kit for pragmatics
Kotlin Multiplatform & Compose Multiplatform - Starter kit for pragmaticscarlostorres15106
 
Automating Business Process via MuleSoft Composer | Bangalore MuleSoft Meetup...
Automating Business Process via MuleSoft Composer | Bangalore MuleSoft Meetup...Automating Business Process via MuleSoft Composer | Bangalore MuleSoft Meetup...
Automating Business Process via MuleSoft Composer | Bangalore MuleSoft Meetup...shyamraj55
 
Salesforce Community Group Quito, Salesforce 101
Salesforce Community Group Quito, Salesforce 101Salesforce Community Group Quito, Salesforce 101
Salesforce Community Group Quito, Salesforce 101Paola De la Torre
 
Presentation on how to chat with PDF using ChatGPT code interpreter
Presentation on how to chat with PDF using ChatGPT code interpreterPresentation on how to chat with PDF using ChatGPT code interpreter
Presentation on how to chat with PDF using ChatGPT code interpreternaman860154
 
From Event to Action: Accelerate Your Decision Making with Real-Time Automation
From Event to Action: Accelerate Your Decision Making with Real-Time AutomationFrom Event to Action: Accelerate Your Decision Making with Real-Time Automation
From Event to Action: Accelerate Your Decision Making with Real-Time AutomationSafe Software
 
Injustice - Developers Among Us (SciFiDevCon 2024)
Injustice - Developers Among Us (SciFiDevCon 2024)Injustice - Developers Among Us (SciFiDevCon 2024)
Injustice - Developers Among Us (SciFiDevCon 2024)Allon Mureinik
 
Benefits Of Flutter Compared To Other Frameworks
Benefits Of Flutter Compared To Other FrameworksBenefits Of Flutter Compared To Other Frameworks
Benefits Of Flutter Compared To Other FrameworksSoftradix Technologies
 
How to Troubleshoot Apps for the Modern Connected Worker
How to Troubleshoot Apps for the Modern Connected WorkerHow to Troubleshoot Apps for the Modern Connected Worker
How to Troubleshoot Apps for the Modern Connected WorkerThousandEyes
 
SQL Database Design For Developers at php[tek] 2024
SQL Database Design For Developers at php[tek] 2024SQL Database Design For Developers at php[tek] 2024
SQL Database Design For Developers at php[tek] 2024Scott Keck-Warren
 
08448380779 Call Girls In Friends Colony Women Seeking Men
08448380779 Call Girls In Friends Colony Women Seeking Men08448380779 Call Girls In Friends Colony Women Seeking Men
08448380779 Call Girls In Friends Colony Women Seeking MenDelhi Call girls
 
Scaling API-first – The story of a global engineering organization
Scaling API-first – The story of a global engineering organizationScaling API-first – The story of a global engineering organization
Scaling API-first – The story of a global engineering organizationRadu Cotescu
 
How to Remove Document Management Hurdles with X-Docs?
How to Remove Document Management Hurdles with X-Docs?How to Remove Document Management Hurdles with X-Docs?
How to Remove Document Management Hurdles with X-Docs?XfilesPro
 
Transforming Data Streams with Kafka Connect: An Introduction to Single Messa...
Transforming Data Streams with Kafka Connect: An Introduction to Single Messa...Transforming Data Streams with Kafka Connect: An Introduction to Single Messa...
Transforming Data Streams with Kafka Connect: An Introduction to Single Messa...HostedbyConfluent
 
Azure Monitor & Application Insight to monitor Infrastructure & Application
Azure Monitor & Application Insight to monitor Infrastructure & ApplicationAzure Monitor & Application Insight to monitor Infrastructure & Application
Azure Monitor & Application Insight to monitor Infrastructure & ApplicationAndikSusilo4
 
Enhancing Worker Digital Experience: A Hands-on Workshop for Partners
Enhancing Worker Digital Experience: A Hands-on Workshop for PartnersEnhancing Worker Digital Experience: A Hands-on Workshop for Partners
Enhancing Worker Digital Experience: A Hands-on Workshop for PartnersThousandEyes
 
The 7 Things I Know About Cyber Security After 25 Years | April 2024
The 7 Things I Know About Cyber Security After 25 Years | April 2024The 7 Things I Know About Cyber Security After 25 Years | April 2024
The 7 Things I Know About Cyber Security After 25 Years | April 2024Rafal Los
 
Human Factors of XR: Using Human Factors to Design XR Systems
Human Factors of XR: Using Human Factors to Design XR SystemsHuman Factors of XR: Using Human Factors to Design XR Systems
Human Factors of XR: Using Human Factors to Design XR SystemsMark Billinghurst
 
Pigging Solutions in Pet Food Manufacturing
Pigging Solutions in Pet Food ManufacturingPigging Solutions in Pet Food Manufacturing
Pigging Solutions in Pet Food ManufacturingPigging Solutions
 

Recently uploaded (20)

04-2024-HHUG-Sales-and-Marketing-Alignment.pptx
04-2024-HHUG-Sales-and-Marketing-Alignment.pptx04-2024-HHUG-Sales-and-Marketing-Alignment.pptx
04-2024-HHUG-Sales-and-Marketing-Alignment.pptx
 
Swan(sea) Song – personal research during my six years at Swansea ... and bey...
Swan(sea) Song – personal research during my six years at Swansea ... and bey...Swan(sea) Song – personal research during my six years at Swansea ... and bey...
Swan(sea) Song – personal research during my six years at Swansea ... and bey...
 
Kotlin Multiplatform & Compose Multiplatform - Starter kit for pragmatics
Kotlin Multiplatform & Compose Multiplatform - Starter kit for pragmaticsKotlin Multiplatform & Compose Multiplatform - Starter kit for pragmatics
Kotlin Multiplatform & Compose Multiplatform - Starter kit for pragmatics
 
Automating Business Process via MuleSoft Composer | Bangalore MuleSoft Meetup...
Automating Business Process via MuleSoft Composer | Bangalore MuleSoft Meetup...Automating Business Process via MuleSoft Composer | Bangalore MuleSoft Meetup...
Automating Business Process via MuleSoft Composer | Bangalore MuleSoft Meetup...
 
Salesforce Community Group Quito, Salesforce 101
Salesforce Community Group Quito, Salesforce 101Salesforce Community Group Quito, Salesforce 101
Salesforce Community Group Quito, Salesforce 101
 
Presentation on how to chat with PDF using ChatGPT code interpreter
Presentation on how to chat with PDF using ChatGPT code interpreterPresentation on how to chat with PDF using ChatGPT code interpreter
Presentation on how to chat with PDF using ChatGPT code interpreter
 
From Event to Action: Accelerate Your Decision Making with Real-Time Automation
From Event to Action: Accelerate Your Decision Making with Real-Time AutomationFrom Event to Action: Accelerate Your Decision Making with Real-Time Automation
From Event to Action: Accelerate Your Decision Making with Real-Time Automation
 
Injustice - Developers Among Us (SciFiDevCon 2024)
Injustice - Developers Among Us (SciFiDevCon 2024)Injustice - Developers Among Us (SciFiDevCon 2024)
Injustice - Developers Among Us (SciFiDevCon 2024)
 
Benefits Of Flutter Compared To Other Frameworks
Benefits Of Flutter Compared To Other FrameworksBenefits Of Flutter Compared To Other Frameworks
Benefits Of Flutter Compared To Other Frameworks
 
How to Troubleshoot Apps for the Modern Connected Worker
How to Troubleshoot Apps for the Modern Connected WorkerHow to Troubleshoot Apps for the Modern Connected Worker
How to Troubleshoot Apps for the Modern Connected Worker
 
SQL Database Design For Developers at php[tek] 2024
SQL Database Design For Developers at php[tek] 2024SQL Database Design For Developers at php[tek] 2024
SQL Database Design For Developers at php[tek] 2024
 
08448380779 Call Girls In Friends Colony Women Seeking Men
08448380779 Call Girls In Friends Colony Women Seeking Men08448380779 Call Girls In Friends Colony Women Seeking Men
08448380779 Call Girls In Friends Colony Women Seeking Men
 
Scaling API-first – The story of a global engineering organization
Scaling API-first – The story of a global engineering organizationScaling API-first – The story of a global engineering organization
Scaling API-first – The story of a global engineering organization
 
How to Remove Document Management Hurdles with X-Docs?
How to Remove Document Management Hurdles with X-Docs?How to Remove Document Management Hurdles with X-Docs?
How to Remove Document Management Hurdles with X-Docs?
 
Transforming Data Streams with Kafka Connect: An Introduction to Single Messa...
Transforming Data Streams with Kafka Connect: An Introduction to Single Messa...Transforming Data Streams with Kafka Connect: An Introduction to Single Messa...
Transforming Data Streams with Kafka Connect: An Introduction to Single Messa...
 
Azure Monitor & Application Insight to monitor Infrastructure & Application
Azure Monitor & Application Insight to monitor Infrastructure & ApplicationAzure Monitor & Application Insight to monitor Infrastructure & Application
Azure Monitor & Application Insight to monitor Infrastructure & Application
 
Enhancing Worker Digital Experience: A Hands-on Workshop for Partners
Enhancing Worker Digital Experience: A Hands-on Workshop for PartnersEnhancing Worker Digital Experience: A Hands-on Workshop for Partners
Enhancing Worker Digital Experience: A Hands-on Workshop for Partners
 
The 7 Things I Know About Cyber Security After 25 Years | April 2024
The 7 Things I Know About Cyber Security After 25 Years | April 2024The 7 Things I Know About Cyber Security After 25 Years | April 2024
The 7 Things I Know About Cyber Security After 25 Years | April 2024
 
Human Factors of XR: Using Human Factors to Design XR Systems
Human Factors of XR: Using Human Factors to Design XR SystemsHuman Factors of XR: Using Human Factors to Design XR Systems
Human Factors of XR: Using Human Factors to Design XR Systems
 
Pigging Solutions in Pet Food Manufacturing
Pigging Solutions in Pet Food ManufacturingPigging Solutions in Pet Food Manufacturing
Pigging Solutions in Pet Food Manufacturing
 

An MVC Approach to WordPress Theme Development

  • 1. An MVC approach to WordPress theme development
  • 2. Bio. ● Mircea Tihu ● Timisoara, Romania ● Lead Developer @ Dream Production AG ● mircea@dreamproduction.com
  • 3. What is MVC? ● Architectural pattern based on 3 components: ○ Model - represents the data ○ View - represents the presentation layer ○ Controller - represents the logic
  • 4. Why MVC? ● Based on the Separation of concerns ● More structured code ● Reduced code complexity ● Decoupled code
  • 5. Timber. Timber separates your theme’s code: ● the PHP files focus only on supplying the data and logic ● A templating engine is used for the presentation: Twig
  • 6. Twig. ● PHP template engine <!DOCTYPE html> <html> <head> <title>Title</title> </head> <body> <ul id="list"> {% for item in list %} <li><a href="{{ item.href }}">{{ item.label }}</a></li> {% endfor %} </ul> </body> </html>
  • 7. WordPress as MVC. Model Controller View index.php <?php $ctx = Timber::get_context(); $ctx['foo'] = 'Bar!'; $ctx['post'] = new Timber/Post(); Timber::render('single.twig', $ctx); ?> index.twig {% extends "base.twig" %} {% block content %} <h1 class="big-title">{{foo}}</h1> <h2>{{post.title}}</h2> <img src="{{post.thumbnail.src}}" /> <div class="body"> {{post.content}} </div> {% endblock %}
  • 8. ACF. ● Allows the users to create extra fields in a very user-friendly manner ● Has a lot of different field types to be configured
  • 9. The normal approach. ● The content manager / back-end developer configures the ACF fields ● The back-end developer queries all the necessary data and appends it to the context variable ● The front-end developer uses the data from the context variable in the twig template, creates the markup and styles it
  • 10. Our approach. ● We’re trying to remove the back-end developer from this equation ● Do to this we automatically map all the ACF fields defined into the twig context variable
  • 11. Our approach. ● Timber has objects defined for each WordPress entity: ○ Posts, Pages, Custom Post Types - TimberPost ○ Categories, Tags, Custom taxonomies - TimberTerm ○ Users - TimberUser ○ Image - TimberImage
  • 12. Our approach. ● Before a Timber object is instantiated we go through all the defined fields and verify if they apply to the current object ● On the Timber object we add a new key called fields, where all the fields that match the criteria for that object are appended ● All the fields added as theme options are stored on a special key called options
  • 13. The fields. ● Text, Textarea, Wysiwyg, Select, Radio, Checkbox - their actual values are added ● For other field types you get the entire set of data ○ Image, Gallery - mapped as TimberImage ○ Post Object - mapped as TimberPost ○ Taxonomy - mapped as TimberTerm ○ User - mapped as TimberUser ○ Repeater, Flexible Content - mapped as arrays containing actual values or Timber objects
  • 14. The fields. Text. Wysiwyg. array(2) { ["text_field"]=> string(26) "Lorem ipsum dolor sit amet" ["wysiwyg_field"]=> string(328) "<p>Lorem ipsum dolor sit amet, consectetur adipiscing elit. Suspendisse lobortis, tortor ut elementum imperdiet, turpis sem ultricies erat, quis mattis nibh mauris nec ante. Nunc quis viverra dolor.</p> <p>Mauris aliquam magna a convallis pretium. Duis ultricies viverra ornare. Donec suscipit risus lacinia luctus iaculis.</p>" }
  • 15. The fields. Image. array(1) { ["image_field"]=> object(TimberImage)#1008 (47) { ["object_type"]=> string(5) "image" ["post_title"]=> string(4) "test" ["id"]=> int(222) ["sizes"]=> array(3) { ... } ... // Additional methods to get the src and // the srcset } }
  • 16. The fields. Post Object. Fields defined on the New post post array(2) { ["text_field"]=> string(18) "Text field content" ["post_field"]=> object(TimberPost)#968 (86) { ["ID"]=> int(119) ["post_content"]=> string(0) "" ["post_title"]=> string(8) "New post" ["post_type"]=> string(4) "post" ["fields"]=> array(2) { ["text_field"]=> string(26) "Lorem ipsum dolor sit amet" ["post_field"]=> NULL } } }
  • 17. The fields. Repeater. array(1) { ["repeater_field"]=> array(3) { [0]=> array(2) { ["text_field_1"]=> string(26) "Lorem ipsum dolor sit amet" ["text_field_2"]=> string(27) "consectetur adipiscing elit" } [1]=> array(2) { ["text_field_1"]=> string(25) "Quisque posuere dui velit" ["text_field_2"]=> string(29) "quis egestas libero malesuada" } [2]=> array(2) { ["text_field_1"]=> string(19) "Mauris auctor augue" ["text_field_2"]=> string(22) "vitae diam consectetur" } } }
  • 18. Challenges. Field caching. While processing the fields for a certain object we need to go through all the field groups defined in ACF. Solution: Store the ids of the fields for each WordPress entity.
  • 19. Challenges. Infinite Loops. If Post A has a reference to Post B and Post B has a reference back to Post A, while retrieving the fields we would find ourselves in an infinite loop. Solution: If an object is already appended to the data variable, we append an empty array instead of the actual object.
  • 20. Summary. ● Timber ● ACF ● For basic website templates the work of the back-end developers can be almost completely replaced by the work of content managers and front-end developers