I'm new to PHP and I'm currently working with BuddyPress and WordPress. I managed to create a custom notification everytime I create a new post. The problem is that I want to change it to a Custom Post Type I created.
I tried to define my $item_id
with this code:
$item_id = array(
'post_type' => 'my_custom_post_type'
);
But unfortunately, it did not work.
Below is my actual code:
function custom_filter_notifications_publish_post_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_publish_post_get_registered_components' );
function bp_custom_format_buddypress_notifications( $action, $item_id, $secondary_item_id, $total_items, $format = 'string' ) {
$item_id =
// New custom notifications
if ( 'custom_action' === $action ) {
$post_info = get_post( $item_id );
$custom_title = get_the_author_meta( 'display_name', $post_info->post_author ) . ' a publié un nouveau parcours ' . get_the_title( $item_id );
$custom_link = get_post_permalink( $item_id );
$custom_text = get_the_author_meta( 'display_name', $post_info->post_author ) . ' a publié un nouveau parcours ' . get_the_title( $item_id );
// WordPress Toolbar
if ( 'string' === $format ) {
$return = apply_filters( 'custom_filter', '' . esc_html( $custom_text ) . '', $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', 'bp_custom_format_buddypress_notifications', 10, 5 );
function bp_post_published_notification( $post_id, $post ) {
$author_id = $post->post_author; /* Post author ID. */
$users = get_users();
$author_id = $post->post_author; /* Post author ID. */
foreach( $users as $user ) {
if ( bp_is_active( 'notifications' ) ) {
bp_notifications_add_notification( array(
'user_id' => $user->ID,
'item_id' => $post_id,
'component_name' => 'custom',
'component_action' => 'custom_action',
'date_notified' => bp_core_current_time(),
'is_new' => 1,
'allow_duplicate' => false,
) );
}
}
}
add_action( 'publish_post', 'bp_post_published_notification', 99, 2 );
At the moment, this works for the CPT post
but I could not do it with a custom post type. Could you please help me?
Many thanks,
Vincent