[Buddy Press] Custom Buddy Press Notification - Notification to all site users on Custom Post Type

Hello there!

I am looking to add a custom notification within buddy press that is created every time I publish a specific custom post type. Would you guys be able to assist with this out of curiosity?

Thank you in advance for your incredible assistance, I truly appreciate your time and helping point me in the right direction :slight_smile:

  • viobru
    • Recruit

    Hello, Jacob!

    Thanks for contacting us, hope you are doing great :slight_smile:

    I’ve been checking this and, after a lot of research, I’ve found that a member asked for an option to notify all users when a new post was published and there was some code provided in order to achieve this.

    I modified the code a bit to get notifications for a new CPT instead of new posts and it worked for me on my testing site. Here is an example of how the notification is displayed:

    To achieve this you’ll need to create a mu-plugin and add this code on it (you need to replace ‘yourcustomposttype’ with your own CPT:

    function wpmu_bp_notice_new_post($ID, $post) {
    	$post_type = $post->post_type;
    	if ($post_type == 'yourcustomposttype') {
    		$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_yourcustomposttype', 'wpmu_bp_notice_new_post',99,2);

    Please note that, in order to make it work, it is necessary to have the BP Private Messaging Component active on the site (it can be activated from Settings > BuddyPress > Components).

    Hope this helps! :slight_smile:

    Kind regards,

  • Jacob
    • New Recruit

    Hello Viobru!

    Thank you so much for taking the time to do research as well as your prompt response :slight_smile:

    I was in fact able to send a notification that appeared across the heading of my site upon the creation of my custom post type which is FANTASTIC!

    Where I am still running into a bit of trouble is in regards to the notification not populating within the "read" or "unread" notifications element within Buddypress itself (see screenshots attached):

    Please let me know if I can provide any additional clarification. I cannot thank you enough for your assistance and truly appreciate the time you have invested into assisting me thus far.

  • viobru
    • Recruit

    Hi, Jacob!

    Hope you are doing fine :slight_smile:

    Unfortunately, the downside of the code I provided is that it can only be used to display the notification box on the Profile page.

    As I mentioned before, the code I suggested was an adaptation from the one that my teammate Adam first shared on another ticket and it can be used to display the box on the profile, but will not make the notifications appear as apart of the toolbar, as he explained on it.

    Getting this done would require further customization (not actually sure if this could be achieved using the code I shared or if it would require whole a new code, though), so I suggest that you look for a developer to continue assisting you with this. You can check our partners from Codeable and also get a discount on the first service with them using the discount from this page: https://premium.wpmudev.org/partners/

    Kind regards,

Thank NAME, for their help.

Let NAME know exactly why they deserved these points.

Gift a custom amount of points.