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 am currently developing a Wordpress plugin handling very specific behaviours in our new Wordpress website. I started by reading the introduction articles in the Codex, and I also found the Wordpress plugin boilerplate. It is said that

to avoid function and variable collisions, we should enclose our functions inside a class, which should be named after our plugin.

The boilerplate already seems to handle that, fine!

However, I also expect to use third party libraries for my plugin (Swiftmailer, Mixpanel, etc.), which will be placed in '/wp-content/plugins/myplugin/includes'.

My question is, what if an other plugin also uses one of those libraries in its code? Swiftmailer is a pretty common library, so an other library could load it when initialized. There will be a collision (a class cannot be redefined in PHP). Are there mechanism in Wordpress to handle that ? Or does somebody already have a solution for this kind of issue ?

share|improve this question
    
Can you check that the library (either the class or a function within the class) exists before including the library? –  helgatheviking May 5 at 18:25

1 Answer 1

up vote 0 down vote accepted

I have had to do this before, and what I find the easiest for me to do is something like this:

    function check_for_library() {
        return class_exists( 'Class_Of_Library_In_Question' );
    }

Then you just fill in the right details and make a call to this function.

    if( ! check_for_library() ) {
        include_once( 'library_file.php' );
    }
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.