0

Say there is a class in someone else's plugin that sends mail using wp_mail(). It has a public variable for the reply-to address, but it is never set. I am adding the settings field to the WordPress admin via my own plugin and want to set the public variable inside the other plugin's class. Should I extend the class?

This is the existing use of the $reply_to variable, but it isn't set and I need to set it. The rest (email, subject, content) is taken care of already. I simply need a way to set this reply-to variable.

/**
 * @class Mailer
 */
class Mailer {

    /** @var string e.g. 'John Smith <email@example.org>' */
    public $reply_to;

    function send() {

        if ( isset( $this->reply_to ) ) {
            $headers[] = 'Reply-To: ' . $this->reply_to;
        }

        $sent = wp_mail(
            $this->email,
            $this->subject,
            $this->get_html(),
            $headers,
            $this->attachments
        );

    }

}

So my line of thinking is that I need to:

  • extend the Mailer class
  • write a method to add the new settings fields for reply-to name and email to the existing settings section in the WP dashboard
  • write a method to pull the 2 settings/options values from the database and set the variable in the correct format for it to be the string for the reply-to email like this 'John Smith <email@example.org>'.

Is there a simpler way to set this public variable in someone else's plugin class?

  • 1
    The original plugin would need to offer you a way to do this via a filter somewhere. You would need to ask its developer. If it ultimately ends up using wp_mail() though, then it shouldn't be necessary, because wp_mail() itself has a filter. – Jacob Peattie Sep 5 '18 at 1:28
  • Thanks. I had a feeling that might be the case. Not real fluent in OOP yet. There is indeed a filter I can use and it's actually very simple after all. – Benbodhi Sep 5 '18 at 5:40

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Browse other questions tagged or ask your own question.