1

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 ?

1
  • Can you check that the library (either the class or a function within the class) exists before including the library? May 5, 2014 at 18:25

1 Answer 1

1

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

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

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