Take the 2-minute tour ×
WordPress Development Stack Exchange is a question and answer site for WordPress developers and administrators. It's 100% free, no registration required.

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);

}
share|improve this question

2 Answers 2

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.

share|improve this answer
    
That's exactly the opposite of what I'm trying to do. useragent_shortcode has to be loaded only when the CF7 plugin is active, or else the site whitescreens. –  songdogtech Apr 13 '12 at 15:12
up vote 0 down vote accepted

This works:

     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);
}
share|improve this answer

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.