How about this?
function useragent_shortcode($tag) {
include_once( ABSPATH . 'wp-admin/includes/plugin.php' );
if ( is_plugin_active('contact-form-7/wp-contact-form-7.php') ) {
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;
} else {
//CF7 not found, hence do this
}
}
wpcf7_add_shortcode('useragent', 'useragent_shortcode', true);
I’ve modified your code a little to move the condition inside the shortcode function itself.