This email gets sent a lot to plugin devs lately:
All plugins should have unique function names, defines, and classnames. This will prevent your plugin from conflicting with other plugins or themes.
For example, if your plugin is called “Easy Custom Post Types”, then you might prefix your functions with ecpt_{your function name here}. Similarly a define of LICENSE would be better done as ECPT_LICENSE.
Please update your plugin to use more unique names.
And once in a while someone asks why we care so much.
There are 38,308 active plugins in the WordPress.org repository. If every one of them uses a global define of IS_PLUGIN_SETUP
then they will all conflict with each other. If half use a script handle of plugincss
then all those plugins will stomp over each other when it comes to enqueuing the CSS.
It’s literally a numbers game.
Every day we get at least 30 new plugin submissions to WordPress.org. That means every day at least 30 new potential conflicts show up. And it’s not just plugins. In WordPress 4.2, a new function was added: get_avatar_url()
This was a great idea that saved people countless hours of work. Unless they logged in to see the error Fatal error: Cannot redeclare get_avatar_url()
prance across their screen.
Now in this case, theme authors had previously been told to include/make themselves, but was later added to core. All theme devs hosting on WordPress.org were notified and it was posted on the change blogs. But not everyone remembers to check those. And not everyone updates their themes right away. In a way, this probably could have been communicated better, but had the themes called their function mythemename_get_avatar_url()
then this wouldn’t have been a problem.
Prefix everything. Make it unique to your plugin or theme. WordPress is ‘home free’ and shouldn’t have to, but you should.
Comments
9 responses to “Uniqueness Matters”
Out of curiosity; does WordPress.org have the ability to scan the theme/plugin repositories en masse looking for a particular function name? When core added get_plugin_url(), could you identify all the conflicting themes/plugins (at least those in the repo) immediately, or was it a slow process of user-feedback?
@Shane Gowland: Yes and no. I use Mark Jaquith’s slurper – https://github.com/markjaquith/WordPress-Plugin-Directory-Slurper – and I can search for that. The problem is anyone using OOP would show up as a false positive.
Also just out of curiosity… I do that when I make changes or when I need functions in child themes. I prepend “mome_” which is an abbreviation of our company name. Those prefixes don’t prevent collisions, just make them more unlikely and this is the goal, right? It would probably be much harder if there were 40 million plugins, so do you know where I could read up on this? I can see that leading up to something like UUIDs as prefixes π
@Floutsch: 40k plugins Only π
The goal is to make them unlikely to conflict with other plugins, that’s right. I often use helf_plugin_ in mine, so helf_jaa_, or I use namespaces or OOP to have normal function names and then a unique object name.
What specially were you looking to read up on?
@Ipstenu (Mika Epstein): I’d be interrested in reading about that kind of collision handling on a larger scale. Like what would be the best approach if plugin numbers were like thousandfold. I just image you to be the kind of person who knows that π
@Floutsch: If we had 10 fold plugins, I would automate that. As is, it’s not generally necessary.
@Floutsch: As Otto said.
In a weird way, with the high number of plugins, it’s less of an issue than it might be. As long as we all remember to use OOP or a prefix, the numbers of collisions are low. Had themes done that, then, get_the_avatar wouldn’t have been a thing.
I have to ask a totally random question…Did you recently visit Ovid, MI to take that picture or did you find it online somewhere. I pass that ugly place every day on my way to and from work. π
@AJ Morris: No, I got it from MorgueFile (which is where I get most of the images for this site). They’re Creative Commons and free to use with or without attribution π