I’m learning how to write wordpress plugins, I’ve said that before here in my blog. One of the ways I’m doing it is by downloading/installing existing plugins I see out there which may be interesting to me – they provide actions and capabilities to do things I want my plugin(s) to do … on a more general scale.
One thing I have noticed – almost all of these plugin authors do NOT use classes in their design. Their .php files are full of functions, most of the time written without the
if (!function_exists('my_function')) {
function my_function() {
// and so on
}
}
or
if (!function_exists('my_function')) :
function my_function() {
// and so on
}
}
endif;
How do they expect to avoid naming collisions? If they have to have a function by a certain name which probably exist in some other plugin developer’s file somewhere, then even if they use this !function_exists business they’ll end up getting the other author’s behavior for that function.
Why don’t these people just use classes! I know I’m coming at this from the java world where you have to use classes for everything, and there’s packaging to avoid naming collisions, but still – this seems like an obvious question to me. Do the modern php programmers have no experience with a real class-oriented language?
Here’s a silly example I’m doing. I have a plugin I’m developing to allow Zlatina’s web administrators to deal with their database of students and contacts. It’s called Bcc DBManager.
I created a subdirectory wp-content/plugins/bcc-dbmanager, with a file named bcc-dbmanager.php … duh. I want to hook into the activate_${plugin} (see this link for an explanation of that). Here’s a partial …
<?php
/*
Plugin Name: Bcc DBManager
Description: Blah blah
*/
$bccDBManager = new BCC_DBmanager();
add_action('activate_bcc-dbmanager/bcc-dbmanager.php',
array(&$bccDBManager,'_activate'));
class BCC_DBmanager {
function _activate() {
// do stuff
$this->very_common_function_name();
// or this way
BCC_DBmanager::very_common_function_name();
}
## function with a very common name
function very_common_function_name() {
// do stuff
}
}
?>
How hard is that? I certainly don’t know much about plugin development at this point. But I would have to describe the use of functions rather than class instances and methods as Code Smell.
[Edit] Since I wrote this post I found this link on Nerdaphernalia: Use Classes in your WordPress plugins to increase code portability and reduce name conflicts
Gravatars
I discovered this interesting idea today, see the website at www.gravatar.com
Play the video on the front page, it explains it quite well.
Basically here’s what’s going on with it: You sign up for a free account, you attach your favorite avatar – image of you or a cartoon character or a symbol or whatever, just so it’s an image – to that email account. Now any website that supports gravatar (such as any wordpress blog out there) where you register with your email address will show your avatar next to your comments.
Here’s a better explanation, though a bit technical for those not so tech-oriented.
Any place which supports gravatars, such as here on my own wordpress blog, any comment I make on the posts will show my gravatar image. Sign up if you want to see your gravatar on comments in this blog, or any other wordpress blog you visit.
I’ve added a sample comment to this blog entry to show you.
[Translate]