因此,目前我有以下代码,它显示了从主题加载的所有php文件的列表。那么,我如何编辑它以显示所有php文件或仅显示插件文件呢?
功能。php
class IncludedPartGrabber
{
private $main;
private $root;
private $switch = false;
public function setup($template)
{
$this->root = wp_normalize_path(get_theme_root()); // theme folder
$this->main = wp_normalize_path($template); // main template
return $template;
}
public function grab()
{
return array_filter(get_included_files(), array($this, \'filter\') );
}
private function filter($file)
{
$norm = wp_normalize_path($file);
if ($norm === $this->main)
$this->switch = TRUE; // after main template all files are good to be included
return $this->switch && strpos($norm, $this->root) === 0; // true if file is in theme dir
}
}
$grabber = new IncludedPartGrabber;
add_action(\'template_include\', array($grabber, \'setup\'));
add_action(\'wp_footer\', function() use($grabber) {
echo \'<pre>\';
print_r($grabber->grab()); // see your footer :)
echo \'</pre>\';
});
然后在页脚中。php
<pre><?php print_r($GLOBALS[\'grabber\']->grab()); ?></pre>
最合适的回答,由SO网友:Milo 整理而成
在里面setup
, 设置插件文件夹根目录:
$this->plugin_root = wp_normalize_path( WP_PLUGIN_DIR );
然后修改
filter
要检查文件是否位于主题根目录或插件根目录中,请执行以下操作:
return $this->switch && ( strpos($norm, $this->root) === 0 || strpos($norm, $this->plugin_root) === 0 );
或要显示所有包含的文件,请不要在中筛选任何结果
grab
:
return get_included_files();