至于分解它,在我的boiler plate中,我使用一个自定义函数在主题目录中查找一个名为functions的文件夹,如果它不在那里,它就会创建它。然后is创建一个包含所有。它在该文件夹中找到的php文件(如果有)并运行include();在他们每个人身上。
这样,每次我需要编写一些新功能时,我只需将一个PHP文件添加到functions文件夹中,而不必担心将其编码到站点中。
<?php
/*
FUNCTIONS for automatically including php documents from the functions folder.
*/
//if running on php4, make a scandir functions
if (!function_exists(\'scandir\')) {
function scandir($directory, $sorting_order = 0) {
$dh = opendir($directory);
while (false !== ($filename = readdir($dh))) {
$files[] = $filename;
}
if ($sorting_order == 0) {
sort($files);
} else {
rsort($files);
}
return ($files);
}
}
/*
* this function returns the path to the funtions folder.
* If the folder does not exist, it creates it.
*/
function get_function_directory_extension($template_url = FALSE) {
//get template url if not passed
if (!$template_url)$template_url = get_bloginfo(\'template_directory\');
//replace slashes with dashes for explode
$template_url_no_slash = str_replace(\'/\', \'.\', $template_url);
//create array from URL
$template_url_array = explode(\'.\', $template_url_no_slash);
//--splice array
//Calculate offset(we only need the last three levels)
//We need to do this to get the proper directory, not the one passed by the server, as scandir doesn\'t work when aliases get involved.
$offset = count($template_url_array) - 3;
//splice array, only keeping back to the root WP install folder (where wp-config.php lives, where the front end runs from)
$template_url_array = array_splice($template_url_array, $offset, 3);
//put back togther as string
$template_url_return_string = implode(\'/\', $template_url_array);
fb::log($template_url_return_string, \'Template\'); //firephp
//creates current working directory with template extention and functions directory
//if admin, change out of admin folder before storing working dir, then change back again.
if (is_admin()) {
$admin_directory = getcwd();
chdir("..");
$current_working_directory = getcwd();
chdir($admin_directory);
} else {
$current_working_directory = getcwd();
}
fb::log($current_working_directory, \'Directory\'); //firephp
//alternate method is chdir method doesn\'t work on your server (some windows servers might not like it)
//if (is_admin()) $current_working_directory = str_replace(\'/wp-admin\',\'\',$current_working_directory);
$function_folder = $current_working_directory . \'/\' . $template_url_return_string . \'/functions\';
if (!is_dir($function_folder)) mkdir($function_folder); //make folder, if it doesn\'t already exist (lazy, but useful....ish)
//return path
return $function_folder;
}
//removed array elements that do not have extension .php
function only_php_files($scan_dir_list = false) {
if (!$scan_dir_list || !is_array($scan_dir_list)) return false; //if element not given, or not array, return out of function.
foreach ($scan_dir_list as $key => $value) {
if (!strpos($value, \'.php\')) {
unset($scan_dir_list[$key]);
}
}
return $scan_dir_list;
}
//runs the functions to create function folder, select it,
//scan it, filter only PHP docs then include them in functions
add_action(\'wp_head\', fetch_php_docs_from_functions_folder(), 1);
function fetch_php_docs_from_functions_folder() {
//get function directory
$functions_dir = get_function_directory_extension();
//scan directory, and strip non-php docs
$all_php_docs = only_php_files(scandir($functions_dir));
//include php docs
if (is_array($all_php_docs)) {
foreach ($all_php_docs as $include) {
include($functions_dir . \'/\' . $include);
}
}
}