Check if action hook exists before adding actions to it

You 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.

Leave a Comment