- This topic is empty.
Viewing 1 post (of 1 total)
Viewing 1 post (of 1 total)
- The forum ‘Back End’ is closed to new topics and replies.
The forums ran from 2008-2020 and are now closed and viewable here as an archive.
I wanted to know if there were any implications I may not be aware of using this code instead of supplying a function name as the second argument of add_action.
The code of mine is as follows:
<?php
add_action('wp_enqueue_scripts', function(){
$styles = ['reset', 'open-sans' => 'fonts', 'jG.min', 'style'];
$scripts = ['jq' => 'jquery', 'modernizr', 'jG.min', 'sticky', 'script'];
if(is_home()){
push($styles, 'home');
push($scripts, 'home');
}
if(is_single()){
push($styles, 'single');
push($scripts, 'single');
}
if(is_archive()){
push($styles, 'archive');
push($scripts, 'archive');
}
if(logged_in()){
push($styles, ['ab', 'admin-bar']);
push($scripts, ['ab', 'admin-bar']);
}
$css_dir = 'css';
$js_dir = 'js';
$stylesheet_dir = get_stylesheet_directory_uri();
foreach($styles as $name => $style){
$stylesheet = "$stylesheet_dir/$css_dir/$style.css";
if(strpos($style, '//') !== false)$stylesheet = $style;
if(is_int($name))$name = $style;
wp_enqueue_style($name, $stylesheet);
}
foreach($scripts as $name => $script){
if(is_int($name))$name = $script;
wp_enqueue_script($name, "$stylesheet_dir/$js_dir/$script.js", array(), '1.0.0', true);
}
});
I just don’t like having a bunch of functions being declared that can be written inline.