0

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

}
0

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.

  • 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. – markratledge Apr 13 '12 at 15:12
0

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

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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