WordPress Theme Development Include Files Automatically |
X

Congrats, You are Subscribed to Receive Updates.

WordPress Theme Development Include Files Automatically


WordPress Theme Development Include Files Automatically help you to include your files directly,No need to require the files to your WordPress theme. Now, its simple function, but helps you to reduce the work of file inclusion, just copy paste your files to the corresponding director. It will include the files automatically, when WordPress works. Whenever you  develop a theme , you need to create files directory structure and make the file accordingly.

Here I made MVC  type of Directory structure, to include files directly from the folder. Don’t get panic because of the complex sub directories. Here i made simple folder structure. The following image shows you the directory structure.

 theme_folder_organiser

Here I dont need to explain much about the directory. The image explains you enough. So directly moving to the coding part. Lets begin with the CSS Folder. While developing a theme, we need to set some admin options. So we need to add some admin styles and JavaScript based on our theme needs and requirement.  Here i just created two sub folders under the CSS Folder front and admin. Lets start scanning the admin folder. Lets write code with a common scanning function.

function kv_load_css_files($kv_folder_path) {
	$kvc_files = scandir($kv_folder_path); 

	foreach($kvc_files as $kvc_file) 	{ 
		if($kvc_file === '.' || $kvc_file === '..') {continue;} 		
			
		if (preg_match('/.css/',$kvc_file)) {
			$kv_require[]=$kvc_file ; 
		} else { continue; }	
	} 
	return $kv_require ; 
}

 And now, we are going to pass the folder path to the above function and get the required files and attach with WordPress theme.

function kv_scan_css_fold_admin() {

	$kv_admin_flod_path = KV_COMMON_URL.'/css/admin/' ; 
	$kv_files =  kv_load_css_files($kv_admin_flod_path); 
	if($kv_files != null || $kv_files != '') {
		foreach($kv_files as $kv_file) 	{ 
			if($kv_file === '.' || $kv_file === '..') {continue;} 	
			$kv_file_position = strrpos($kv_file, ".");
			$kv_trimed_file_name = substr($kv_file, 0, $kv_file_position);
			 wp_enqueue_style("kvc_".$kv_trimed_file_name, get_template_directory_uri() . '/css/admin/'.$kv_file, false, "", "all");			
		}
	}
}
if(is_admin()) {
	add_action('admin_print_styles', 'kv_scan_css_fold_admin');
}

 The above function get all the css files from the ” css/admin/ ” folder and include it to your theme. likewise the following function will collect the front folder files.

function kv_scan_css_fold_front() {

	$kv_front_flod_path = KV_COMMON_URL.'/css/front/' ; 
	$kv_files =  kv_load_css_files($kv_front_flod_path); 
	if($kv_files != null || $kv_files != '') {
		foreach($kv_files as $kv_file) 	{ 
			if($kv_file === '.' || $kv_file === '..') {continue;} 	
			$kv_file_position = strrpos($kv_file, ".");
			$kv_trimed_file_name = substr($kv_file, 0, $kv_file_position);
			 wp_enqueue_style("kvc_".$kv_trimed_file_name, get_template_directory_uri() . '/css/front/'.$kv_file, false, "", "all");
			
		}
	}
}
if(!is_admin()) {
	add_action('wp_enqueue_scripts', 'kv_scan_css_fold_front');
}

 That’s it for style sheet attachment. Likewise here I attached the php files.

define('KV_COMMON_URL', get_template_directory( __FILE__ ));

function kv_load_include_files($kv_folder_path) {
	$kvc_files = scandir($kv_folder_path); 
	//$kv_require = ''; 
	foreach($kvc_files as $kvc_file) 	{ 
		if($kvc_file === '.' || $kvc_file === '..') {continue;} 		
			
		if (preg_match('/.php/',$kvc_file)) {
			$kv_require[]=$kvc_file ; 
		} else { continue; }	
	} 
	return $kv_require ; 
}


