php文件如何引用wordpress方法
1、目前很多 WordPress 主题都不会在 functions.php 里面写入过多的自定义函数代码,一来这里是恶意代码的重灾区,二来全部自定义函数都往 这里面塞显得很乱,所以一般我们都把需要自定义的一些功能分开单独写一个 php 文件,然后在 functions.php 里面引用,而如果 php 文件多了, 又必须要一个个去引用,显得很麻烦,所以就有了下面的这个自定义函数,该函数可以一次性自动引用某个文件夹下的所有 php 文件。
2、今天陌小雨就给大家介绍两个函数,他们的功能类似,一个是include_once的集体引用,另一个是require_once的集体引用。
3、require_once define猾诮沓靥('inlo_func', TEMPLATEPATH.'/inc'); // 定义集体 php 所在的文件夹 inc function inlo_requireAll( $dir ){ // require_once 集体引用 php foreach( glob( "{$dir}/*.php" ) as $filename ) require_once $filename; } inlo_requireAll( inlo_func ); // 执行函数
4、include_on罕铞泱殳ce define('inlo_func', TEMPLATEPATH.'/inc'); // 定义集体 php 所在的文件夹 inc function inlo_includeAll( $dir ){ // include_once 集体引用 php $dir = realpath( $dir ); if($dir){ $files = scandir( $dir ); sort( $files ); foreach( $files as $file ){ if( $file == '.' || $file == '..' ){ continue; }elseif( preg_match('/.php$/i', $file) ){ include_once $dir.'/'.$file; } } } } inlo_includeAll( inlo_func ); // 执行函数
5、以上代码二选一加入 functions.php 里面即可,加入后,只要把需要引用的 php 文件放在 inc 文件夹里面效果就如同放在 functions.php 里面一样了。