Как в buddypress добавить свой notifications?
Здравствуйте, скажите как добавить notifications в плагине buddypress. Я хотел бы добавлять свое оповещение при определенных действиях авторизованного пользователя на сайте. Нашел несколько функций для работы с оповещением но не разобрался как с ними работать( bp_notifications_add_notification и bp_core_add_notification). В сети есть примеры но они довольно сложные и размытые. Скажите можно ли в buddypress добавить свои, произвольные notifications(оповещение) не используя плагин?
На самом деле добавить свой тип оповещения(notifications) в buddypress не так уж сложно, приведу пример, нотификейшена, который будет брать данные (заголовок, контент и ссылку) с произвольных полей поста.
// 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, 'onwp_notifications' ); // 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 ( 'onwp_notifications_action' === $action ) { // проверим это наше действие(само название действия указано будет ниже, при вызове) $custom_title = get_post_meta($item_id, 'notifications_title', true); // берем заголовок для нотификейшена $custom_link = get_post_meta($item_id, 'notifications_link', true); // берем ссылку для нотификейшена $custom_text = get_post_meta($item_id, 'notifications_title', true); // берем контент для нотификейшена // WordPress Toolbar if ( 'string' === $format ) { $return = apply_filters( 'onwp_notifications_filter', '<a href="' . esc_url( $custom_link ) . '" title="' . esc_attr( $custom_title ) . '">' . esc_html( $custom_text ) . '</a>', $custom_text, $custom_link ); // onwp_notifications_filter -добавим свой фильтр, для возможность если что изменить вывод. // Deprecated BuddyBar } /*else { $return = apply_filters( 'onwp_notifications_filter', array( 'text' => $custom_text, 'link' => $custom_link ), $custom_link, (int) $total_items, $custom_text, $custom_title ); }*/ echo $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( $user_id, $item_id ) { bp_notifications_add_notification( array( 'user_id' => $user_id, 'item_id' => $item_id, 'component_name' => 'onwp_notifications', // имя нотификейшена 'component_action' => 'onwp_notifications_action', // действие для нотификейшена 'date_notified' => bp_core_current_time(), 'is_new' => 1, ) ); }
Теперь пример добавления нашего нового нотификейшена
function onwp_add_notification(){ bp_custom_add_notification( 1, 9999 ); // 1 - id пользователя, для которого будет добавлен нотификейшен // 9999 - id поста из которого будем брать данные для контента нашего нотификейшена } add_action('init', 'onwp_add_notification');