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.