bp_email_get_unsubscribe_link( array $args )

Creates unsubscribe link for notification emails.


Parameters Parameters

$redirect_to

(string) (Required) The URL to which the unsubscribe query string is appended.

$args

(array) (Required) Used to build unsubscribe query string.

  • 'notification_type'
    (string) Which notification type is being sent.
  • 'user_id'
    (string) The ID of the user to whom the notification is sent.
  • 'redirect_to'
    (string) Optional. The url to which the user will be redirected. Default is the activity directory.
  • 'email'
    (string) Optional. The email address of the user to whom the notification is sent.


Top ↑

Return Return

(string) The unsubscribe link.


Source Source

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

function bp_email_get_unsubscribe_link( $args ) {
	$emails = bp_email_get_unsubscribe_type_schema();

	if ( empty( $args['notification_type'] ) || ! array_key_exists( $args['notification_type'], $emails ) ) {
		return wp_login_url();
	}

	$email_type  = $args['notification_type'];
	$redirect_to = ! empty( $args['redirect_to'] ) ? $args['redirect_to'] : site_url();
	$user_id     = (int) $args['user_id'];

	// Bail out if the activity type is not un-unsubscribable.
	if ( empty( $emails[ $email_type ]['unsubscribe'] ) ) {
		return '';
	}

	$link = '';
	// Case where the recipient is a member of the site.
	if ( ! empty( $user_id ) ) {
		$link = add_query_arg(
			array(
				'action' => 'unsubscribe',
				'nh'     => hash_hmac( 'sha1', "{$email_type}:{$user_id}", bp_email_get_salt() ),
				'nt'     => $args['notification_type'],
				'uid'    => $user_id,
			),
			$redirect_to
		);

	// Case where the recipient is not a member of the site.
	} else if ( ! empty( $args['email_address'] ) ) {
		$email_address = $args['email_address'];
		$member_id     = (int) $args['member_id'];
		$link          = add_query_arg(
			array(
				'action' => 'unsubscribe',
				'nh'     => hash_hmac( 'sha1', "{$email_type}:{$email_address}", bp_email_get_salt() ),
				'nt'     => $args['notification_type'],
				'mid'    => $member_id,
				'uem'    => $email_address,
			),
			$redirect_to
		);
	}

	/**
	 * Filters the unsubscribe link.
	 *
	 * @since 2.7.0
	 */
	return apply_filters( 'bp_email_get_link', $link, $redirect_to, $args );
}


Top ↑

Changelog Changelog

Changelog
Version Description
2.7.0 Introduced.

Top ↑

User Contributed Notes User Contributed Notes

You must log in before being able to contribute a note or feedback.