BuddyDev

Search

Show notification when post is published

  • Participant
    Level: Initiated
    Posts: 8
    Hayleigh Thornhill on #20639

    Hi,

    We are currently using buddypress and i am trying to generate a notification when a post is published, i have got it to the point that the indicator is showing a notification as unread but when i view the notification there is no text/link

    The code i have used is :-

    // 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, 'publishpost' );
        // Return component's with 'custom' appended
        return $component_names;
    }
    add_filter( 'bp_notifications_get_registered_components', 'custom_filter_notifications_get_registered_components' );
    
    // this hooks to post creation and saves the post id
    function bp_custom_add_notification( $post_id, $post ) {
            $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'    => 'publishpost',
                'component_action'  => 'publishpost_action',
                'date_notified'     => bp_core_current_time(),
                'is_new'            => 1,
            ) );   
    }
    add_action( 'publish_post', 'bp_custom_add_notification', 99, 2 );
    
    function custom_format_buddypress_notifications( $action, $item_id, $secondary_item_id, $total_items, $format = 'string' ) 
    {
    // New custom notifications
        if ( 'publishpost_action' === $action ) {
    
            $post = get_post( $item_id );
    
            $custom_title = $post->post_author . ' published a new post ' . get_the_title( $item_id );
            $custom_link  = get_permalink( $post );
            $custom_text = $post->post_author . ' published a new post ' . get_the_title( $item_id );
            // WordPress Toolbar
            if ( 'string' === $format ) {
                $return = apply_filters( 'publishpost_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( 'publishpost_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 );

    if anyone could help me resolve why there is no message showing it would be much appreciated

    Thanks

  • Keymaster
    (BuddyDev Team)
    Posts: 24211
    Brajesh Singh on #20650

    Hi Hayleigh,
    Thank you for asking.
    Most probably you are putting the code in your theme’s functions.php or similar?

    For me, your code did work. I have put it in wp-content/plugins/bp-custom.php

    Regards
    Brajesh

  • Participant
    Level: Initiated
    Posts: 8
    Hayleigh Thornhill on #20692

    Hi Brajesh,

    Thanks for your reply, i have now moved the above code to wp-content/plugins/bp-custom.php and removed from the functions.php file of my theme. Unfortunately when i publish a post it generates the notification but the notification description is still blank. Is there something else missing or have i not coded the description properly?

    thanks

  • Keymaster
    (BuddyDev Team)
    Posts: 24211
    Brajesh Singh on #20696

    Hi Hayleigh,
    That’s strange. I am not sure what might be causing it.

    Here is what I see
    https://i.imgur.com/1KFBLz0.png

    Regards
    Brajesh

  • Participant
    Level: Initiated
    Posts: 8
    Hayleigh Thornhill on #20699

    Hi,

    This is what my back end database shows

    https://www.dropbox.com/s/0lvwzti5opwmemw/DBNotofocations.PNG?dl=0

    its almost like it doesn’t know what text to use to match the publishpost_action, am i supposed to have put this in the database somewhere?

    could it be this bit isn’t working?
    if ( 'publishpost_action' === $action ) {

    Thanks

  • Keymaster
    (BuddyDev Team)
    Posts: 24211
    Brajesh Singh on #20706

    Hi Hayleigh,
    The screenshot I shared was from the notifications created by your code and as you may see, It was working.

    My suggestion will be to try your code on a clean install of BuddyPress(with default theme) and see if it works. If it does, you can troubleshoot your setup by changing theme and deactivating plugins.

    Regards
    Brajesh

  • Participant
    Level: Initiated
    Posts: 8
    Hayleigh Thornhill on #20709

    Hi,

    I have just tried it on a new wordpress install using the twenty nineteen theme with only the BuddyPress and bbpress plugins activated and it is still showing as no text, i really need to get this working and cant see what is going on

    https://www.dropbox.com/s/qhbqxzc8hd6o0dr/Capture.PNG?dl=0

    Thanks

  • Participant
    Level: Initiated
    Posts: 8
    Hayleigh Thornhill on #20710

    how does it know what text to show for the notification?

  • Keymaster
    (BuddyDev Team)
    Posts: 24211
    Brajesh Singh on #20711

    The followig line in your code

    
    		// WordPress Toolbar
    		if ( 'string' === $format ) {
    			$return = apply_filters( 'publishpost_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( 'publishpost_filter', array(
    				'text' => $custom_text,
    				'link' => $custom_link
    			), $custom_link, (int) $total_items, $custom_text, $custom_title );
    		}
    

    Tells BuddyPress what to show.

  • Participant
    Level: Initiated
    Posts: 8
    Hayleigh Thornhill on #20714
    This reply has been marked as private.

You must be logged in to reply to this topic.

This topic is: not resolved