If you’re doing any type of WordPress plugin development, then odds are you’re writing your own functions.
You should be naming your functions something original enough such that they won’t collide with any core functions or any other functions that other plugins may use, but if so here are two things you can do in order to prevent that from happening:
1. Prefix Your Functions with Your Plugin Name
Seems like a no-brainer, right?
If you’re working on Your_Fancy_Plugin, then prefix all of your functions with that name:
[cc lang=”php”]
function your_fancy_plugin_my_fancy_function() {
// your code…
}
[/cc]
No other developer should be writing those functions.
2. Use Classes To Write Your Plugins
I’m a fan of object-oriented programming so, when possible, I’ll always use classes when writing code.
Aside from making code cleaner, it also restricts your functions to the context of your class.
For example:
[cc lang=”php”]
class MyFancyPlugin {
function my_fancy_function() {
// code…
}
}
[/cc]
This function can now only be accessed by:
[cc lang=”php”]
$this->my_fancy_function(); // this is within the context of the plugin
[/cc]
Either method works, though I’m obviously a fan of the latter.
Eric J says
Yep always put your plugin name in your class you don’t want to be trying to use the same function of another program if you both name your function the same for example display() or something else that is common.