Notifications of new posts in Buddypress menu

Hello,

is it possible for Buddypress Users to see a notification at their profile menu bar every time a new post is published?

Thank you!!

  • Predrag Dubajic

    Hey Ioannis,

    Hope you're doing well today :slight_smile:

    I'm not familiar with such plugin or function so I tried finding one for you but didn't had much luck with it :slight_frown:

    I did stumble upon this solution that shows number of posts in admin sidebar so perhaps you can use that to create a custom solution for your request:
    http://stackoverflow.com/questions/8625674/how-can-i-add-notification-bubble-to-wordpress-admin-menu

    Best regards,
    Predrag

  • Dimitris

    Hello Ioannis,

    I hope that you're doing really good and don't mind skipping in! :slight_smile:

    I also cannot provide a working solution like a plugin or custom snippet, but I'd like to give some things to consider, and maybe you should be able to make it work with some coding. :slight_smile:

    The catch here is that you can extend the BP Notifications using a function already in place: bp_notifications_add_notification.

    This is used by BBPress plugin as can be seen here https://bbpress.trac.wordpress.org/browser/tags/2.5.3/includes/extend/buddypress/notifications.php
    You can inspect this code and create some new notifications whenever a post is published or updated. :wink:

    Have a good one,
    Dimitris

  • Ioannis

    Hello Dimitris,

    thank you so so much for your support but I am not a developer, so it is very difficult to me to understand php files or combine them in the right way. I have found this code

    <?php
    // this is to add a fake component to BuddyPress. A registered component is needed to add notifications
    function custom_filter_notifications_get_registered_components( $component_names = array() ) {
    	// Force $component_names to be an array
    	if ( ! is_array( $component_names ) ) {
    		$component_names = array();
    	}
    	// Add 'custom' component to registered components array
    	array_push( $component_names, 'custom' );
    	// Return component's with 'custom' appended
    	return $component_names;
    }
    add_filter( 'bp_notifications_get_registered_components', 'custom_filter_notifications_get_registered_components' );
    // this gets the saved item id, compiles some data and then displays the notification
    function custom_format_buddypress_notifications( $action, $item_id, $secondary_item_id, $total_items, $format = 'string' ) {
    	// New custom notifications
    	if ( 'custom_action' === $action ) {
    
    		$comment = get_comment( $item_id );
    
    		$custom_title = $comment->comment_author . ' commented on the post ' . get_the_title( $comment->comment_post_ID );
    		$custom_link  = get_comment_link( $comment );
    		$custom_text = $comment->comment_author . ' commented on your post ' . get_the_title( $comment->comment_post_ID );
    		// WordPress Toolbar
    		if ( 'string' === $format ) {
    			$return = apply_filters( 'custom_filter', '<a href="' . esc_url( $custom_link ) . '" title="' . esc_attr( $custom_title ) . '">' . esc_html( $custom_text ) . '</a>', $custom_text, $custom_link );
    		// Deprecated BuddyBar
    		} else {
    			$return = apply_filters( 'custom_filter', array(
    				'text' => $custom_text,
    				'link' => $custom_link
    			), $custom_link, (int) $total_items, $custom_text, $custom_title );
    		}
    
    		return $return;
    
    	}
    
    }
    add_filter( 'bp_notifications_get_notifications_for_user', 'custom_format_buddypress_notifications', 10, 5 );
    // this hooks to comment creation and saves the comment id
    function bp_custom_add_notification( $comment_id, $comment_object ) {
    	$post = get_post( $comment_object->comment_post_ID );
    	$author_id = $post->post_author;
    	bp_notifications_add_notification( array(
    		'user_id'           => $author_id,
    		'item_id'           => $comment_id,
    		'component_name'    => 'custom',
    		'component_action'  => 'custom_action',
    		'date_notified'     => bp_core_current_time(),
    		'is_new'            => 1,
    	) );
    
    }
    add_action( 'wp_insert_comment', 'bp_custom_add_notification', 99, 2 );

    Do you think that there is an easy way to modify it and make it work for blog posts?

    Thank you a lot,
    Ioannis

  • Predrag Dubajic

    Hi Ioannis,

    Changing the above code should be possible but I couldn't get the original code to work so I'm not sure how to test this.
    Where did you get the above code from?

    This could be bit complex to develop so perhaps you should seek for dev help to create a custom solution for you.
    You can post a job in our job board where you can hire a developer to assist further:
    http://premium.wpmudev.org/wpmu-jobs/
    Please note that, no WPMU official staff members are allowed to work in the job board.

    Best regards,
    Predrag

  • Dimitris

    Hey there Ioannis,

    hope you're doing good today! :slight_smile:

    I managed to make some changes in the code you shared and make this work! :slight_smile:

    <?php
    // this is to add a fake component to BuddyPress. A registered component is needed to add notifications
    function custom_filter_notifications_get_registered_components( $component_names = array() ) {
    
    	// Force $component_names to be an array
    	if ( ! is_array( $component_names ) ) {
    		$component_names = array();
    	}
    
    	// Add 'custom' component to registered components array
    	array_push( $component_names, 'custom' );
    
    	// Return component's with 'custom' appended
    	return $component_names;
    }
    add_filter( 'bp_notifications_get_registered_components', 'custom_filter_notifications_get_registered_components' );
    
    // this gets the saved item id, compiles some data and then displays the notification
    function custom_format_buddypress_notifications( $action, $item_id, $secondary_item_id, $total_items, $format = 'string' ) {
    
    	// New custom notifications
    	if ( 'custom_action' === $action ) {
    
    		$title = get_the_title( $item_id );
    		$link  = get_the_permalink( $item_id );
    		$text = 'New blog post published: '.$title;
    
    		// WordPress Toolbar
    		if ( 'string' === $format ) {
    			$return = apply_filters( 'custom_filter', '<a href="' . esc_url( $link ) . '" title="' . esc_attr( $title ) . '">' . esc_html( $text ) . '</a>', $title, $link );
    
    		// Deprecated BuddyBar
    		} else {
    			$return = apply_filters( 'custom_filter', array(
    				'text' => $text,
    				'link' => $link
    			), $link, (int) $total_items, $title, $title );
    		}
    
    		return $return;
    
    	}
    
    }
    add_filter( 'bp_notifications_get_notifications_for_user', 'custom_format_buddypress_notifications', 10, 5 );
    
    // this hooks to comment creation and saves the comment id
    function bp_custom_add_notification( $post_id ) {
    
    	$post = get_post( $post_id );
    	$author_id = $post->post_author;
    
    	bp_notifications_add_notification( array(
    		'user_id'           => $author_id,
    		'item_id'           => $post_id,
    		'component_name'    => 'custom',
    		'component_action'  => 'custom_action',
    		'date_notified'     => bp_core_current_time(),
    		'is_new'            => 1,
    	) );
    
    }
    add_action( 'save_post', 'bp_custom_add_notification', 99, 2 );

    Feel free to use this in a MU-plugin and whenever a post is saved (created or updated), users will get notifications in adminbar and profile page (see screenshot for the "New blog post published")

    Warm regards,
    Dimitris

  • Ioannis

    Hello Dimitris,

    I hope you're doing really good today!:relaxed:

    Thank you so much about your help but unfortunately the code does not work for my website. If I add the code as a mu-plugin my admin dashboard dissapears and there is only a blank page. I tried also adding the code as a code snippet but when I added a new post no notification appeared in Buddypress Menu. I have enabled Grant Support Access in case you need to enter my account to check this out.

    Thank you a lot,
    Ioannis

  • Adam Czajczyk

    Hello Ioannis!

    I checked the code and unfortunately it's only setting/fetching notifications for specific user. For example, when the post is published it saves the "author id" as the "receiver ID" and as a result this notification will only be shown to the... post author.

    That's why no other members see those.

    I tried to find a legitimate way to overwrite this but it seems that the notification system of BuddyPress works that way - it's saving notifications directed towards a specific user. There's not much clear documentation on this and it only states that the default value of that "receiver id" is optional and by default is set to "0". However, it doesn't "broadcast" notifications to all users.

    I can think of two workarounds:

    - one would be to find the way to "read" notifications with a "receiver_id" of "0"
    - another would be to set multiple notifications at once (for all users).

    The latter one doesn't seems to be extremely resourceful though so I don't think it's the way to go. I tried to follow the first route but so far with no success as I couldn't force BP to read those specific notifications.

    The bottom line is: the code seems good but I think it will need a different and fully custom approach to broadcast messages to all members. This is however beyond the scope of this forum so you may want to hire a developer e.g. via our "Jobs & Pros" job board (please note: no WPMU DEV staff involved!) here:

    https://premium.wpmudev.org/wordpress-development/

    Best regards,
    Adam

  • Adam Czajczyk

    Hello Ioannis!

    That's a nice shot! I haven't thought of this first.

    There's indeed an option for admin to send a site-wide notice and it can be used to send "new post notifications" to all users. The downside is that it will not appear as apart of toolbar but rather as a "box" on top of the page (visible on front-side only). I think however this may be adjusted using some CSS.

    Check this code please:

    <?php
    function wpmu_bp_notice_new_post($post_id) {
    	$post = get_post($post_id);
    	$author_id = $post->post_author;
    	$notice_subject = "New post has arrived on " . $post->post_date;
    	$notice_msg = '<a href="' . get_permalink($post) . '">' . $post->post_title . '</a>';
    	$result = messages_send_notice($notice_subject,$notice_msg);
    }
    add_action( 'save_post', 'wpmu_bp_notice_new_post',99,2);

    Remove the entire code you used before and add this to the site as MU plugin:

    - create an empty file with ".php" extension
    - put that code inside
    - upload to the "/wp-content/mu-plugins" folder

    I tested it on my site and it works quite nice. Whenever a new post is published a notice box appears on front end for each BP member.

    Let me know if this works and if so we could then work on further "fine-tuning".

    Best regards,
    Adam

  • Adam Czajczyk

    Hello Ioannis!

    Yes, this is possible. Please replace the code I recently gave you with following one:

    <?php
    function wpmu_bp_notice_new_post($ID, $post) {
    	$author_id = $post->post_author;
    	$notice_subject = "New post has arrived on " . $post->post_date;
    	$notice_msg = '<a href="' . get_permalink($ID) . '">' . $post->post_title . '</a>';
    	$result = messages_send_notice($notice_subject,$notice_msg);
    }
    add_action( 'publish_post', 'wpmu_bp_notice_new_post',99,2);

    This will work the same way except it will only add notice if a new post is published or a draft is updated and its status has been changed to "published".

    I hope that helps!
    Best regards,
    Adam

  • Adam Czajczyk

    Hello Ioannis!

    Here's an updated code, it should do the trick:

    <?php
    function wpmu_bp_notice_new_post($ID, $post) {
    	$post_type = $post->post_type;
    	if ($post_type == 'post') {
    		$author_id = $post->post_author;
    		$author_name = get_userdata($author_id)->display_name;
    		$notice_subject = "New post by " . $author_name . " has arrived";
    		$notice_msg = '<a href="' . get_permalink($ID) . '">' . $post->post_title . '</a>';
    		$result = messages_send_notice($notice_subject,$notice_msg);
    	}
    }
    add_action( 'publish_post', 'wpmu_bp_notice_new_post',99,2);

    Best regards,
    Adam

Thank NAME, for their help.

Let NAME know exactly why they deserved these points.

Gift a custom amount of points.