Skip to:
Content
Pages
Categories
Search
Top
Bottom

Hook to bbpress notifications


  • t3_rrY
    Participant

    @t3_rry

    Hi everyone,

    though I’ve used this forum a lot lately, here’s my first post:

    I’m using a custom plugin which uses Mandrill API instead of wp_mail() to notify (future) users.
    I’m trying to hook bbpress notification to use templates with Mandrill yet after hours of search around the web I can’t find how to do so. Could you please, help me on this one?
    I’ve already created a setting which enables me to pass the name of the wanted template but I don’t know how to superseed bbpress wp_mail() method…

    Thanks in advance

Viewing 12 replies - 1 through 12 (of 12 total)

  • t3_rrY
    Participant

    @t3_rry

    I must add that Mandrill do send the notification as expected yet I’d want it to use the proper template to do so; it appears that the email type is plain text.


    Kolya Korobochkin
    Participant

    @korobochkin

    bbPress have two types of notifications: new message in topic or new topic in forum. Functions that sends messages in bbpress/includes/common/functions.php. You can’t redeclare they. You need to remove default functions and add your custom one. Here is my working code for armyofselenagomez.com wich sends emails via external SMTP server (like Mandrill. but I use Yandex, like Gmail but in Russia). Be careful with priorities and numbers of arguments to pass.

    Logis of process:

    1. User leave a message or create a topic.
    2. Create single wp-crone task which create messages and send it.

    Positive sides of this solution: Emails sends per user (not 1 email per 100000 users like in bbPress by default). Site work faster because emails send via cron, not immediately. I dont store emails or messages to send in DB, just topic ID. Emails sends via SMTP server (not just PHP Mailer or wp_mail()).

    <?php
    function selena_network_bbp_init () {
    	/*
    	 * Оповещание о новых ответах в темах на форуме
    	 * 1. Удаляем дефолтную оповещалку.
    	 * 2. Добавляем свою оповещалку, которая создает крон-задачу.
    	 * 3. Экшн фильтр задачи, который создает письма и отправляет их.
    	 *
    	 * Функции находятся в файле
    	 * bbpress/includes/common/functions.php
    	 */
    	remove_action ('bbp_new_reply', 'bbp_notify_subscribers', 11);
    	add_action ('bbp_new_reply', 'selena_network_bbp_notify_subscribers', 11, 5);
    	add_action ('selena_network_bbp_notify_subscribers_cron', 'selena_network_bbp_notify_subscribers_callback', 10, 5);
    
    	/*
    	 * Оповещения о создании новых тем на форуме
    	 * 1. Удаляем дефолтную оповещалку.
    	 * 2. Добавляем свою оповещалку, которая создает крон-задачу.
    	 * 3. Экшн для задачи, который будет запускать крон (создает письма и отправляет их).
    	 *
    	 * Функции находятся в файле
    	 * bbpress/includes/common/functions.php
    	 */
    	remove_action ('bbp_new_topic', 'bbp_notify_forum_subscribers', 11);
    	add_action ('bbp_new_topic', 'selena_network_bbp_notify_forum_subscribers', 11, 4);
    	add_action ('selena_network_bbp_notify_forum_subscribers_cron', 'selena_network_bbp_notify_forum_subscribers_callback', 10, 4);
    }
    add_action ('bbp_init', 'selena_network_bbp_init', 99);
    <?php
    function selena_network_bbp_notify_subscribers (
    	$reply_id = 0,
    	$topic_id = 0,
    	$forum_id = 0,
    	$anonymous_data = false,
    	$reply_author = 0
    ) {
    	wp_schedule_single_event (
    		time (),
    		'selena_network_bbp_notify_subscribers_cron',
    		array (
    			$reply_id,
    			$topic_id,
    			$forum_id,
    			$anonymous_data,
    			$reply_author
    		)
    	);
    
    	return true;
    }
    
    function selena_network_bbp_notify_subscribers_callback (
    	$reply_id,
    	$topic_id,
    	$forum_id,
    	$anonymous_data,
    	$reply_author
    ) {
    	// Bail if subscriptions are turned off
    	if ( !bbp_is_subscriptions_active() ) {
    		return false;
    	}
    
    	$reply_id = bbp_get_reply_id( $reply_id );
    	$topic_id = bbp_get_topic_id( $topic_id );
    	$forum_id = bbp_get_forum_id( $forum_id );
    
    	// Bail if topic is not published
    	if ( !bbp_is_topic_published( $topic_id ) ) {
    		return false;
    	}
    
    	// Bail if reply is not published
    	if ( !bbp_is_reply_published( $reply_id ) ) {
    		return false;
    	}
    
    	// User Subscribers
    	$user_ids = bbp_get_topic_subscribers( $topic_id, true );
    	if ( empty( $user_ids ) ) {
    		return false;
    	}
    
    	// Poster name
    	$reply_author_name = bbp_get_reply_author_display_name( $reply_id );
    
    	// Remove filters from reply content and topic title to prevent content
    	// from being encoded with HTML entities, wrapped in paragraph tags, etc...
    	remove_all_filters( 'bbp_get_reply_content' );
    	remove_all_filters( 'bbp_get_topic_title'   );
    
    	// Strip tags from text and setup mail data
    	$topic_title   = strip_tags( bbp_get_topic_title( $topic_id ) );
    	$reply_url     = bbp_get_reply_url( $reply_id );
    
    	$subject = sprintf(
    		_x (
    			'%1$s wrote new comment in “%2$s”',
    			'%1$s = Name of user who create comment
    %2$s = Topic title in which user leave a comment',
    			'selena_network'),
    		$reply_author_name,
    		$topic_title
    	);
    	if (empty ($subject)) {
    		return;
    	}
    
    	// Loop through users
    	foreach ( (array) $user_ids as $user_id ) {
    
    		// Don't send notifications to the person who made the post
    		if ( !empty( $reply_author ) && (int) $user_id === (int) $reply_author ) {
    			continue;
    		}
    
    		// For plugins to filter messages per reply/topic/user
    		$message = sprintf (
    			_x (
    '%1$s wrote new comment in conversation “%2$s” %3$s.
    
    +
    Notification settings → %4$s.
    Send email at %5$s or respond on that letter if you have a questions.',
    			'%1$s = Reply author name
    %2$s = Topic title
    %3$s = Reply URL to this topic
    %4$s = Manage subscribe settings link
    %5$s = Admin email',
    			'selena_network'
    			),
    
    			$reply_author_name,
    			$topic_title,
    			$reply_url,
    			bbp_get_subscriptions_permalink ($user_id),
    			get_option ('admin_email')
    		);
    
    		if ( empty( $message ) ) {
    			continue;
    		}
    
    		wp_mail (
    			get_userdata( $user_id )->user_email,
    			$subject,
    			$message
    		);
    	}
    
    	return true;
    }
    
    /* Новая тема на форуме */
    function selena_network_bbp_notify_forum_subscribers (
    	$topic_id = 0,
    	$forum_id = 0,
    	$anonymous_data = false,
    	$topic_author = 0
    ) {
    	wp_schedule_single_event (
    		time (),
    		'selena_network_bbp_notify_forum_subscribers_cron',
    		array (
    			$topic_id,
    			$forum_id,
    			$anonymous_data,
    			$topic_author
    		)
    	);
    
    	return true;
    }
    function selena_network_bbp_notify_forum_subscribers_callback (
    	$topic_id,
    	$forum_id,
    	$anonymous_data,
    	$topic_author
    ) {
    	// Bail if subscriptions are turned off
    	if ( !bbp_is_subscriptions_active() ) {
    		return false;
    	}
    
    	$topic_id = bbp_get_topic_id( $topic_id );
    	$forum_id = bbp_get_forum_id( $forum_id );
    
    	// Get topic subscribers and bail if empty
    	$user_ids = bbp_get_forum_subscribers( $forum_id, true );
    	if ( empty( $user_ids ) ) {
    		return false;
    	}
    
    	// Bail if topic is not published
    	if ( ! bbp_is_topic_published( $topic_id ) ) {
    		return false;
    	}
    
    	// Poster name
    	$topic_author_name = bbp_get_topic_author_display_name( $topic_id );
    
    	// Remove filters from reply content and topic title to prevent content
    	// from being encoded with HTML entities, wrapped in paragraph tags, etc...
    	remove_all_filters( 'bbp_get_topic_content' );
    	remove_all_filters( 'bbp_get_topic_title'   );
    
    	// Strip tags from text and setup mail data
    	$topic_title   = strip_tags( bbp_get_topic_title( $topic_id ) );
    	$forum_title   = strip_tags( bbp_get_forum_title( $forum_id ) );
    	$topic_url     = get_permalink( $topic_id );
    
    	// For plugins to filter titles per reply/topic/user
    	//$subject = apply_filters( 'bbp_forum_subscription_mail_title', '[' . $blog_name . '] ' . $topic_title, $topic_id, $forum_id, $user_id );
    	$subject = sprintf (
    		_x (
    			'%1$s created new conversation “%2$s”',
    			'%1$s = Topic author name who create new new topic
    %2$s = Topic title',
    			'selena_network'
    		),
    		$topic_author_name,
    		$topic_title
    	);
    	if ( empty( $subject ) ) {
    		return;
    	}
    
    	// Loop through users
    	foreach ( (array) $user_ids as $user_id ) {
    
    		// Don't send notifications to the person who made the post
    		if ( !empty( $topic_author ) && (int) $user_id === (int) $topic_author ) {
    			continue;
    		}
    
    		// For plugins to filter messages per reply/topic/user
    		$message = sprintf( _x( '%1$s created new conversation “%2$s” in the forum “%3$s” %4$s.
    
    +
    Notification settings → %5$s.
    Send email at %6$s or respond on that letter if you have a questions.',
    			'%1$s = Topic author name
    %2$s = Created topic title
    %3$s = Forum title in wich topic created
    %4$s = Link to brand new topic
    %5$s = URL to manage user subscribtions
    %6$s = Admin email',
    			'selena_network' ),
    
    			$topic_author_name, // 1
    			$topic_title, // 2
    			$forum_title, // 3
    			$topic_url, // 4
    			bbp_get_subscriptions_permalink ($user_id), // 5
    			get_option ('admin_email') // 6
    		);
    
    		if ( empty( $message ) ) {
    			continue;
    		}
    
    		// Send notification email
    		wp_mail (
    			get_userdata( $user_id )->user_email,
    			$subject,
    			$message
    		);
    	}
    
    	return true;
    }
    ?>

    t3_rrY
    Participant

    @t3_rry

    Ok thanks very much for your reply and sharing your code; any idea on the use of HTML template for BBp notification?


    Kolya Korobochkin
    Participant

    @korobochkin

    You just need some functions or methods from Mandrill API instead of my templates 🙂 wp_mail also can send rich text emails (html emails). So you just need pass your html email template to wp_mail for example.

    Also wp_mail by default sends email via PHP Mailer (not SMTP server). I recommend send emails through SMTP server – for protect from spam folder and SMTP server can add dkim record to emails. I dont know anything about Mandrill – maybe your plugin already change wp_mail for SMTP, maybe not. I use https://wordpress.org/plugins/wp-mail-smtp/ plugin for sending emails via SMTP.


    t3_rrY
    Participant

    @t3_rry

    Thanks very much Kolya Korobochkin, Ok I’ll continue my search. My plugin already uses SMTP server and is supposed returning HTML emails (already the case with my new subscription email HTML template) 😉


    andreippo
    Participant

    @andreippo

    @korobochkin, thanks a lot for sharing this.

    Should I just paste you code on bbpress/includes/common/functions.php and change the attributes of it?

    What does selena_network refer to?

    Thanks a lot!


    Kolya Korobochkin
    Participant

    @korobochkin

    @andreippo Do not change the plugin files (bbPress). Use MU plugins or write your custom plugin. I use MU. If you write custom plugin make sure what all hooks connects in the right sequence.

    Do not change the attributes passed to functions (they are correct). You can change logic inside functions and change the headers, title, text of outgoing emails.

    selena_network_ is my multisite prefix used in all functions at armyofselenagomez.com and selenaselena.ru.


    tharsheblows
    Participant

    @tharsheblows

    @t3_rrY — I think you’d use the mandrill_payload filter as explained here: http://blog.mandrill.com/own-your-wordpress-email-with-mandrill.html

    You could add it into @korobochkin’s code. I haven’t tried it – I’m not sure how the template is handled (I don’t use them) but that’s where I start. You can also simply add html to the message in the functions above, although for anything the least bit complicated design-wise, a template would be the way to go.

    I’ve just started using Mandrill; it’s great, isn’t it?


    Kolya Korobochkin
    Participant

    @korobochkin

    bbPress 2.5.6 have some changes with this. Filter bbp_notify_subscribers renamed to bbp_notify_topic_subscribers.


    Stagger Lee
    Participant

    @stagger-lee

    This is for custom info block above comment form:

    function ntwb_custom_topic_form_notice() {
    	?>
    	<div class="bbp-template-notice">
    		<p>Something here...</p>
    		<p>Something else...</p>
    	</div>
    	<?php
    
    }
    add_action( 'bbp_theme_before_topic_form_notices', 'ntwb_custom_topic_form_notice' );

    Robkk
    Moderator

    @robkk

    @stagger-lee

    this is for the subscription emails sent to users when they subscribe to topics/forums

    not the little notice text above a topic


    Stagger Lee
    Participant

    @stagger-lee

    Sorry, my mistake.
    Just tested and noticed it is only for topic form, not reply form.

Viewing 12 replies - 1 through 12 (of 12 total)
  • You must be logged in to reply to this topic.
Skip to toolbar