Good Developers Prefix

Look around at most WordPress tutorials out there and you’ll see functions like:

function save_css(){
...
}
function update_meta_box(){
...
}

While the functions above will work they’re supremely bad practice and unfortunatley few people are talking about it.

###Why??

The above functions are bad practice because any functions that operate in a global name space (meaning pretty much every tutorial you’ve ever seen) could conflict with another theme or plugin. Say we add some custom meta box code with the function above then we add a plugin for some more custom meta box code which uses the same functions, which one gets used? Really neither gets used and you’re going to get a PHP conflict.

For each client theme I build I use two items as my prefix.

function sfn_clientname_save_css(){
...
}

The client name is obvious so I’ll just point out that my business name is [SFNdesign][sfn] and I use sfn as the base for the prefix. It’s highly unlikely that any other plugin/theme will use the same function name so I’ve most likely avoided any conflicts.

You could also use a PHP Class to sandbox your functions:

class sfn_clientname_metabox(){

	function init(){
	...
	}

	function save(){
	...
	}

}

$sfn_clientname_metabox = new sfn_clientname_metabox();

I’ll admit I don’t work with classes often and I actually don’t write them for my own code. This is simply a product of my lack of knowledge on exactly how they work. Many better developers than I use the class method all the time. I just stick with what I currently know.

###Pushing it Further

It has also [been suggested][nacin] that we prefix our Custom Post Types. While it’s hard to disagree with Nacin since he’s a seriously smart dude I’m not sure I agree in this case. Looking at it from a client perspective if they have an `event` CPT and switch to a new event managment plugin they’d loose all the event data if it was namespaced. If they both use the `event` CPT identifier than the client has a good chunk of data already in the site.

My thoughts do require that developers standardize a few CPT names like event which is probably not likely unless WordPress publickly gets behind it. I certainly don’t think that Nacin’s suggestions are bad I’m just not sure that they will give clients the experience they expect and if you namespace your CPT names you’ll certainly encourage lock in which is not what WordPress is about.

What about you? Should we be prefixing our CPT names? Any other thoughts on prefixing stuff for WordPress?

[sfn]: http://curtismchale.ca
[nacin]: http://andrewnacin.com/2010/05/11/in-wordpress-prefix-everything/ “In WordPress Prefix Everything”

Curtis McHale

Posts Twitter Google+

Curtis is a web designer/developer specialized in WordPress eCommerce development on WooCommerce. You can get in touch with him about projects on SFNdesign.
My Blog is 4 Times Faster Than Your Blog

Trackbacks and Pingbacks:

  1. Add Page Slug to body_class - October 6, 2011

    [...] Before you copy/paste this in to your site, don’t forget to change the function prefix to something that’s better suited to your site/theme/plugin. Any half way decent dev should do this if you don’t know why then read about it. [...]

  2. Working with post_class(); - October 31, 2011

    [...] the mix. All of these samples would go in your theme functions.php file and don’t forget to prefix your functions [...]

Leave a Reply

*

Text formatting is available via select HTML.

<a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <q cite=""> <strike> <strong>