Always use prefix on your code!
Many people when start developing plugins and themes to Wordpress don't notice the consequences of writing messy codes until it is too late.
I decided to decicate a whole post to this prefix matter because I know I will come back to it always when possible, and it will be easier to have a post to link instead of repeating myself.
Using a prefix means that, for each plugin or theme we create, we choose a small string that describes this piece of code, and in every function/class, and even hook tags, we add this string before its name.
It may seem boring to those people that still don't understand why this is important, but I'll explain.
First, let's see an exemple of prefix in use. Suppose you develop a plugin named "My Worderful Plugin", you could abbreviate it to MWP, and use /mwp/
as its folder name. Folder names are unique, they all go inside .../plugins/
or .../themes/
, and if 2 plugins or 2 themes try to use the same folder name, it won't be possible.
Therefore, if you use your plugin/theme folder as your functions prefix, you'll be sure that no other code will use the same prefix (well, 1 plugin and 1 theme could yet, but it at least narrows down the possibilities).
Now suppose your plugin has a variable that stores its version, and you decide to add the following code in the top of your main file:
1 | $vesion = '1.0'; |
What will happen if another plugin developer decides to have the same brilliant idea of having a variable storing his plugin version, and of course
use the same $version
name for it? What will happen is that the least plugin to startup will overwrite the previous one value.
With variables at least it may work, but if you use the same function or class name in 2 different PHP codes, you will receive a fatal error and your whole website will probably not finish being built. And if you do it in JavaScript, the least file to be presented in the website document will overwrite the fist one's variable/function/class/whatever, and the first one will break or both will stop working.
Using prefixes you avoid this problem. You can name your stuff whatever you want, because you will be (almost) sure that the prefix is exclusively yours, and therefore all your names will be unique.
Taking the previous exemple, instead of naming your variable as $version
, you'd name it $mwp_version
. If you have a function named function printStats(...){...}
, it will be function mwp_printStats(...){...}
.
Same thing for classed, and BTW, using a class instead of a bunch of spreaded functions is a good idea, because as long as you have uniquely prefix-named classes and objects, you don't have to use prefix in their methods and fields/variables, so depending on your prefix you can even use it as your class name and use all its methods and variables statically (without instantiating objects).
Indeed, that's what is done in JavaScript, using a "class" as a namespace and storing all your functions and variables inside it.
OZH had also talked about it in his Top 10 Most Common Coding Mistakes in WordPress Plugins (http://planetozh NULL.com/blog/2009/09/top-10-most-common-coding-mistakes-in-wordpress-plugins/):
Way too generic function names
40% of the plugins I've reviewed use a waaaaay too generic function naming scheme. The point here is to make sure your plugin will never trigger a "Fatal error: Cannot redeclare your poorly named function" error.
Function names must be two things: descriptive and unique. I've found plugins with functions named as simply as
pretty_title()
,pages()
orupdate_options()
. Ironically enough, a coder submitted several plugins that won't be able to run on the same blog because they all use the same function declarations.
Better function names would have been for instance
joeplugin_post_pretty_title()
,joeplugin_list_pages()
orjoeplugin_update_options()
. An alternative to keep function names short is to wrap them into a class (that also must have a unique and descriptive name).
I hope I let it clear enough about how important it is to use prefixes, even in small plugins (bigger ones could use the same function name your 2-functions plugin uses, and wonder who users will blame!), and everybody will understand why all my codes exemples will have prefix_
everywhere
Popularity: 4%
It has accumulated a total of 23,588 views. You can follow any comments to this article through the Comments RSS 2.0 Feed. You can leave a comment, or trackback from your own site.
Readers who viewed this page, also viewed:
Related Posts:
- Finding theme folders for parent and child
- Uninstalling my plugins
- How is Wordpress winning the war?
- Wordpress comments: new built-in feature X Wordpress…
- Hikari Titled Comments
- Hikari Internal Links - Exemples
- Hikari Krumo
- Hikari Email & URL Obfuscator - Advanced Usage
- Hikari Hooks Troubleshooter
- Hikari Featured Comments
Comentando vc contribui e participa na criação do conteúdo do site.
Contribua. Commente. :)
(Os comentários abaixo representam a opinião dos visitantes, o autor do site não se responsabiliza por quaisquer consequências e/ou danos que eles venham a provocar.)