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

1081
1082
1083
1084
1085
1086
1087
1088
1089
1090
1091
1092
1093
1094
1095
1096
1097
1098
1099
1100
1101
1102
1103
1104
1105
1106
1107
1108
1109
1110
1111
1112
1113
1114
1115
1116
1117
1118
1119
1120
1121
1122
1123
1124
1125
1126
1127
1128
1129
1130
1131
1132
1133
1134
1135
1136
1137
1138
1139
1140
1141
1142
1143
1144
1145
1146
1147
1148
1149
1150
1151
1152
1153
1154
1155
1156
1157
1158
1159
1160
1161
1162
1163
1164
1165
1166
1167
1168
1169
1170
1171
1172
1173
1174
1175
1176
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'      => '&nbsp;|&nbsp;',
        '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

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.