最新消息:Welcome to the puzzle paradise for programmers! Here, a well-designed puzzle awaits you. From code logic puzzles to algorithmic challenges, each level is closely centered on the programmer's expertise and skills. Whether you're a novice programmer or an experienced tech guru, you'll find your own challenges on this site. In the process of solving puzzles, you can not only exercise your thinking skills, but also deepen your understanding and application of programming knowledge. Come to start this puzzle journey full of wisdom and challenges, with many programmers to compete with each other and show your programming wisdom! Translated with DeepL.com (free version)

plugins - Check if a class exists within a method

matteradmin9PV0评论

I'm new to OOP and I'm writing my first plugin. I want to check if the Facebook plugin has already been activated. If so, I want to skip some code:

class MyClass {
...
    function fb_js_sdk_setup() {
        // Check if Facebook plugin is activated 
        if ( class_exists( 'Facebook_WP' ) )
            return;
        // Continue if Facebook plugin is not active
        ...
    }

}

My if (class_exists()) statement isn't working. Appreciate any advice and pointers. Thanks!

I'm new to OOP and I'm writing my first plugin. I want to check if the Facebook plugin has already been activated. If so, I want to skip some code:

class MyClass {
...
    function fb_js_sdk_setup() {
        // Check if Facebook plugin is activated 
        if ( class_exists( 'Facebook_WP' ) )
            return;
        // Continue if Facebook plugin is not active
        ...
    }

}

My if (class_exists()) statement isn't working. Appreciate any advice and pointers. Thanks!

Share Improve this question asked Sep 6, 2012 at 3:49 blogjunkieblogjunkie 6081 gold badge7 silver badges18 bronze badges 3
  • On which action do your create your instance? Make sure to wait until the Facebook plugin has started. – fuxia Commented Sep 6, 2012 at 3:56
  • @toscho oh, good point. My instance is attached to init which I guess is too early. When should I load it instead? – blogjunkie Commented Sep 6, 2012 at 3:59
  • 1 wp_loaded maybe? I don’t know when the FB instance is created. Darshan’s advice is good. – fuxia Commented Sep 6, 2012 at 4:20
Add a comment  | 

1 Answer 1

Reset to default 2

You should use is_plugin_active() method to check if a certain plugin is activated. The class Facebook_WP will still exists even you deactivate the plugin.

Post a comment

comment list (0)

  1. No comments so far