bbp_get_user_subscribe_link( array $args = array(), int $user_id, bool $wrap = true )

Return the link to subscribe/unsubscribe from a forum or topic


Description Description


Parameters Parameters

$args

(Optional) This function supports these arguments: - subscribe: Subscribe text - unsubscribe: Unsubscribe text - user_id: User id - topic_id: Topic id - forum_id: Forum id - before: Before the link - after: After the link

Default value: array()

$user_id

(Optional) User id

$wrap

(Optional) If you want to wrap the link in <span id="subscription-toggle">.

Default value: true


Top ↑

Return Return

(string) Permanent link to topic


Top ↑

Source Source

File: includes/users/template.php

	function bbp_get_user_subscribe_link( $args = array(), $user_id = 0, $wrap = true ) {

		// Bail if subscriptions are inactive
		if ( ! bbp_is_subscriptions_active() ) {
			return;
		}

		// Parse arguments against default values
		$r = bbp_parse_args( $args, array(
			'subscribe'   => esc_html__( 'Subscribe',   'bbpress' ),
			'unsubscribe' => esc_html__( 'Unsubscribe', 'bbpress' ),
			'user_id'     => 0,
			'object_id'   => 0,
			'object_type' => 'post',
			'before'      => '',
			'after'       => '',
			'redirect_to' => '',

			// Deprecated. Use object_id.
			'forum_id'    => 0,
			'topic_id'    => 0
		), 'get_user_subscribe_link' );

		// Validate user
		$user_id     = bbp_get_user_id( $r['user_id'], true, true );
		$object_type = sanitize_key( $r['object_type'] );

		// Back-compat for deprecated arguments
		if ( ! empty( $r['topic_id'] ) ) {
			$object_id = absint( $r['topic_id'] );
		} elseif ( ! empty( $r['forum_id'] ) ) {
			$object_id = absint( $r['forum_id'] );
		} else {
			$object_id = absint( $r['object_id'] );
		}

		// Bail if anything is missing
		if ( empty( $user_id ) || empty( $object_id ) || empty( $object_type ) ) {
			return false;
		}

		// No link if you can't edit yourself
		if ( ! current_user_can( 'edit_user', $user_id ) ) {
			return false;
		}

		// Decide which link to show
		$is_subscribed = bbp_is_user_subscribed( $user_id, $object_id );
		if ( ! empty( $is_subscribed ) ) {
			$text   = $r['unsubscribe'];
			$q_args = array(
				'action'      => 'bbp_unsubscribe',
				'object_id'   => $object_id,
				'object_type' => $object_type
			);
		} else {
			$text   = $r['subscribe'];
			$q_args = array(
				'action'      => 'bbp_subscribe',
				'object_id'   => $object_id,
				'object_type' => $object_type
			);
		}

		// Custom redirect
		if ( ! empty( $r['redirect_to'] ) ) {
			$q_args['redirect_to'] = urlencode( $r['redirect_to'] );
		}

		// URL
		$url  = esc_url( wp_nonce_url( add_query_arg( $q_args ), 'toggle-subscription_' . $object_id ) );
		$sub  = $is_subscribed ? ' class="is-subscribed"' : '';
		$html = sprintf( '%s<span id="subscribe-%d"  %s><a href="%s" class="subscription-toggle" data-bbp-object-id="%d" data-bbp-object-type="%d" data-bbp-nonce="%s">%s</a></span>%s', $r['before'], $object_id, $sub, $url, $object_id, $object_type, wp_create_nonce( 'toggle-subscription_' . $object_id ), $text, $r['after'] );

		// Initial output is wrapped in a span, ajax output is hooked to this
		if ( ! empty( $wrap ) ) {
			$html = '<span id="subscription-toggle">' . $html . '</span>';
		}

		// Filter & return
		return apply_filters( 'bbp_get_user_subscribe_link', $html, $r, $user_id, $object_id );
	}

Top ↑

Changelog Changelog

Changelog
Version Description
2.6.0 bbPress (r6308) Add 'redirect_to' support
2.0.0 Introduced.

Top ↑

User Contributed Notes User Contributed Notes

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