Skip to:
Content
Pages
Categories
Search
Top
Bottom

How to modify default BuddyPress mention notification content?


  • cahara
    Participant

    @cahara

    Hey all I’m attempting to modify the content returned by BuddyPress’ default ‘@’ mention notification. I’m using this as part of a custom profile page and would like to modify the “username mentioned you’ text to something more personalized based on post type.

    
    // Change mention output
    function format_mention_notifications( $action, $item_id, $secondary_item_id, $total_items, $format = 'string', $component_action, $component_name, $notification_id) {
        if ( $component_action === 'new_at_mention') { // User mentioned notification
    
            $mentioned_source_type = get_post_type($item_id);
            $username = wl_get_username($secondary_item_id);
            $mention_avatar = wl_get_avatar($secondary_item_id);
            $mention_title = '';
            $mention_text = $username;
    
            if ($mentioned_source_type == 'topic') {
                $mention_title = bbp_get_topic_title( $item_id );
                $mention_text .= ' mentioned you in the forum post ';
                $mention_source_link = ''; // link to topic
            } else if ($mentioned_source_type == 'reply') {
                $parent_topic_id = bbp_get_reply_topic_id( $item_id );
                $mention_title = bbp_get_topic_title( $parent_topic_id );
                $mention_text .= ' mentioned you in their response to the forum post ';
                $mention_source_link = bbp_get_reply_url( $item_id ); // link to reply
            } else if ($mentioned_source_type == 'comment') {
                $comment = get_comment($item_id);
                $mention_text .= ' mentioned you in their comment';
                $mention_source_link = get_comment_link($comment);
    
            } else {
                $mention_avatar = '';
                $mention_title = get_the_title($item_id);
                $mention_text = 'You were mentioned in ';
                $mention_source_link = get_permalink($item_id);
            }
    
            $mention_source_link = wp_nonce_url( add_query_arg( array( 'action' => 'read', 'notification_id' => $notification_id), $mention_source_link ), 'bp_notification_mark_read_' . $notification_id);
    
            // WordPress Toolbar
            if ( 'string' === $format ) {
                $return = apply_filters( 'bp_activity_single_at_mentions_notification', '<a href="' . esc_url( $mention_source_link ) . '" title="New mention">'. $mention_avatar . esc_html( $mention_text ) . '<span class="activity-title">'.$mention_title.'</span></a>', (int) $total_items, $mention_text, $mention_source_link );
    
            // Deprecated BuddyBar
            } else {
                $return = apply_filters( 'bp_activity_single_at_mentions_notification', array(
                    'text' => $mention_text . $mention_title,
                    'link' => $mention_source_link
                ), $mention_source_link, (int) $total_items, $mention_text, $mention_title );
            }
        }
    
        return $return;
    
    }
    add_filter( 'bp_notifications_get_notifications_for_user', 'format_mention_notifications', 100, 8 );
    

    What I’ve tried so far:
    – Changing the priority of the filter
    – Moving my logic into the activity_format_notifications action with various different priorities
    – All of the above in a custom plugin file, bp-custom.php, and my themes’ function.php file

    I’ve used a similar filter for custom notifications and that aspect works great. I just can’t seem to override the default content with my own. My thoughts are that the the filters/actions run too late to be noticed by BuddyPress.

    Many thanks for your help.

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