How can I include the js and css file of my custom theme dynamically in header.php

I guess you wanted to enqueue all the files in your css and js directory of your theme. Thats the reason you are trying to use the scandir(). get_template_directory_uri() returns URL not the directory path. You need to use get_template_directory() for directory.

If you wanted to do that, then do it in functions.php

function enqueue_all_front_resources(){

   $js_directory = get_template_directory().'/js/';
   $all_js_files = array_diff(scandir($js_directory), array('..', '.'));
   // to avoid .. and . from the scandir function i used array_diff

   //now add the js with wp_enqueue_script()
   foreach($all_js_files as $js_file){
     wp_enqueue_script( $js_file.'-name', get_template_directory_uri() . '/js/'.$js_file );
   }


   $css_directory = get_template_directory().'/css/';
   $all_css_files = array_diff(scandir($css_directory), array('..', '.'));

   //now add the css with wp_enqueue_style()
   foreach($all_css_files as $css_file){
     wp_enqueue_style( $css_file.'-name', get_stylesheet_uri().'/css/'.$css_file );
   }

}

add_action('wp_enqueue_scripts','enqueue_all_front_resources');

Disadvantages in Doing this

  • Unwanted resources will be added thus eats server as well browser(many file request and processing all scripts in browser makes little slow)
  • You can’t give dependency correctly unless you name the js file with dependecy at the end of file name and parse add the dependency in enqueue_scripts and you need to take care of sorting too.

I Won’t recommand this method. I answered this for the interest of automation. Since you are thinking the dynamic way. Dynamic is good but we needs to be sure that it will work correctly and efficiently.

Hata!: SQLSTATE[HY000] [1045] Access denied for user 'divattrend_liink'@'localhost' (using password: YES)