admin管理员组文章数量:1323714
Is it possible to run add_action
under a certain condition?
For example, let's say I wanted to echo "Hello" in the header on the front page and then use the same function, but echo "Hello" in the footer for the archive page.
function say_hello() {
echo '<p>Hello!</p>';
}
// Echo in wp_head on the front page
if ( is_front_page() ) {
add_action('wp_head', 'say_hello');
}
// Echo in wp_footer on the archive
if ( is_archive() ) {
add_action('wp_footer', 'say_hello');
}
The above doesn't work, any ideas?
Is it possible to run add_action
under a certain condition?
For example, let's say I wanted to echo "Hello" in the header on the front page and then use the same function, but echo "Hello" in the footer for the archive page.
function say_hello() {
echo '<p>Hello!</p>';
}
// Echo in wp_head on the front page
if ( is_front_page() ) {
add_action('wp_head', 'say_hello');
}
// Echo in wp_footer on the archive
if ( is_archive() ) {
add_action('wp_footer', 'say_hello');
}
The above doesn't work, any ideas?
Share Improve this question edited Sep 10, 2020 at 16:11 Sam asked Sep 10, 2020 at 16:04 SamSam 2,1963 gold badges30 silver badges59 bronze badges1 Answer
Reset to default 1I'd guess this code is being run directly from your plugin's functions.php, meaning that you're evaluating is_front_page()
and is_archive()
as you load the plugin. That's too early - that's before enough state has been initialised to know which page you're on.
To fix this, you either need to:
- move the logic into your say_hello functions, and check is_front_page or is_archive at the point that you're rendering the header or footer:
function say_hello_in_header() {
// Echo in wp_head on the front page
if ( is_front_page() ) {
echo '<p>Hello!</p>';
}
}
add_action( 'wp_head', 'say_hello_in_header' );
function say_hello_in_footer() {
// Echo in wp_footer on the archive
if ( is_archive() ) {
echo '<p>Hello!</p>';
}
}
add_action( 'wp_footer', 'say_hello_in_footer' );
- or pick an action to hook that's after all of the state has been set up but before the header and footer have been rendered, e.g. wp_enqueue_scripts, and perform the tests and the head and footer actions there:
function enqueue_say_hellos() {
// Echo in wp_head on the front page
if ( is_front_page() ) {
add_action( 'wp_head', 'say_hello' );
}
// Echo in wp_footer on the archive
if ( is_archive() ) {
add_action( 'wp_footer', 'say_hello' );
}
}
add_action( 'wp_enqueue_scripts', 'enqueue_say_hellos' );
That all said, please note that wp_head is not the correct place to add HTML to the page, since it's run from the page's <head> tag and not the <body> tag. Any content you add there will appear in the wrong place if at all. If you want to modify the header on your page you'll have to edit your template or find some other mechanism.
本文标签: actionsRun addaction hook if condition
版权声明:本文标题:actions - Run add_action hook if condition 内容由网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://www.betaflare.com/web/1742127218a2421995.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
发表评论