Take the 2-minute tour ×
WordPress Development Stack Exchange is a question and answer site for WordPress developers and administrators. It's 100% free, no registration required.

How can I override a function in pluggable.php?

I have tried making my own plugin -- got the fatal error on function already defined.

I tried functions.php in my theme -- got the white screen.

Is it possible to override a pluggable.php function without touching the source code file itself?

Thanks.

Here is the function I wish to override (located in ../wp-includes/pluggable.php):

if ( !function_exists('wp_new_user_notification') ) :
/**
 * Notify the blog admin of a new user, normally via email.
 *
 * @since 2.0
 *
 * @param int $user_id User ID
 * @param string $plaintext_pass Optional. The user's plaintext password
 */
function wp_new_user_notification($user_id, $plaintext_pass = '') {
...
}
share|improve this question
    
Out of curiosity, which function are you trying to override? And can you provide the code (or a snippet) so we can see in what scope you are defining your function? –  William Jun 26 '12 at 15:07
    
@William -- I revised my question to include the function() I am trying to override. –  frank13 Jun 26 '12 at 15:10
    
Try putting the code in your theme functions.php and setting define('WP_DEBUG', true); in your wp-config.php file so you can see what errors are being thrown. EDIT: by "the code" I mean your function override. –  William Jun 26 '12 at 15:11
    
Ok, will do @William. I got the white screen first time through...this time I will use debug. My guess is it will yell at me for a duplicate fucntion. –  frank13 Jun 26 '12 at 15:15

2 Answers 2

up vote 1 down vote accepted

Wrap it in a function_exists check:

if( ! function_exists('some_pluggable_function') ) {
    function some_pluggable_function()
    }
}
share|improve this answer
    
Thanks @Milo but that merely prevents the fatal error. It does not result in my function superseding the pluggable.php call hence my function ends up not running -- the pluggable.php function does. –  frank13 Jun 26 '12 at 14:55
    
Here's the answer: See sltaylor.co.uk/blog/… It does not make sense that you need to wrap your overriding function with the function_exists() even though it is wrapped in pluggable.php, but you do! That's the trick. SO you are right @Milo. –  frank13 Jun 26 '12 at 15:42
    
the reason it needs to be wrapped in function_exists is because within the context of a plugin activation, the function does already exist. This is due to the way WordPress activates plugins in a sandbox, which is necessary to enable it to recover from fatal errors in the activated plugin. Technically, once the plugin is activated the function_exists check would not be necessary, but you'd never be able to activate that plugin via conventional means without it. –  Milo Sep 24 '12 at 15:57

The correct way to override functions in pluggable.php is to redefine the same function in a plugin. Your plugin's function will override the one in pluggable.php since the WP one is wrapped in a call to function_exists().

But make sure you're only doing this once. From the Codex:

Note: A function can only be reassigned this way once, so you can’t install two plugins that plug the same function for different reasons. For safety, it is best to always wrap your functions with if ( !function_exists() ), otherwise you will produce fatal errors on plugin activation.

From what you describe happening, it sounds like more than one plugin is trying to override the same function.

share|improve this answer
1  
Thanks @EAMann but "I have tried making my own plugin -- got the fatal error on function already defined." –  frank13 Jun 26 '12 at 15:13

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.