1

I'm kinda new to wordpress and programming in general. I have the following question.

Recently I came across this article and wanted to use this approach in the wordpress theme I'm working on.

I wrote this Enqueue class for enqueuing my styles and js to to the theme:

<?php

class Enqueue
{

   private function actionEnqueueScripts($function) {
        add_action('wp_enqueue_scripts', function () use ($function) {
            $function();
        });
    }


   public function addStyle($handle, $src = '', $deps = array(), $ver = false, $media = 'all') {
        $this->actionEnqueueScripts(function () use ($handle, $src, $deps, $ver, $media) {
            wp_enqueue_style($handle, $src, $deps, $ver, $media);
        });
        return $this;
    }

}

So that, I can add styles in my function.php like this:

require_once 'vendor/class-enqueue.php';

$theme = new Enqueue;

$theme->addStyle('stylesheet-1', get_template_directory_uri() . '/dist/assets/css/bundle.css', array(), time(), 'all')
      ->addStyle('stylesheet-2', get_template_directory_uri() . '/dist/assets/css/bundle.css', array(), time(), 'all');

My question is: is it not a bit inefficient? Because, every time I add a css file, doesn't it calls the add_action hook 'wp_enqueue_scripts ? whereas, in a procedural approach I'd call this action hook only once for all the styles? is it okay?

PS: if it's okay, would it be possible to add conditional to the addStyle method? like this

$theme->(if(is_single)) addStyle('stylesheet-1', get_template_directory_uri() . '/dist/assets/css/bundle.css', array(), time(), 'all')

Thank you for reading.

1 Answer 1

2

(side note). Writting classes do not mean your code is OOP. Many people confuse the use of classes with OOP while classes are just a syntax that lets you group methods and data together. For OOP you actually need to figure out what are the objects your system has and what operations can be done on them.

Bad OOP can actually lead to misleading code. For example this line from that post

$theme->addSupport('post-thumbnails');

but in practice it is not the theme to which a support of post thumbnails is added but actually to the wordpress admin so this would have been more accurate code

$wp->addThemeSupport($theme, 'post-thumbnails');

Getting the theoretical garbage out of the way we can focus on the actuall question ;)

  1. Yes, as in every other bad OOP implementation you get very verbous code with no actual benefit, but.... it is just a coding style, and if this kind of "wrapping everything with classes" make you more productive you should go for it, but there is nothing intrinsicly better about OOP over procedural and functional code.

  2. From the efficiency POV it do not realy matter how many times you add an action as the overhead is tiny compared to the bottlenecks which are usually related to DB access. Your ability to read and maintain the code over time is x100 more important than some micro optimization.

4
  • yes and in many cases we can just use namespaces as method wrappers instead.
    – birgire
    Aug 9, 2021 at 12:34
  • Thank you, I've just recently started learning OOP and I've done wordpress a bit longer. So I thought I'd learn OOP along with wordpress. Butsince I've asked the question, I've been reading about it and actually starting to think in this case it's actually overkill. For enqueuing scripts and theme supports it's too much hassle but i think for some cases like customizer and widgets it's easier to code using OOP. And this bugs me using two styles. Thank you for your response. Aug 9, 2021 at 13:59
  • widget is one of the few areas in core in which OOP is done almost properly. It will be an hassle to try to do it in a procedural way there so there is not much choice there unless you want to make your life harder :) Aug 9, 2021 at 14:02
  • It just bugs me that i have two styles, but I guess nothing can be done. Thank you for your response. I'm new, so I can't upvote. Aug 9, 2021 at 14:05

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.