目前很多 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视频教程
以上就是php文件如何引用wordpress方法的详细内容,更多请关注【创想鸟】其它相关文章!
声明:本站所有文章,如无特殊说明或标注,均为本站原创发布。任何个人或组织,在未征得本站同意时,禁止复制、盗用、采集、发布本站内容到任何网站、书籍等各类媒体平台。如若本站内容侵犯了原著者的合法权益,可联系我们进行处理。