function kv_scan_include_fold_front() {

	$kv_front_flod_path = KV_COMMON_URL.'/includes/front/' ; 
	$kv_files =  kv_load_include_files($kv_front_flod_path); 
	if($kv_files != null || $kv_files != '') {
		foreach($kv_files as $kv_file) 	{ 
			if($kv_file === '.' || $kv_file === '..') {continue;} 	
			require_once($kv_front_flod_path.$kv_file); 
		}
	}
}
if(!is_admin()) {
	add_action('init', 'kv_scan_include_fold_front');
}

function kv_scan_include_fold_admin() {

	$kv_admin_flod_path = KV_COMMON_URL.'/includes/admin/' ; 
	$kv_files =  kv_load_include_files($kv_admin_flod_path); 
	if($kv_files != null || $kv_files != '') {
		foreach($kv_files as $kv_file) 	{ 
			if($kv_file === '.' || $kv_file === '..') {continue;} 	
			require_once($kv_admin_flod_path.$kv_file); 
		}
	}
}
if(is_admin()) {
	add_action('init', 'kv_scan_include_fold_admin');
}

 The same way you can add the Javascript files too.

function kv_load_js_files($kv_folder_path) {
	$kvc_files = scandir($kv_folder_path); 
	//$kv_require = ''; 
	foreach($kvc_files as $kvc_file) 	{ 
		if($kvc_file === '.' || $kvc_file === '..') {continue;} 		
			
		if (preg_match('/.js/',$kvc_file)) {
			$kv_require[]=$kvc_file ; 
		} else { continue; }	
	} 
	return $kv_require ; 
}


function kv_scan_js_fold_admin() {

	$kv_admin_flod_path = KV_COMMON_URL.'/js/admin/' ; 
	$kv_files =  kv_load_js_files($kv_admin_flod_path); 
	if($kv_files != null || $kv_files != '') {
		foreach($kv_files as $kv_file) 	{ 
			if($kv_file === '.' || $kv_file === '..') {continue;} 	
			$kv_file_position = strrpos($kv_file, ".");
			$kv_trimed_file_name = substr($kv_file, 0, $kv_file_position);
			 wp_enqueue_script("kvc_".$kv_trimed_file_name, get_template_directory_uri() . '/js/admin/'.$kv_file, false, "", "all");
			
		}
	}
}
if(is_admin()) {
	add_action('admin_head', 'kv_scan_js_fold_admin');
}

function kv_scan_js_fold_front() {

	$kv_front_flod_path = KV_COMMON_URL.'/js/front/' ; 
	$kv_files =  kv_load_js_files($kv_front_flod_path); 
	if($kv_files != null || $kv_files != '') {
		foreach($kv_files as $kv_file) 	{ 
			if($kv_file === '.' || $kv_file === '..') {continue;} 	
			$kv_file_position = strrpos($kv_file, ".");
			$kv_trimed_file_name = substr($kv_file, 0, $kv_file_position);
			 wp_enqueue_script("kvc_".$kv_trimed_file_name, get_template_directory_uri() . '/js/front/'.$kv_file, false, "", "all");
			
		}
	}
}
if(!is_admin()) {
	add_action('wp_footer', 'kv_scan_js_fold_front');
}

 That’s it. it will help you to attach your theme code files, You don’t need to attach each and every file. Just copy paste into the directory based on the code working area. If you have trouble on finding the code. Just download the files.

  Download Source

commenter

About Varadharaj V

The founder of Kvcodes, Varadharaj V is an ERP Analyst and a Web developer specializing in WordPress(WP), WP Theme development, WP Plugin development, Frontaccounting(FA), Sales, Purchases, Inventory, Ledgers, Payroll & HRM, CRM, FA Core Customization, PHP and Data Analyst. Database Management Advance Level

One comment

  1. commenter

    I found your blog post via a thread in Stack Exchange. IMHO, I think this technique is useful only when your php files don’t include dependency, like class extends another class.

Comment Below

Your email address will not be published. Required fields are marked *

*

Current ye@r *

Menu

Sidebar