调用该函数没有任何好处,因为我认为它主要用于调用“用户”函数(如插件),因为编辑核心文件不是一个好选择。这是Wordpress使用的肮脏的例子
<?php
/*
* my_plugin.php
*/
function myLocation($content){
return str_replace('@', 'world', $content);
}
function myName($content){
return $content."Tasikmalaya";
}
add_filter('the_content', 'myLocation');
add_filter('the_content', 'myName');
?>
...
<?php
/*
* core.php
* read only
*/
$content = "hello @ my name is ";
$listFunc = array();
// store user function to array (in my_plugin.php)
function add_filter($fName, $funct)
{
$listFunc[$fName]= $funct;
}
// execute list user defined function
function apply_filter($funct, $content)
{
global $listFunc;
if(isset($listFunc))
{
foreach($listFunc as $key => $value)
{
if($key == $funct)
{
$content = call_user_func($listFunc[$key], $content);
}
}
}
return $content;
}
function the_content()
{
$content = apply_filter('the_content', $content);
echo $content;
}
?>
....
<?php
require_once("core.php");
require_once("my_plugin.php");
the_content(); // hello world my name is Tasikmalaya
?>
输出
hello world my name is Tasikmalaya