我可以在PHP中拦截用户定义的全局函数吗?

时间:2022-12-06 05:00:06

I'm trying to add customization to a Wordpress theme. Instead of replacing the original code, I'm using a "child theme" technique where I can replace files individually.

我正在尝试为Wordpress主题添加自定义。我没有替换原始代码,而是使用“子主题”技术,我可以单独替换文件。

Now, that original code invokes the get_footer(), and I like to intercept that call so that I can insert my own html output before the actual get_footer function does its work.

现在,原始代码调用get_footer(),我喜欢拦截该调用,以便我可以在实际的get_footer函数执行之前插入我自己的html输出。

I've read about the PHP 5's __call method but that only applies to classes whereas the WP code is not using classes but global functions. So I cannot use that technique, right?

我已经阅读了PHP 5的__call方法,但这仅适用于类,而WP代码不使用类而是使用全局函数。所以我不能使用那种技术,对吗?

1 个解决方案

#1


1  

get_footer() function just find and load proper template for the footer. There is a hook in WP get_footer that rans just before template being loaded.

get_footer()函数只需找到并加载适当的页脚模板。 WP get_footer中有一个钩子,它在模板加载之前就开始运行。

Codex says that is not good to echo-es from this function, but you can use it to load your own template just before template that get_footer() loads.

Codex说这对函数的回显并不好,但是你可以在get_footer()加载的模板之前使用它来加载你自己的模板。

You can do something like this in your functions.php:

您可以在functions.php中执行以下操作:

add_action( 'get_footer', 'my_hook_fn' );
function my_hook_fn() {
   load_template('my_template_file.php');
}

Codex

法典

I use that thick in several sites, they do not use child themes, but I think this will do the trick.

我在几个网站上使用那么厚,他们不使用儿童主题,但我认为这将成功。

#1


1  

get_footer() function just find and load proper template for the footer. There is a hook in WP get_footer that rans just before template being loaded.

get_footer()函数只需找到并加载适当的页脚模板。 WP get_footer中有一个钩子,它在模板加载之前就开始运行。

Codex says that is not good to echo-es from this function, but you can use it to load your own template just before template that get_footer() loads.

Codex说这对函数的回显并不好,但是你可以在get_footer()加载的模板之前使用它来加载你自己的模板。

You can do something like this in your functions.php:

您可以在functions.php中执行以下操作:

add_action( 'get_footer', 'my_hook_fn' );
function my_hook_fn() {
   load_template('my_template_file.php');
}

Codex

法典

I use that thick in several sites, they do not use child themes, but I think this will do the trick.

我在几个网站上使用那么厚,他们不使用儿童主题,但我认为这将成功。