WordPress – Pluggable Functions and Plugins

WordPress, in addition to the hooks and the template functions, supports pluggable functions – a simple yet efficient way of changing behavior of WordPress.

What are Pluggable Functions

Pluggable functions give you an oppurtunity to custom define a function that is used by WordPress. For a certain set of functions WordPress checks if you have already defined them. If yes, your definition is loaded, if not, the default definition is loaded. Using this you can redefine certain core functions used by WordPress.

So what?

What is so cool about this is that, without hacking, i.e., without changing any existing code in WordPress, I can change its behavior through plugins. That means just by controlling activation of certain plugins I can control behavior of WordPress. This not only makes it easy to upgrade, but also to maintain, because you need to worry only about your definition of the function.

Lets consider an example: Instead of using the standard sendmail utility, you want to use a different SMTP server to send your email. Or say you want PHPMailer to be used when WordPress sends out emails. This can be very easily done by redefining the function wp_mail($to, $subject, $message, $headers='') in your plugin and activating it. How about that!

The List

You can find the pluggable functions in the [wordpress root folder]/wp-includes/pluggable-functions.php file. We can categorize the functions in following categories:

User Login

WordPress has made the following user login functions pluggable:

  • wp_login: You can change the policy or procedure for authenticating users who login. It can very well be a third-party authentication system.
  • is_user_logged_in: This function checks whether the current user is logged in or not. You are free to change the way this check is done.
  • auth_redirect: redirects the user to the login page if the user is not logged in. This enables you to write your own login page, on which you can put your logo, banner or information. Without this you would have to hack into the current wp-login.php for your changes.
  • check_admin_referer: This function calls the hook check_admin_referer, used for the basic security of the WordPress admin area.
  • wp_redirect: The general cookie safe redirect function.

User Data

These functions retrieve user data in various scenarios. These functions allow you to choose to retain additional information about the user anywhere you want. You can then retrieve the additional information whenever the usual information is requested.

  • set_current_user: Sets the global variable $current_user which contains all the current user information. In addition to this it also invokes the hook set_current_user where you can do additional processing.
  • get_currentuserinfo: Gets information of currently logged in user. This is of tremendous help if you have some more data associated to the user that has to be retrieved now.
  • get_userdata: Returns information about an user, including the level, whose user ID is provided. This also lets you do some custom processing, e.g., you can have your own permissions system depending on the user’s level.
  • get_userdatabylogin: Similar to earlier one, but this can get information by the user login.
  • update_user_cache: Currently this function does not do much, and I could not a reference to it anywhere. But it might give a chance to have your own user cache system.

Notification

This includes functions for notifications and emails.

  • wp_mail: is used by WordPress to send emails at different events. You can completely customize it as per your requirement. As mentioned earlier, you can either choose to use PHPMailer or any other mailing system to replace the default sendmail utility.
  • wp_notify_postauthor: This is the function used to notify you about your post and their comments. This internally uses wp_mail, which you already know can be overwritten. This function can be more useful to customise the message that usually goes out with this notification, or always copy the email to someone.
  • wp_notify_moderator: Similar to the other notification functions, this lets you customize the email structure and sending mechanism.
  • wp_new_user_notification: Customize the email, whom to send and how to send when a new user registers with you.

In addition to these there are two cookie related functions: wp_setcookie and wp_clearcookie. These functions can be used to control expiry of the cookie, and hence the session or use this in conjunction with wp_login to encrypt the password.

Hooks typically let you add custom behaviour, but the pluggable functions let you change them.

Cons

As usual, I am also going to consider the cons of this approach.

  • Since you are allowed to modify the functions that WordPress is going to call, you have to comply with the function signature or interface that is already provided. e.g., in wp_mail function, even if you want to add a parameter for non-html message, you cannot do that.
  • This one is probably more severe practically, because of the nature of loading, these functions cannot be defined more than once by you. Either it will give an error that the function has been defined twice or it might override your function by something one defined by some other plugin. An implication of this is that two plugins which use these functions cannot co-exist.
  • Why are more functions not made pluggable? Can wp_search be added here!
  • If done Object Oriented way, we can make multiple definitions co-exist and make the selection at runtime.

In spite of the cons, it provides immense flexibility and power to the user. This is a cleaner way of controlling the behavior instead of hacking WordPress code.

Technorati tags: , , , , , , , , , , , , , , , , , , , , , , , , ,

Copyright Abhijit Nadgouda
About these ads

2 Responses to “WordPress – Pluggable Functions and Plugins”

  1. Lorelle on WordPress » WordPress Pluggable Functions and Plugins by Nadgouda Says:

    […] My new found friend, Abhijit Nadgouda @ iface, has a new article on WordPress Pluggable Functions and Plugins that takes a great inside look at WordPress Plugins and their functions. What is so cool about this is that, without hacking, i.e., without changing any existing code in WordPress, I can change its behavior through plugins. That means just by controlling activation of certain plugins I can control behavior of WordPress. This not only makes it easy to upgrade, but also to maintain, because you need to worry only about your definition of the function. […]


Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s

Follow

Get every new post delivered to your Inbox.

%d bloggers like this: