好得很程序员自学网

<tfoot draggable='sEl'></tfoot>

php文件如何引用wordpress方法

目前很多 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方法的详细内容,更多请关注Gxl网其它相关文章!

查看更多关于php文件如何引用wordpress方法的详细内容...

  阅读:48次