Advertisement
sierre

Enqueue ALL the CSS files from the theme and generate to the FRONTEND ONLY

Nov 24th, 2024
34
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
PHP 3.12 KB | None | 0 0
  1. <?php
  2. /**
  3.  * Auto-enqueue all CSS files from the theme directory for frontend
  4.  */
  5. function auto_enqueue_theme_styles() {
  6.     // Only proceed if we're on the frontend
  7.     if (!is_admin()) {
  8.         // Get the theme directory path
  9.         $theme_dir = get_template_directory();
  10.        
  11.         // Function to recursively get all CSS files
  12.         function get_all_css_files($dir) {
  13.             $css_files = array();
  14.            
  15.             // Get all files in the current directory
  16.             $files = glob($dir . '/**/*.css');
  17.            
  18.             foreach ($files as $file) {
  19.                 // Skip admin-specific CSS files
  20.                 if (strpos($file, 'admin') === false) {
  21.                     $css_files[] = $file;
  22.                 }
  23.             }
  24.            
  25.             return $css_files;
  26.         }
  27.        
  28.         // Get all CSS files from theme directory
  29.         $css_files = get_all_css_files($theme_dir);
  30.        
  31.         // Sort files to ensure consistent loading order
  32.         sort($css_files);
  33.        
  34.         // Make sure style.css loads first if it exists
  35.         $main_style = get_template_directory() . '/style.css';
  36.         if (file_exists($main_style)) {
  37.             wp_enqueue_style(
  38.                 'theme-main-style',
  39.                 get_template_directory_uri() . '/style.css',
  40.                 array(),
  41.                 filemtime($main_style),
  42.                 'all'
  43.             );
  44.         }
  45.        
  46.         // Enqueue all other CSS files
  47.         foreach ($css_files as $css_file) {
  48.             // Get the file path relative to the theme directory
  49.             $relative_path = str_replace($theme_dir . '/', '', $css_file);
  50.            
  51.             // Create a unique handle for each file
  52.             $handle = 'theme-' . str_replace(['/', '.css'], ['-', ''], $relative_path);
  53.            
  54.             // Get the file's URI
  55.             $css_uri = get_template_directory_uri() . '/' . $relative_path;
  56.            
  57.             // Skip style.css as it's already enqueued
  58.             if ($relative_path !== 'style.css') {
  59.                 wp_enqueue_style(
  60.                     $handle,
  61.                     $css_uri,
  62.                     array('theme-main-style'), // Make all styles dependent on main style.css
  63.                     filemtime($css_file),
  64.                     'all'
  65.                 );
  66.             }
  67.         }
  68.     }
  69. }
  70. add_action('wp_enqueue_scripts', 'auto_enqueue_theme_styles');
  71.  
  72. /**
  73.  * Optional: Add preload for critical CSS files
  74.  */
  75. function add_critical_css_preload() {
  76.     if (!is_admin()) {
  77.         // Add preload tags for important CSS files
  78.         $critical_files = array(
  79.             'style.css',
  80.             'assets/css/critical.css'
  81.         );
  82.        
  83.         foreach ($critical_files as $file) {
  84.             $file_path = get_template_directory() . '/' . $file;
  85.             if (file_exists($file_path)) {
  86.                 $file_url = get_template_directory_uri() . '/' . $file;
  87.                 echo '<link rel="preload" href="' . esc_url($file_url) . '" as="style">';
  88.             }
  89.         }
  90.     }
  91. }
  92. add_action('wp_head', 'add_critical_css_preload', 1);
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement