Object-Oriented Wordpress Themes
Composer, PSR-0, OO, Twig
Tom Forrer
Wordpress & OOP
- Wordpress Core: some parts are object-oriented components
- Plugin architecture: some are object-oriented
- Problem: communication between components via globals
- No Unit Testing of core and components
- Wordpress Core: >120k lines of code
- Themes: a bunch of PHP files accessing everything via some global
PHP Community
packagist.org
- 18 865 packages registered
- 64 245 versions available
-
89 447 424 packages installed (since 2012-04-13)
Wordpress & Composer
-
use packages of packagist.org
-
use wordpress plugins through composer with wpackagist.org
- Very nice guide: composer.rarst.net by Andrey "@Rarst" Savchenko
Demo
Twig
"The flexible, fast, and secure
template engine for PHP"
-
Concise
-
Template oriented syntax
-
Full Featured
-
Easy to learn
-
Extensible
-
Unit tested
-
Secure
Concision
<?php echo htmlspecialchars($var, ENT_QUOTES, 'UTF-8') ?>vs
{{ var }}Template oriented Syntax
<?php if ($items): ?>
<?php foreach ($item as $item): ?>
* <?php echo $item; ?>
<?php endforeach; ?>
<?php else : ?>
No items have been found
<?php endif; ?>vs
{% for items in items %}* {{ item }}{% else %}No items have been found{% endfor %}
Features
-
include views
-
extend views
-
embed views
<!-- base.html.twig --> <html><body>{% block content %}{% endblock %} </body> </html>
<!-- index.html.twig -->{% extends "base.html.twig" %}{% block content %}Some content{% endblock %}
Wordpress & Twig

"Making a Twig Proxy": TwigProxy
Wordpress Template loader
-
filter the template file with:
- home_template filter
- single_template filter
- index_template filter
- 404_template filter
- ...
-
load Twig view with
- template_include action
Use case
take the free html5up.net "Escape velocity" template
and create twig views from the HTML template:
Demo
Next steps
- Asset management with Assetic
- make ServiceContainer available for plugins
- Wordpress Wrapper in ServiceContainer: Unit Testing
- practice, gain experience with OO-Wordpress themes
Thank you
Object-Oriented Wordpress Themes
By Tom Forrer
Object-Oriented Wordpress Themes
- 2,004