Halloween Sale 15% off Ends in See the plans
PHP

bbPress: Email the Admin About New Topics

Sends the site admin (or a moderator address) an email with the title, author and link whenever a new forum topic is posted.

Don’t have WPIDE yet? Install it on your site

Code

if ( ! function_exists( 'bbp_get_topic_permalink' ) ) {
    return;
}

$wpide_bbp_notify_to     = get_option( 'admin_email' );
$wpide_bbp_notify_forums = array();

add_action( 'bbp_new_topic', function ( $topic_id, $forum_id ) use ( $wpide_bbp_notify_to, $wpide_bbp_notify_forums ) {
    if ( $wpide_bbp_notify_forums && ! in_array( (int) $forum_id, $wpide_bbp_notify_forums, true ) ) {
        return;
    }

    wp_mail(
        $wpide_bbp_notify_to,
        sprintf( 'New topic in %s: %s', bbp_get_forum_title( $forum_id ), bbp_get_topic_title( $topic_id ) ),
        sprintf(
            "%s started a new topic.\n\n%s\n\n%s",
            bbp_get_topic_author_display_name( $topic_id ),
            wp_trim_words( wp_strip_all_tags( bbp_get_topic_content( $topic_id ) ), 60 ),
            bbp_get_topic_permalink( $topic_id )
        )
    );
}, 10, 2 );

Notes

Limit it to some forums with $wpide_bbp_notify_forums; leave empty for all forums.

How to add this snippet

  1. Install WPIDE on your WordPress site (it is free) and open Code Snippets.
  2. Use snippet above adds it from the library in one click. Or copy the code into a new snippet with the same code type.
  3. It is added inactive: review the code and settings, then switch it on.

Have a snippet worth sharing?

Publish it to the library from WPIDE or right here. Sign in with any WordPress site that runs WPIDE, free or Pro. Public snippets are reviewed by the WPIDE team before they appear.

Which site should get this snippet?

Enter the address of your WordPress site. WPIDE opens there in a new tab with the snippet ready to review and save.

Remembered in this browser only, so you are asked once.

Report this snippet

Tell the WPIDE team what is wrong: broken code, something harmful, spam or copied work.