Webtecho Blog

WordPress plugin conflicts: Avoid them with namespaces

in wordpress, Wordpress plugins

Plugin conflicts are a big headache for both developers and the users of plugin alike. The developer who is often short on time and pushed for delivery resort to oversight when it comes to writing code that does not conflict with others’ code.

For example, the most common variable to be used by wordpress developers is $option. Since it sounds like a rhyme , it is easy to code something like this

$option = get_option(….) ;

The problem is that if some other plugin uses the same name, you might see wordpress not working as expected in live sites or throwing up errors in test sites.

This can happen for PHP variables, functions, classes , CSS ids and many other things. Imagine a common function like get_html() being defined by two plugins.

The best way to counter it is ( in my opinion ) is to use proper unique name spaces.

For example all variables that are global in Share Juice Pro plugin have ‘share_juice_’ prefixed. That makes them an unlikely duplicate target. Even CSS classes  and IDs are prefixed by ‘share-juice-‘ . They make id names really long but it is better than unnecessarily spending some hours on debugging when the problem is at your own backyard.

Even conflicts on the Blog page can happen because of conflicting JavaScript function and variable names.

I learned this hard fact in Share Juice Pro 1.0 and thus 2.0 now contains the prefixed words with the variables.

Remember: Variables and other things that can be common have high degree of conflict. It is more a possibility when variable names are common ( such as $facebook, $google, $print etc. ) . Always use your unique namespaces in your coding.

{ 1 comment… read it below or add one }

Vijay Kudal May 16, 2013 at 6:06 am

I had once habit of using plugin for anything to change in wordpress,once I looked at database size and started even using plugin

can we have some site where we can have repostery of plugin,plugin creating/modifying,querying database ,frequency,name space plugins using etc etc ?


Cancel reply

Leave a Comment

Previous post:

Next post: