bp_notifications_add_notification( array $args = array() )

Add a notification for a specific user, from a specific component.

Description

Parameters

$args

(array) (Optional) Array of arguments describing the notification. All are optional.

  • 'user_id'
    (int) ID of the user to associate the notification with.
  • 'item_id'
    (int) ID of the item to associate the notification with.
  • 'secondary_item_id'
    (int) ID of the secondary item to associate the notification with.
  • 'component_name'
    (string) Name of the component to associate the notification with.
  • 'component_action'
    (string) Name of the action to associate the notification with.
  • 'date_notified'
    (string) Timestamp for the notification.

Default value: array()

Return

(int|bool) ID of the newly created notification on success, false on failure.

Source

File: bp-notifications/bp-notifications-functions.php

function bp_notifications_add_notification( $args = array() ) {

	$r = bp_parse_args( $args, array(
		'user_id'           => 0,
		'item_id'           => 0,
		'secondary_item_id' => 0,
		'component_name'    => '',
		'component_action'  => '',
		'date_notified'     => bp_core_current_time(),
		'is_new'            => 1,
		'allow_duplicate'   => false,
	), 'notifications_add_notification' );

	// Check for existing duplicate notifications.
	if ( ! $r['allow_duplicate'] ) {
		// Date_notified, allow_duplicate don't count toward
		// duplicate status.
		$existing = BP_Notifications_Notification::get( array(
			'user_id'           => $r['user_id'],
			'item_id'           => $r['item_id'],
			'secondary_item_id' => $r['secondary_item_id'],
			'component_name'    => $r['component_name'],
			'component_action'  => $r['component_action'],
			'is_new'            => $r['is_new'],
		) );

		if ( ! empty( $existing ) ) {
			return false;
		}
	}

	// Setup the new notification.
	$notification                    = new BP_Notifications_Notification;
	$notification->user_id           = $r['user_id'];
	$notification->item_id           = $r['item_id'];
	$notification->secondary_item_id = $r['secondary_item_id'];
	$notification->component_name    = $r['component_name'];
	$notification->component_action  = $r['component_action'];
	$notification->date_notified     = $r['date_notified'];
	$notification->is_new            = $r['is_new'];

	// Save the new notification.
	return $notification->save();
}

Changelog

Changelog
Version Description
BuddyPress 1.9.0 Introduced.

Questions?

We're always happy to help with code or other questions you might have! Search our developer docs, contact support, or connect with our sales team.