Home >Backend Development >PHP Problem >How to reference wordpress methods in php files

How to reference wordpress methods in php files

王林
王林Original
2019-09-18 13:05:053199browse

How to reference wordpress methods in php files

Currently many WordPress themes do not write too many custom function codes in functions.php. Firstly, this is the hardest hit area for malicious code, and secondly, all custom functions It seems very messy to stuff them all into this, so generally we separate some functions that need to be customized and write a separate php file, and then reference them in functions.php. If there are too many php files, we must reference them one by one. It seems very troublesome, so we have the following custom function, which can automatically reference all php files in a certain folder at one time.

Today I will introduce to you two functions. Their functions are similar. One is the collective reference of include_once, and the other is the collective reference of 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 ); // 执行函数

Choose one of the above codes and add it to functions.php , after joining, just put the php file that needs to be referenced in the inc folder, and the effect will be the same as putting it in functions.php.

The above content is for reference only!

Recommended tutorial: PHP video tutorial

The above is the detailed content of How to reference wordpress methods in php files. For more information, please follow other related articles on the PHP Chinese website!

Statement:
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn