Skip to:
Content
Pages
Categories
Search
Top
Bottom

bbp_add_user_subscription help

  • @jrb9406

    Participant

    I am trying to make a single page where users can choose to subscribe or unsubscribe to their forums (see https://bbpress.org/forums/topic/manage-subscriptions/). I’m pretty new to php/java, so please excuse me if this is obvious.

    Basically, I have 3 forums (79, 80, 81). I have a button for each forum. When the user clicks a button, I use AJAX to run a php script. The php script is used to either subscribe or unsubscribe the user depending on whether they are already subscribed or not.

    My problem is that when a user is subscribed, I’m not able to remove their subscription, and vice versa. When I click a subscribe button, the php code executes, it is able to determine if the user is subscribed, and the if statement executes properly. My problem is that bbp_add_user_subscription and bbp_remove_user_subscription return false every time.

    Any help is greatly appreciated. The PHP and java script are below.

    <?php
    if (isset($_POST[‘action’])) {
    $forum = $_POST[‘action’];
    $forumint = 0 + $forum;
    $subs=bbp_is_user_subscribed_to_forum($user_id,$forum);
    if($subs==false) {
    //if user not subscribed, then subscribe
    $add=bbp_add_user_subscription($user_id,$forumint);
    }
    else {
    $rmv=bbp_remove_user_subscription($user_id,$forumint);
    //do_action(‘bbp_remove_user_subscription’, $user_id, $forumint)
    }
    echo ‘forum ‘;
    var_dump ($forum);
    echo ‘subscribed ‘;
    var_dump ($subs);
    echo ‘add ‘;
    var_dump ($add);
    echo ‘remove ‘;
    var_dump ($rmv);
    echo ‘foruminteger ‘;
    var_dump ($forumint);
    exit;
    }
    ?>
    <?php
    
    <!– initiate all buttons –>
    <button id=”790″ class=”button” onclick=”Subscriber(‘790’)”>Subscribe</button><br><br>
    <button id=”800″ class=”button” onclick=”Subscriber(‘800’)”>Subscribe</button><br><br>
    <button id=”810″ class=”button” onclick=”Subscriber(‘810’)”>Subscribe</button><br><br>
    
    <!– change button text on load based on user subscription –>
    
    <script>
    $( document ).ready(function() {
    $( “.button” ).click(function() {
    var ForumID = $(this).attr(“id”)/10;
    $.ajax({
    //url: ”, // url is empty because I’m working in the same file
    data: {‘action’: ForumID},
    type: ‘post’,
    success: function(result) {
    alert(“action performed successfully”); //this alert is fired
    $(‘div#result’).text(‘Button clicked: ‘ + result);
    }
    });
    });
    });
    </script>
Viewing 4 replies - 1 through 4 (of 4 total)
  • @jrb9406

    Participant

    EDIT:
    Also tried bbp_remove_user_forum_subscription and bbp_add_user_forum_subscription with same results.

    As I understand it, it should return true if the user was subscribed/unsubscribed.

    @jrb9406

    Participant

    sorry, one more edit to hopefully help. With the script below, it does not subscribe or unsubscribe from the forum, but when I execute the code and check my subscriptions page (from profile), the titles of the subscribed forums are removed, but the row for the forum remains (ie. when I click subscribe, my subscriptions page shows titles, when I click unsubscribe, the subscriptions page does not show the title for the forum I tried to unsubscribe to, but it shows the number of topics, posts, etc.).

    <?php
    if (isset($_POST[‘action’])) {
    $forum = $_POST[‘action’];
    $forumint = 0 + $forum;
    $user_id = bbp_get_user_id();
    $subs=bbp_is_user_subscribed_to_forum($user_id, $forum);
    if($subs==false) {
    //if user not subscribed, then subscribe
    $add=bbp_add_user_forum_subscription( $user_id, $forumint );
    $add2=bbp_add_user_subscription( $user_id, $forumint);
    }
    else {
    $rmv=bbp_remove_user_forum_subscription( $user_id, $forumint );
    $rmv=bbp_remove_user_subscription( 0, 79 );
    $rmv2=bbp_remove_user_subscription( $user_id, $forumint);
    }
    echo ‘forum ‘;
    var_dump ($forum);
    echo ‘subscribed ‘;
    var_dump ($subs);
    echo ‘add ‘;
    var_dump ($add);
    echo ‘remove ‘;
    var_dump ($rmv);
    echo ‘foruminteger ‘;
    var_dump ($forumint);
    echo ‘userid ‘;
    var_dump ($user_id);
    exit;
    }
    ?>

    @jrb9406

    Participant

    Ok, I’ve been working at this all day (really several weeks) and here’s what I learned.

    bbp_add_user_forum_subscription requires user ID which must be obtained by doing

    $user_idd = bbp_get_user_id(0,FALSE,TRUE);

    these will not work:

    $user_idd = bbp_get_user_id()
    $user_idd = bbp_get_user_id(0,TRUE,FALSE)

    (note that I changed the variable from $user_id so as to not double define)

    This works. The working php code is below.

    <?php
    
    if (isset($_POST[‘action’])) {
    $forum = $_POST[‘action’];
    $forumint = 0 + $forum;
    $user_idd = bbp_get_user_id(0,FALSE,TRUE);
    $subs=bbp_is_user_subscribed_to_forum($user_idd, $forum);
    if($subs==false) {
    //if user not subscribed, then subscribe
    $add=bbp_add_user_forum_subscription( $user_idd, $forumint );
    }
    else {
    $rmv=bbp_remove_user_forum_subscription( $user_idd, $forumint );
    }
    exit;
    }
    ?>

    @robkk

    Moderator

    I am glad that you seem to have this working now. 🙂

Viewing 4 replies - 1 through 4 (of 4 total)
  • You must be logged in to reply to this topic.
Skip to toolbar