admin管理员组文章数量:1287990
I am trying to add a rel=next
and rel=prev
link tags to the head element of image.php
template file. I have Wordpress SEO installed and I like to hook to this plugin wpseo_head
action hook to achieve that.
What I would like to do is to check if the wpseo_head
action hook exists before I attach my action to it, and if not like in the case of the plugin is not installed or deactivated, I would use wp_head
instead.
I have tried has_action('wpseo_head')
function, but the problem with that is if there are no functions attached to this hook it will return false
even if the plugin is installed activated.
if (has_action('wpseo_head')) {
add_action('wpseo_head', 'ad_add_link_tag_image_head');
} else {
add_action('wp_head', 'ad_add_link_tag_image_head');
}
What is the way to check for actions that may not exist in Wordpress?
Thanks.
I am trying to add a rel=next
and rel=prev
link tags to the head element of image.php
template file. I have Wordpress SEO installed and I like to hook to this plugin wpseo_head
action hook to achieve that.
What I would like to do is to check if the wpseo_head
action hook exists before I attach my action to it, and if not like in the case of the plugin is not installed or deactivated, I would use wp_head
instead.
I have tried has_action('wpseo_head')
function, but the problem with that is if there are no functions attached to this hook it will return false
even if the plugin is installed activated.
if (has_action('wpseo_head')) {
add_action('wpseo_head', 'ad_add_link_tag_image_head');
} else {
add_action('wp_head', 'ad_add_link_tag_image_head');
}
What is the way to check for actions that may not exist in Wordpress?
Thanks.
Share Improve this question asked Nov 15, 2012 at 9:15 Ahmad MAhmad M 2,6692 gold badges21 silver badges19 bronze badges4 Answers
Reset to default 5You cannot check if an action will be called before that happens. Even if there were already callbacks attached to the action there would be no guarantee the matching do_action()
will be used.
In your case, test with is_plugin_active()
:
if ( is_plugin_active( 'wordpress-seo/wp-seo.php' ) )
{
// do something
}
As @Barry Carlyon mentions in the comments, the directory can be renamed. You could check if a constant or a class has been defined/declared. But there is not 100% solution: Constants, functions or classes may change after an upgrade, or another plugin defines these constants already. Especially in this case: There is another WP SEO plugin available (I never understood why he used a name that was already in use …), and you could get a false positive match.
There is the function get_file_data()
. Some pseudo-code, not tested:
$wpseo_active = FALSE;
$plugins = get_option( 'active_plugins', array() );
foreach ( $plugins as $plugin )
{
if ( FALSE !== stripos( $plugin, 'wp-seo' )
{
$data = get_file_data( WP_PLUGIN_DIR . "$plugin" , array ( 'Author' ) );
if ( 'Joost de Valk' === $data[0] )
{
$wpseo_active = TRUE;
}
}
}
if ( $wpseo_active )
{
// do something
}
And that is still not safe: Names can be changed.
I found this answer partly the question,
// check for the existence of "some_hook_filter" filter
if( array_key_exists( 'some_hook_filter' , $GLOBALS['wp_filter']) ) {
//do your code here
}
However, as noted by the original contributor, this will not turn up all possible filters depending on when the code executes. Theme contributed filters will not show up at plugin loading time for example. Both filters and actions are included in this array.
I found a list of WP variables stored in the $GLOBALS array.
You can't check to see if a action exists reliably.
Because the only time the action exists is when to do_action is called or someone else adds actions to it.
So the action only exists when someone adds a action to it, or the action is called.
And if someone adds actions, it doesn't guarantee the action will be called at all.
A large problem some plugin developers can have is relying on the wp_head action, but a poorly built theme, won't call the wp_head action!
You should use function has_action
.
Function
has_action( string $hook_name, callable|false $callback = false )
This function checks if any action has been registered for a hook.
Parameters
$hook_name (string) (Required) The name of the action hook.
$callback (callable|false) (Optional) The callback to check for. Default value: false
More information
Please check for more information the WP Code Reference site: has_action
本文标签: Check if action hook exists before adding actions to it
版权声明:本文标题:Check if action hook exists before adding actions to it 内容由网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://www.betaflare.com/web/1741333860a2372925.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
发表评论