现在许多 WordPress 主题都不会在 functions.php 内里写入过量的自定义函数代码,一来这里是恶意代码的重灾区,二来悉数自定义函数都往 这内里塞显得很乱,所以平常我们都把需要自定义的一些功用离开零丁写一个 php 文件,然后在 functions.php 内里援用,而假如 php 文件多了, 又必需要一个个去援用,显得很贫苦,所以就有了下面的这个自定义函数,该函数能够一次性自动援用某个文件夹下的一切 php 文件。
本日就给人人引见两个函数,他们的功用相似,一个是include_once
的团体援用,另一个是require_once
的团体援用。
1、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 ); // 实行函数
2、include_once
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 ); // 实行函数
以上代码二选一到场 functions.php 内里即可,到场后,只要把需要援用的 php 文件放在 inc 文件夹内里结果就犹如放在functions.php 内里一样了。
以上内容仅供参考!
引荐教程:PHP视频教程
以上就是php文件怎样援用wordpress要领的细致内容,更多请关注ki4网别的相干文章!