DEV Community

Cover image for Simplifying WordPress's functions.php with OOP

Simplifying WordPress's functions.php with OOP

Tyler Smith on November 30, 2018

I love WordPress, and I use it for most of my clients' sites. Between its built-in features, rich plugin ecosystem and endless learning resources/d...
Collapse
 
luc45 profile image
Lucas Bustamante

Hi Tyler,

Always good to see some OOP in WP. Looks similar to a project I was developing a month ago, called Modern WordPress Website (github.com/Luc45/ModernWordPressWe...). I follow PSR-2.

Best,
Lucas

Collapse
 
tylerlwsmith profile image
Tyler Smith

Interesting! I'll have to check Modern WordPress Website out, it looks pretty spiffy!

Collapse
 
theking2 profile image
theking2

the link died unfortunately

Collapse
 
vanaf1979 profile image
Stephan Nijman

Nice article. WordPress may not have a lot of modern php, but that doesn't mean my/our code cant. I may take some of your ideas and apply them to my own OOP functions.php boilerplate: github.com/vanaf1979/functionsphp Thanks.

Collapse
 
tylerlwsmith profile image
Tyler Smith

I'm glad you potentially found some of these ideas helpful! I've been building out this class since I wrote this article. At some point I want to turn it into a Composer package so I can share easily among my themes and potentially with anyone else who is interested in it.

Collapse
 
zimaben profile image
Benny T

I really liked this post and played around with changing my construction process around it. Ultimately I didn't want to invest the time to properly figure out how to tackle filters or large $arg functions (I'm sure it's possible), but holy hell this option is elegant as hell declaring wp_ajax_ handlers. Just throw the name of the handler function in and you're set:

private function __construct()
{
$this->addAjaxHandler('feature_post' )
->addAjaxHandler('unfeature_post');

}

private function addAjaxHandler( $function_name ){

\add_action( 'wp_ajax_' . $function_name, function() use( $function_name){
self::$function_name();
} );
return $this;

}

Collapse
 
tylerlwsmith profile image
Tyler Smith

I'm glad you got some value out of the post! I hadn't even considered using this for ajax, but I'll have to try it out at some point.

Collapse
 
zimaben profile image
Benny T

Yeah it works in this case since wp_ajax follows a standard naming convention with your function and PHP will natively try to execute a variable as a function if you put the () parens after, so declaring the function name pulls double duty here.

Collapse
 
mathiu profile image
Mathiu • Edited

Action hooks fail silently, and I can't tell you how many times I've typed after_theme_setup instead of after_setup_theme.

I have created a simple theme generator years ago, its purpose was mostly to prefix some functions and create all the standard files. I gave it to one of my newly hired colleagues once and as he was finishing his first project, translations were not working exactly because of this. It was fun looking for misspelling.

Collapse
 
tylerlwsmith profile image
Tyler Smith

That's the worst! To some extent these kinds of mistakes are unavoidable, and probably a good a good argument for test driven development. I've got methods wrapping this functionality in this theme class because I'm using the same hooks over and over, but in most cases this approach is less than practical because of the sheer volume of hooks available.

Building a theme generator like the one you built sounds super cool. That's something I should definitely focus on learning soon.

Collapse
 
thomas_grgl profile image
Thomas G. • Edited

Hey, thanks for your post !

I've been using it in a little library i'm currently building, it's time to treat wordpress as an object !

I hope it doesn't bother you 😇

gitlab.com/tgeorgel/object-press

Collapse
 
tylerlwsmith profile image
Tyler Smith

I'm glad you found it useful. If you wanted to link to this article in your readme in would be appreciated but it isn't required. Best of luck with your library!

Collapse
 
thomas_grgl profile image
Thomas G.

Of course, will do ;)

Collapse
 
theking2 profile image
theking2

I am in the process of actually creating something very similar . what for wordpress is imperative is the possibility to dequeue or remove actions in a child theme. So each "add" method should have a mirroring "disable" or "remove" method.

Collapse
 
codingmindfully profile image
Daragh Byrne

You might be interested in Sage from roots.io - it’s a starter theme that makes WordPress MVC and uses Laravels blade engine

Collapse
 
tylerlwsmith profile image
Tyler Smith

I'm a fan of Sage! I'm launching a site that I built with Sage next month, and I especially love having Blade in WordPress.

It's great for big data-heavy sites, though I tend to avoid it for smaller sites because it ends up feeling like too much tool for the job. Its folder structure can overwhelm me, and I sometimes feel like using PHP namespaces with WordPress feels like a "square block in a round hole" kind of thing.

The Blade templating though; I'll definitely be using Sage again in the future for that alone!

Collapse
 
codingmindfully profile image
Daragh Byrne

Yes - I’ve made similar observations - the templating is gold, and if you hook it up to advanced custom fields life gets so much easier!

Collapse
 
grandemayta profile image
Gabriel Mayta

Awesome!

Collapse
 
tylerlwsmith profile image
Tyler Smith

Thanks Gabriel, I'm glad you liked it!

Collapse
 
lboneluv profile image
José Manuel Ramírez

Nice article. I will apply some in my projects 😉

Collapse
 
tylerlwsmith profile image
Tyler Smith

I'm glad you like it! I hope it's helpful in your projects.

Collapse
 
devellopah profile image
Islam Ibakaev • Edited

Thanks Tyler for the post and for sharing classy gist!

Collapse
 
tylerlwsmith profile image
Tyler Smith

Thanks for reading! I hope you got some value out of it.

Collapse
 
vkuberan profile image
Velmurugan Kuberan

Wonderful article. But my question is do we really need OOP for WordPress?

Collapse
 
tylerlwsmith profile image
Tyler Smith

Nope, you definitely don't need to use an object-oriented programming approach for WordPress, though there are a few exceptions like the WP_Query object. I use this approach because it solved a pain point I was having with the functions file, but I only reach for OOP when it solves a problem that I'm having.

Collapse
 
keithmifsud profile image
Keith Mifsud

If the code looks like the spaghetti in the cover photo, I would want to fix it :)