Skip to:
Content
Pages
Categories
Search
Top
Bottom

Personalized notification when publishing a type of post


  • Pynet
    Participant

    @pynet

    Hello,
    I am trying to create my own notification, but it is not working.
    The notification should not be for the author of the post, but for whoever is assigned as client “_sliced_client”, which is stored in the postmeta table in the database and returns the client id.
    Wordpress version: 6.1.1
    BuddyPress version: 10.6.0

    <?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, 'propre' );
        // 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 ( 'propre_action' === $action ) {
    
            $post = get_post( $item_id );
    		//
            $custom_title = $post->post_author . ' add a quote for you' . get_the_title( $item_id );
            $custom_link  = get_permalink( $post );
            $custom_text = $post->post_author . ' add a quote for you ' . get_the_title( $item_id );
            // WordPress Toolbar
            if ( 'string' === $format ) {
                $return = apply_filters( 'propre_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( 'propre_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 post creation and saves the post id
    function bp_custom_add_notification( $post_id, $post ) {
        if ( $post->post_type == 'sliced_quote' ) {
            $post = get_post( $post_id );
    		$client_id= get_post_meta($post_id, '_sliced_client');
            $author_id = $post->post_author;
            bp_notifications_add_notification( array(
                'user_id'           => $client_id,
                'item_id'           => $post_id,
                'component_name'    => 'propre',
                'component_action'  => 'propre_action',
                'date_notified'     => bp_core_current_time(),
                'is_new'            => 1,
            ) );
        }   
    }
    add_action( 'wp_insert_post', 'bp_custom_add_notification', 99, 2 );

    Any ideas?
    Thank you very much!
    Regards and happy new year!

Viewing 2 replies - 1 through 2 (of 2 total)

  • Pynet
    Participant

    @pynet

    No one?


    Pynet
    Participant

    @pynet

    Ok, I fixed it, there was a function that returned an array instead of a string.
    In case anyone is interested:
    $client_id= get_post_meta($post_id, ‘_sliced_client’, true);
    Regards

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