php文件如何引用wordpress方法

王林
王林 原创
2023-02-24 11:28:02 2583浏览

目前很多 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.'//m.sbmmt.com/m/'.$file;
               }
           }
       }
   }
   inlo_includeAll( inlo_func ); // 执行函数

以上代码二选一加入 functions.php 里面即可,加入后,只要把需要引用的 php 文件放在 inc 文件夹里面效果就如同放在functions.php 里面一样了。

以上内容仅供参考!

推荐教程:PHP视频教程

以上就是php文件如何引用wordpress方法的详细内容,更多请关注php中文网其它相关文章!

声明:本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系admin@php.cn核实处理。