I'm trying to use is_plugin_active within functions.php.
I need to wrap my useragent_shortcode function within is_plugin_active because when Contact Form 7 gets deactivated, I get a white screen.
I've included wp-admin/includes/plugin.php as shown in other WPSE answers because this is outside of admin, but no luck. The shortcode function doesn't run and the raw shortcode is echoed to the page.
Any ideas? Is my code too complex for what I want to do?
4/16/12 Update: My own answer is below.
This works in functions.php:
If (in_array( 'contact-form-7/wp-contact-form-7.php', apply_filters( 'active_plugins', get_option( 'active_plugins' ) ) ) ) {
function useragent_shortcode($tag) {
if ( ! is_array( $tag ) )
return '';
$options = (array) $tag['options'];
foreach ( $options as $option ) {
if ( preg_match( '%^name:([-0-9a-zA-Z_]+)$%', $option, $matches ) ) {
$name_att = $matches[1];
}
$userAGENT .= $_SERVER['HTTP_USER_AGENT'];
}
$user_agent = $userAGENT;
$html = '<input type="hidden" name="' . $name_att . '" value="'.$user_agent.'" />';
return $html;
}
wpcf7_add_shortcode('useragent', 'useragent_shortcode', true);
}