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 = '') {
...
}
Answers:
Thank you for visiting the Q&A section on Magenaut. Please note that all the answers may not help you solve the issue immediately. So please treat them as advisements. If you found the post helpful (or not), leave a comment & I’ll get back to you as soon as possible.
Method 1
Wrap it in a function_exists check:
if( ! function_exists('some_pluggable_function') ) {
function some_pluggable_function()
}
}
Method 2
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.
All methods was sourced from stackoverflow.com or stackexchange.com, is licensed under cc by-sa 2.5, cc by-sa 3.0 and cc by-sa 4.0