A look at WordPress Pluggable Functions

There are a list of PHP functions in WordPress called Pluggable Functions used internally by WordPress to execute certain tasks.

The aim of these functions is to give plugin developers the ability to modify or override tasks perform by these functions.

A full list of pluggable functions is available at the codex page.

Note: for safety, it is best to always wrap your defined pluggable functions with if ( !function_exists() ), otherwise a fatal errors will be produced on plugin activation.

An Example

Let’s see some code example to further grasp the concept of Pluggable Functions.

The function wp_new_user_notification is a pluggable function used internally by WordPress to notify the website administrator of a newly registered user via email and also mail the new users their login credentials ( username and password).

Below is the code for this function. (gotten from wp-includes/pluggable.php).


function wp_new_user_notification($user_id, $plaintext_pass = '') {
    $user = get_userdata( $user_id );

    // The blogname option is escaped with esc_html on the way into the database in sanitize_option
    // we want to reverse this for the plain text arena of emails.
    $blogname = wp_specialchars_decode(get_option('blogname'), ENT_QUOTES);

    $message  = sprintf(__('New user registration on your site %s:'), $blogname) . "rnrn";
    $message .= sprintf(__('Username: %s'), $user->user_login) . "rnrn";
    $message .= sprintf(__('E-mail: %s'), $user->user_email) . "rn";

    @wp_mail(get_option('admin_email'), sprintf(__('[%s] New User Registration'), $blogname), $message);

    if ( empty($plaintext_pass) )
        return;

    $message  = sprintf(__('Username: %s'), $user->user_login) . "rn";
    $message .= sprintf(__('Password: %s'), $plaintext_pass) . "rn";
    $message .= wp_login_url() . "rn";

    wp_mail($user->user_email, sprintf(__('[%s] Your username and password'), $blogname), $message);

}

What to achieve

The mail sent to the WordPress admin when there is a newly registered user contains the username and email of the new user but the one sent to the user contain the username and password.

What we are going to do is modify the default behaviour of wp_new_user_notification() so that the mail sent to the admin will also include the password of the user as well as the username and email. Grab?

Below is the modified function.


if ( !function_exists('wp_new_user_notification') )  {
    function wp_new_user_notification($user_id, $plaintext_pass = '') {
        $user = get_userdata( $user_id );

        // The blogname option is escaped with esc_html on the way into the database in sanitize_option
        // we want to reverse this for the plain text arena of emails.
        $blogname = wp_specialchars_decode(get_option('blogname'), ENT_QUOTES);

        $message  = sprintf(__('New user registration on your site %s:'), $blogname) . "rnrn";
        $message .= sprintf(__('Username: %s'), $user->user_login) . "rnrn";
        $message .= sprintf(__('E-mail: %s'), $user->user_email) . "rn";

        /** 
         * include the password among the message sent to the blog admin
         */
        $message .= sprintf(__('Password: %s'), $plaintext_pass) . "rn";

        @wp_mail(get_option('admin_email'), sprintf(__('[%s] New User Registration'), $blogname), $message);

        if ( empty($plaintext_pass) )
            return;

        $message  = sprintf(__('Username: %s'), $user->user_login) . "rn";
        $message .= sprintf(__('Password: %s'), $plaintext_pass) . "rn";
        $message .= wp_login_url() . "rn";

        wp_mail($user->user_email, sprintf(__('[%s] Your username and password'), $blogname), $message);

    }
}

To use the function, create a site specific or dummy plugin and dump the code in it.

Keep in Mind

Placing a function meant to override a pluggable function in your theme’s functions.php file won’t produce the desire result because, in WordPress execution order, the Plugin is called first followed by Pluggable Functions and Themes.

Pluggable functions are no longer being added to WordPress core. All new functions instead use filters on their output to allow for similar overriding of their functionality.

Have any question or contribution? Let me know in the comment section.

Don’t miss out!
Subscribe to My Newsletter
Invalid email address