bbp_get_user_subscribe_link( mixed $args = '', int $user_id, bool $wrap = true )
Return the link to subscribe/unsubscribe from a forum or topic
Description
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: ''
- $user_id
-
(Optional) User id
- $wrap
-
(Optional) If you want to wrap the link in <span id="subscription-toggle">.
Default value: true
Return
(string) Permanent link to topic
Source
File: bp-forums/users/template.php
function bbp_get_user_subscribe_link( $args = '', $user_id = 0, $wrap = true ) {
if ( ! bbp_is_subscriptions_active() ) {
return;
}
// Parse arguments against default values
$r = bbp_parse_args( $args, array(
'subscribe' => __( 'Subscribe', 'buddyboss' ),
'unsubscribe' => __( 'Unsubscribe', 'buddyboss' ),
'user_id' => 0,
'topic_id' => 0,
'forum_id' => 0,
'before' => ' | ',
'after' => ''
), 'get_user_subscribe_link' );
// Validate user and object ID's
$user_id = bbp_get_user_id( $r['user_id'], true, true );
$topic_id = bbp_get_topic_id( $r['topic_id'] );
$forum_id = bbp_get_forum_id( $r['forum_id'] );
if ( empty( $user_id ) || ( empty( $topic_id ) && empty( $forum_id ) ) ) {
return false;
}
// No link if you can't edit yourself
if ( ! current_user_can( 'edit_user', (int) $user_id ) ) {
return false;
}
// Check if viewing a single forum
if ( empty( $topic_id ) && ! empty( $forum_id ) ) {
// Decide which link to show
$is_subscribed = bbp_is_user_subscribed_to_forum( $user_id, $forum_id );
if ( ! empty( $is_subscribed ) ) {
$text = $r['unsubscribe'];
$query_args = array( 'action' => 'bbp_unsubscribe', 'forum_id' => $forum_id );
} else {
$text = $r['subscribe'];
$query_args = array( 'action' => 'bbp_subscribe', 'forum_id' => $forum_id );
}
// Create the link based where the user is and if the user is
// subscribed already
if ( bbp_is_subscriptions() ) {
$permalink = bbp_get_subscriptions_permalink( $user_id );
} elseif ( bbp_is_single_forum() || bbp_is_single_reply() ) {
$permalink = bbp_get_forum_permalink( $forum_id );
} else {
$permalink = get_permalink();
}
$url = esc_url( wp_nonce_url( add_query_arg( $query_args, $permalink ), 'toggle-subscription_' . $forum_id ) );
$sub = $is_subscribed ? ' class="is-subscribed"' : '';
$html = sprintf( '%s<span id="subscribe-%d" %s><a href="%s" class="subscription-toggle" data-forum="%d">%s</a></span>%s', $r['before'], $forum_id, $sub, $url, $forum_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>';
}
} else {
// Decide which link to show
$is_subscribed = bbp_is_user_subscribed_to_topic( $user_id, $topic_id );
if ( ! empty( $is_subscribed ) ) {
$text = $r['unsubscribe'];
$query_args = array( 'action' => 'bbp_unsubscribe', 'topic_id' => $topic_id );
} else {
$text = $r['subscribe'];
$query_args = array( 'action' => 'bbp_subscribe', 'topic_id' => $topic_id );
}
// Create the link based where the user is and if the user is
// subscribed already
if ( bbp_is_subscriptions() ) {
$permalink = bbp_get_subscriptions_permalink( $user_id );
} elseif ( bbp_is_single_topic() || bbp_is_single_reply() ) {
$permalink = bbp_get_topic_permalink( $topic_id );
} else {
$permalink = get_permalink();
}
$url = esc_url( wp_nonce_url( add_query_arg( $query_args, $permalink ), 'toggle-subscription_' . $topic_id ) );
$sub = $is_subscribed ? ' class="is-subscribed"' : '';
$html = sprintf( '%s<span id="subscribe-%d" %s><a href="%s" class="subscription-toggle" data-topic="%d">%s</a></span>%s', $r['before'], $topic_id, $sub, $url, $topic_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>';
}
}
// Return the link
return apply_filters( 'bbp_get_user_subscribe_link', $html, $r, $user_id, $topic_id );
}
Changelog
| Version | Description |
|---|---|
| bbPress (r2668) | 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.