1

I just not only want to override a pluggable function (wp_new_user_notification) but I want to make sure that if any other plugin is overriding the same function, so my function , override that too or I want my overridden function to work instead of other plugin that is overriding the same function (wp_new_user_notification). May be a silly question but can I do it ??

2 Answers 2

2

The short answer to your question is yes; however, the methods to do this are a little bit hacky and generally I would recommend against all of them unless absolutely necessary.

Since pluggable functions actually replace the stock function by way of function_exists() whichever plugin that "plugs" the function first will win. What you want to do is ensure your plugin loads first globally.

Plugins are loaded according to their order in an array stored in the wp_options table. This array is sorted alphabetically. Knowing that, you have 2 options.

  1. Make it very likely that your plugin is alphabetically first. I believe that @ is considered alphabetically first in php, so if you start your plugin name with something like @@MyPlugin then it should get loaded first.
  2. Change the order of the array stored in the database using the activated_plugin hook. I did a quick google search and found that someone had already written a function here. I've re-posted it below in case the link goes dead.

    add_action( 'activated_plugin', 'my_plugin_load_first' );
    function my_plugin_load_first(){
        $path = str_replace( WP_PLUGIN_DIR . '/', '', __FILE__ );
        if ( $plugins = get_option( 'active_plugins' ) ) {
            if ( $key = array_search( $path, $plugins ) ) {
                array_splice( $plugins, $key, 1 );
                array_unshift( $plugins, $path );
                update_option( 'active_plugins', $plugins );
            }
        }
    }
    

    Place this function in your plugin and it will re-order the array so that your plugin is loaded first.

The other option that you have is to omit the if( function_exists()) statement when you declare your version of the function. If you do that, and someone else has plugged that function then php will exit with a fatal error "function already exists". This will have the effect of preventing someone else from plugging the function but it will also prevent the site from loading so I doubt that is a viable option for you.

5
  • Thank you so much for the answer , I tried 2nd method and i got this error Parse error: syntax error, unexpected T_STRING in /home/agtech/public_html/wp-content/plugins/disable.php on line 54 and you can see the line number here link Jul 15, 2015 at 21:03
  • What is on line 54? unexpected T_STRING usually means something along the lines of a missing quote or semicolon. Jul 15, 2015 at 21:08
  • sorry , line 55 and that line is $path = str_replace( WP_PLUGIN_DIR . '/', '', __FILE__ ); Jul 15, 2015 at 21:11
  • Weird, it works fine for me. Try replacing that line with $path = end(explode('/',__FILE__)); That should do the same thing. Jul 15, 2015 at 21:53
  • same error , only wp is giving this error :/ Jul 15, 2015 at 22:03
0

Just needed to create a little comment in here. Pluggable functions are introduced in early days of WordPress (version 1.5.1) when we had just a few plugins. Nowadays, when many plugins may use the same function (say: wp_mail) we are in trouble.

I found this blog article has the point. What he suggested as a solution to pluggable functions is to use filters. He called the method short circuiting, where instead of this:

if ( ! function_exists( 'f1' ) ){
function f1( $name ) {
    echo 'Hello ' . $name;
}
}

you would have :

function f1( $name ) {      
    $pre = apply_filters( 'f1', false, $name );    
    if ( false !== $pre ) {
        echo $pre;
        return;
    }    
    echo 'Hello ' . $name;
}

Great, but I can imagine the solution not even using the pluggable function. You simple copy/paste the code from the pluggable function to your new plugin function that has different name/class and you are done.

Of course, if the pluggable function ever changes you simple release the new plugin version.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

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