Removing function from pluggable.php (3 posts)

  1. frietbakker
    Posted 2 years ago #

    The wp_mail function in my plugin does not override the core wp_mail.

    The function declaration in the plugin is like this:

    if (!function_exists("wp_mail")){
          function wp_mail( $to, $subject, $message, $headers = '', $attachments = array()) {

    When i remove the wp_from from pluggable.php, the wp_mail from the plugin is called. Otherwise the function from pluaggable.php is called.

    What is going on?

  2. ianhaycox
    Posted 2 years ago #

    Whilst the function wp_mail exists in pluggable.php your if statement will always evaluate to false so you never try to re-declare wp_mail.

    But you can't re-declare PHP functions, however you can override them. See http://php.net/manual/en/function.override-function.php


    rename_function('wp_mail', 'original_wp_mail' );
    override_function('wp_mail', '$a,$b,$c,$d', 'echo "My wp_mail";');

    However - I would suggest this is a really bad idea. Any other WP component (plugins, themes, core) would then use your wp_mail function and possibly break. In addition any bug fixes, filters or actions in the core wp_mail will not be actioned.

    Why not just wrap wp_mail in your own mailing function ?

  3. bcworkz
    Posted 2 years ago #

    ...your if statement will always evaluate to false...

    Not necessarily, plugin files load before pluggable.php, so a true case can occur. I just verified this on my test installation to be sure. It is intended for functions in pluggable.php to be defined by plugins, when desired, before the pluggable.php file loads. Thus the plugin definition takes precedence and the pluggable.php version is not evaluated.

    ianhaycox points out why one's replacement function must be carefully considered, properly crafted, and diligently maintained. Additionally, the pluggable version contains several hooks such that wholesale replacement of wp_mail() is almost never warranted. Never the less, it should be possible to plug the function using the OP's example script.

    It does in fact work as intended on my test installation. Usually the only reason such script would fail is if another plugin were to define the function before your plugin is able to. The behavior described after renaming the pluggable.php function indicates this is not the case.

    I'm at a loss to explain this behavior, but it is not because the if statement will always evaluate to false. Regardless, you are better off using the provided hooks or wrapping wp_mail() as ianhaycox suggested than trying to plug the function. Especially now that it does not work for some mysterious reason.

Topic Closed

This topic has been closed to new replies.

About this Topic