bbPress Digest 2.1: Customizing Emails

I’ve finally released update of my WordPress/bbPress plugin bbPress Digest that fixes all existing issues and that’s compatible with latest versions of both software.

Additionally, I’ve added a few hooks that enable developers to customize email that is sent to users. Thanks to Gawain Lynch whose patch gave me idea about that.

So how to use these new hooks? The point was to use wp_mail() filters so what I did was to add actions that are fired just before and after email was sent. All possible parameters are passed so you can do as much customization as possible. See BBP_Digest_Event::mail() how that looks like.

And here is one example of using this. Here we will change email address and name of sender, subject of email, change type of email to HTML and append additional content. This is just a proof of concept, to give you an idea how to use this, you can obviously do whatever you need.


/**
 * Filter digest emails.
 */
class MD_BBP_Digest_Event_Filter_Email {
  /**
   * Set class properties.
   */
  public function __construct() {
    add_action( 'bbp_digest_before_mail', array( $this, 'register'   ), 10, 7 );
    add_action( 'bbp_digest_after_mail',  array( $this, 'deregister' ), 10, 7 );
  }

  /**
   * Register email filter.
   */
  function register( $email_address, $subject, $message, $user, $topic_ids, $period, $event ) {
    add_filter( 'wp_mail',              array( $this, 'email'        ) );
    add_filter( 'wp_mail_from',         array( $this, 'from'         ) );
    add_filter( 'wp_mail_from_name',    array( $this, 'name'         ) );
    add_filter( 'wp_mail_content_type', array( $this, 'content_type' ) );
  }

  /**
   * Deregister email filter.
   */
  function deregister( $email_address, $subject, $message, $user, $topic_ids, $period, $event ) {
    remove_filter( 'wp_mail',              array( $this, 'email'        ) );
    remove_filter( 'wp_mail_from',         array( $this, 'from'         ) );
    remove_filter( 'wp_mail_from_name',    array( $this, 'name'         ) );
    remove_filter( 'wp_mail_content_type', array( $this, 'content_type' ) );
  }

  /**
   * Filter email.
   */
  function email( $args ) {
    $args['subject'] = 'Forum activity';
    $args['message'] .= '<p>Sent from <a href="http://example.com">Example Forum</a></p>';

    return $args;
  }

  /**
   * Filter email sender's email address.
   */
  function from( $from ) {
    $from = 'notifier@example.com';

    return $from;
  }

  /**
   * Filter email sender's name.
   */
  function name( $name ) {
    $name = 'Example Forum';

    return $name;
  }

  /**
   * Filter email content type.
   */
  function content_type( $content_type ) {
    $content_type = 'text/html';

    return $content_type;
  }

}

new MD_BBP_Digest_Event_Filter_Email;

I apologize to everyone who waited for this necessary update for more than a year, I can partly blame core bbPress developers for not keeping backward compatibility that broke bbPress Digest, and I hope that this will change in the future.

Leave a Reply

Comment Guidelines:
  • Your email address will not be published
  • Fields marked with * are required
  • If you want your picture next to comment, open an account at Gravatar
  • If you want to know if someone replied to your comment, check “Notify me of followup comments via e-mail”
  • You can use these HTML tags: <a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <q cite=""> <s> <strike> <strong>