Skip to:
Content
Pages
Categories
Search
Top
Bottom

New Users Auto Subscribe to One Forum


  • Nathan
    Participant

    @nhadsall

    Hi,
    I would like to hook in so any new user is automatically subscribed to a forum. I know the user ID and forum ID, but I do not know what function I should call to subscribe that user to the forum. Thanks for the help!

    bbPress 2.5.3

    – Nathan

Viewing 24 replies - 1 through 24 (of 24 total)

  • Robin W
    Moderator

    @robin-w

    The function is

    bbp_add_user_forum_subscription

    it is in

    bbpress/includes/users/functions.php

    you pass it user-id and forum_id


    iclimb
    Participant

    @iclimb

    Hi nhadsall.

    This is just the function i’mo looking for. have you made something that works? I need all future users to be subscribed to a specific forum, but still be able to unsubscribe afterwards.

    My php skills are wery sparse, so i hope you might share your solution or help me one the way.
    I know my way around wordpress and coding in general so i won’t take hours of guidance 🙂

    Thanks in advance.

    Regards Christian


    Robin W
    Moderator

    @robin-w

    @iclimb hopefully Nathan will share his solution with us.


    Nathan
    Participant

    @nhadsall

    @robin-w thanks for the function. This is not going to be as easy as I expected.

    It was easy to hook into the new user creation with “user_register”, but WordPress is gives me a “Fatal error: Call to undefined function” when I try to call bbp_add_user_forum_subscription from this hook (my theme’s function.php).

    I saw somewhere else that some functions need to be run within a bbpress loop?


    @iclimb
    , I’ll be happy to share if I get it working.


    Nathan
    Participant

    @nhadsall

    Okay,
    It looks like I am running into problems because the hook is executing on wp-activate.php, which does not load plugins. This may be more of a problem than I thought…


    Robin W
    Moderator

    @robin-w

    keep us updated, this is not my field of expertise, so can’t directly help you !


    palmdoc
    Participant

    @palmdoc

    I’d like to see an auto-subscribe feature for forums too!


    Nathan
    Participant

    @nhadsall

    So, after DAYS of working on this I finally have a solution. I have not done a lot of testing, but this has worked so far.

    Basically what is happening is that for forums (not replies) the logic is inverted. This means that users are subscribed to a forum unless they unsubscribe (ie. are added to the DB).

    Just add this to your theme’s function.php and it should work.

    I hope this helps someone, because it has taken a LOT of work!

    add_filter( 'bbp_get_user_subscribe_link', 'invert_get_user_subscribe_link', 10, 4 ); //invert forum subscription
    add_filter( 'bbp_is_user_subscribed_to_forum', 'invert_is_user_subscribed_to_forum', 10, 4 ); //invert forum subscription
    add_filter( 'bbp_get_forum_subscribers', 'invert_get_forum_subscribers' ); //invert forum subscription
    add_filter( 'bbp_is_user_subscribed', 'invert_is_user_subscribed', 10, 4 ); //invert forum subscription
    
    function invert_is_user_subscribed($retval, $user_id, $object_id, $subscribed_ids) {
    	if (get_post_type( $object_id ) == bbp_get_forum_post_type())
    		return !$retval;
    	else	
    		return $retval;
    }
    
    function strContains($needle, $haystack) {
    	if (strpos($haystack, $needle) !== false) {
    		return true;
    	} else {
    		return false;
    	}
    }
    
    function invert_get_user_subscribe_link ($html, $r, $user_id, $topic_id) {
    	if (strContains( "bbp_unsubscribe", $html )) {
    		$html = str_replace("bbp_unsubscribe", "bbp_subscribe", $html);
    	} else {
    		$html = str_replace("bbp_subscribe", "bbp_unsubscribe", $html);
    	}
    	return $html;
    }
    
    function invert_get_forum_subscribers( $users ) {
    	$args = array('fields' => 'id');
    	$all_users = get_users($args);
    	$send_to_users    = array_diff($all_users, $users);
    	return $send_to_users;
    }
    
    function invert_is_user_subscribed_to_forum( $retval, $user_id, $forum_id, $subscribed_ids ) {
    	return !$retval;
    }
    

    I also include make users “subscribed” to anything they write by default. All they have to do is uncheck the box. The following code is also added to function.php.

    add_action('bbp_theme_after_topic_form_subscriptions', 'subscribed_by_default'); //default subscribe
    add_action('bbp_theme_after_reply_form_subscription', 'subscribed_by_default'); //default subscribe
    
    function subscribed_by_default() 
    	echo '<script type="text/javascript">jQuery("#bbp_topic_subscription").prop("checked","checked");</script>';
    }

    Robin W
    Moderator

    @robin-w

    Great stuff !

    It always amazes me how long I spend to produce so little code 🙂

    Bet you learned a lot along the way though!


    iclimb
    Participant

    @iclimb

    nhadsall, this is great..! thumbs up and great work. i’ve tested it a little bit and it seems to work when a new forum i created.

    I really don’t want to sound demanding, because i’m really appreciating the work you’re doing!
    But one thing i think it is missing, is when a new user is created, he is not subscribed to existing forums. He will only be subscribed to new ones. Do you think it would be possible to make new users subscribed to all forums when created? i realize that it probably means some work, but i think it will make most sense that way.

    Best regards, Christian


    Nathan
    Participant

    @nhadsall

    Hi Christian,
    The code I ended up using was not my original idea. I didn’t hook into a new user creation. What this does is inverts the subscribe list. It turns it into an unsubscribe list. So any user should be subscribed to any new forum by default. Let me know if something is not working for you, and I’ll see what I can do!

    – Nathan


    crushdg
    Participant

    @crushdg

    Anyone have an idea of how I can use this script, to just auto subscribe everyone to a specific form, based upon it’s ID?


    Robin W
    Moderator

    @robin-w

    @crushdg

    I doubt that would be possible.

    This script reverses the logic

    so normally the database would have a list of forums you have manually subscribed to.

    This changes it to have a list of forums you have manually unsubscribed to, thus you are subscribed by default.

    So the code would not work to just have one forum.


    tummybos
    Participant

    @tummybos

    I think we better look for a work around to be able to subscribe a new user to a partocular forum. So from there we can subscribe new users to all forums if we want.


    tummybos
    Participant

    @tummybos

    Another thing @nhadsall is will this not unsubscribe a user that intentionally clicks “notify me” when replying a topic?

    Just in case people end up here looking for a solution, I have included a function in my bbP Toolkit plugin to add new users automatically to a list of predefined forums.


    SRD75
    Participant

    @srd75

    Hi. I tried the following code, created a new user, logged in as that user, went to subscriptions, and the user was not subscribed to any forums.

    Was hoping the new user would be subscribed to the single forum that exists.

    Help appreciated.

    Regards,
    Steve

    add_filter( 'bbp_get_user_subscribe_link', 'invert_get_user_subscribe_link', 10, 4 ); //invert forum subscription
    add_filter( 'bbp_is_user_subscribed_to_forum', 'invert_is_user_subscribed_to_forum', 10, 4 ); //invert forum subscription
    add_filter( 'bbp_get_forum_subscribers', 'invert_get_forum_subscribers' ); //invert forum subscription
    add_filter( 'bbp_is_user_subscribed', 'invert_is_user_subscribed', 10, 4 ); //invert forum subscription
    
    function invert_is_user_subscribed($retval, $user_id, $object_id, $subscribed_ids) {
    	if (get_post_type( $object_id ) == bbp_get_forum_post_type())
    		return !$retval;
    	else	
    		return $retval;
    }
    
    function strContains($needle, $haystack) {
    	if (strpos($haystack, $needle) !== false) {
    		return true;
    	} else {
    		return false;
    	}
    }
    
    function invert_get_user_subscribe_link ($html, $r, $user_id, $topic_id) {
    	if (strContains( "bbp_unsubscribe", $html )) {
    		$html = str_replace("bbp_unsubscribe", "bbp_subscribe", $html);
    	} else {
    		$html = str_replace("bbp_subscribe", "bbp_unsubscribe", $html);
    	}
    	return $html;
    }
    
    function invert_get_forum_subscribers( $users ) {
    	$args = array('fields' => 'id');
    	$all_users = get_users($args);
    	$send_to_users    = array_diff($all_users, $users);
    	return $send_to_users;
    }
    
    function invert_is_user_subscribed_to_forum( $retval, $user_id, $forum_id, $subscribed_ids ) {
    	return !$retval;
    }

    SRD75
    Participant

    @srd75

    Hello @nhadsall, can you help please?


    SRD75
    Participant

    @srd75

    Hello anyone?


    Robin W
    Moderator

    @robin-w

    Psacal’s bbp toolkit does that

    bbP Toolkit


    SRD75
    Participant

    @srd75

    Thank you Robin.

    I’ve installed and activated the plugin, but can’t find the plugin’s options screen.

    Can you direct me to it please?

    Thanks,
    Steve


    SRD75
    Participant

    @srd75

    Found it. The new WordPress.org interface had me confused.

    Found the install instructions now.


    SRD75
    Participant

    @srd75

    Excellent, just what we needed.

    Thanks Robin.


    Robin W
    Moderator

    @robin-w

    no problem, glad you are fixed !

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