Skip to:
Content
Pages
Categories
Search
Top
Bottom

bbp_new_forum hook


  • Laviic
    Participant

    @laviic

    Hello,

    I am trying to write a plugin that adds some meta-information to a forum when it gets created. I can see that bbPress has a “bbp_new_forum” and a “bbp_edit_forum” hook, yet those never appear to fire, no matter what i do. I even added a php-die directly in front of their respective “do_action” calls inside the bbPress source code just to find out, that the part of the code that should call the new_forum hook appears to never actually be executed. What are those hooks for then, and what hooks DO get called when you create/edit a bbPress forum?

    Thank you for your help!

    My Version of WordPress is 3.8.1
    My Version of bbPress is 2.5.3

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

  • Laviic
    Participant

    @laviic

    By further digging around i noticed, that the hooks mentioned above indeed DO fire, but not when a new forum is created… They fire when a new TOPIC inside a forum is created.

    This is highly unintuitve. I suggest giving those hooks a more understandable name?


    John James Jacoby
    Keymaster

    @johnjamesjacoby

    That doesn’t seem right. bbp_new_forum will only fire when forums are created outside of wp-admin, like in a BuddyPress Group Forum. If you want to add meta when any new forum is created, you’ll likely need to hook into WordPress’s wp_insert_post action, instead.


    Laviic
    Participant

    @laviic

    You are correct. The execution of the hooks when creating a new topic was a misobservation on my side. Sorry about that!

    The bbp_new_forum hook firing only when a forum is created outside wp-admin is interesting information. Thanks for clearing that up.

    I have already worked around the ‘missing’ hooks by tying into the save_post hook. Its not exactly what i would call elegant, and it requires some additional checks to make sure my callback was executed for the right event, but it does the job.

    So, the only thing that is left for me to ask is: Why isn’t there an intuitively named hook for when one creates a forum from the backend? Or is there one that i am missing? 🙂

    Thank you for your response!


    antipole
    Participant

    @antipole

    Hi… I am trying this. I want to auto-subscribe all users to new forums and I have tried the following:

    function ovni_subscribe_all_users_to_new_forum($forum_id){  // subscribe all users to a new forum
    	echo 'ovni_subscribe_all_users_to_new_forum fired';
    	// this function will fire on all wp-insert_posts - only act if is for a new forum
    	if ((get_post($forum_id)->post_type == bbp_get_forum_post_type()) && !wp_is_post_revision( $forum_id )){
    		// have just posted a new forum
    		$all_user_ids = get_users();
    		foreach ($all_user_ids as $user){
    			bbp_add_user_forum_subscription($user_id, $forum_id);
    			}
    		}
    	}
     add_action(save_post, ovni_subscribe_all_users_to_new_forum, 90,1);
    

    When I load the New Forum admin page, the echo trace statement appears at the top of the New Forum page, before it has been submitted.

    Can SKS help here please?


    antipole
    Participant

    @antipole

    I am still looking for a solution to this – I want to subscribe users to a new forum when it is created.

    The code above fires when you open the New forum admin page. I guess a new post is created at that time so an object ID is allocated. If you don’t go through and publish, the entry must then get deleted. Anyhow, if you do create the forum by publishing it, the subscriptions do not get created. My guess is that it is too soon in the process of creating a forum to add subscriptions to it.

    I have tried hooking onto the publish action but not got it to fire there. I really need to find a hook much later in the process. As stated above, the bbb_new_forum hook only fires from the front end and that limits it to creating group forums, as far as I can see.

    Any pointers gratefully received. Meanwhile I am having to manually activate some code and run it once after creating a new forum and then deactivate it – very error prone and easily forgotten.


    fterra
    Participant

    @fterra

    Hi, antipole,
    I’m trying to do the same, subscribe all users to new forums and to new topics also.
    I’ve been working on it for a while, mainly figuring out how bbPress itself works.
    In the case you want to run something when someone creates a new forum from the admin page, my bet is you will want to hook to bbp_forum_attributes_metabox_save action, whose argument is $forum_id.
    Hope it helps.


    Robin W
    Moderator

    @robin-w


    antipole
    Participant

    @antipole

    @fterra – thanks for the suggestion. I gave it a go but I don’t see it working for me.

    You say this is your “bet”. Have you actually got something working on this hook?


    antipole
    Participant

    @antipole

    Robin.. thanks for pointing to topic-subscribe.

    It works for me mostly. In testing, I found that when a new topic is held for moderation (I have things set for user’s first topic to be moderated), then once the topic is approved all other users get subscribed to the topic except the submitter, who is left unsubscribed. I guess the approval process is unsubscribing.

    However, I modified your plugin to hook it on last in the actions:

    add_action (‘bbp_new_topic_post_extras’, ‘rw_topic_subscribe’, 1, 99) ;

    and now it seems to work even for moderated topics.

    I also have bbPress auto subscribe for new topics and replies installed and moderated topics are not getting subscribed despite this plugin ticking the box. {I tested your plugin with this other plugin deactivated. This is probably the same issue although the solution may not be so simple – I have raised it on its support forum.

    This may be a bit off-topic as it is not about new forums but I see no better place.

    Tony


    Robin W
    Moderator

    @robin-w

    thanks for that update !


    fterra
    Participant

    @fterra

    Yes, @antipole,
    after I wrote to you I tried the bbp_forum_attributes_metabox_save hook and it has worked for me.
    But I guess it’s also possible to use wp_insert_post hook as suggested by John James Jacoby.
    bbp_forum_attributes_metabox_save will most likely fire only in admin page.
    On the other hand wp_insert_post will most likely fire both in admin page and front-end. Seems like front-end forum creation requires the use of [bbp-forum-form] shortcode, which I have never tried.


    fterra
    Participant

    @fterra

    If you use wp_insert_post, remember to check if the post being saved is a forum.
    Default forum type value is 'forum'.
    But you should use bbp_get_forum_post_type() to use its current value in case it has been altered by some filter.
    You may also take advantage of the following wordpress dynamic hook:
    "save_post_{$post->post_type}"
    which you would hook to with something like:
    add_action("save_post_" . bbp_get_forum_post_type(), "my_hook");


    fterra
    Participant

    @fterra

    Besides that I just noticed that both "save_post_{$post->post_type}" and wp_insert_post are fired automaticaly during editing, to keep the forum saved as a draft.
    So maybe you will benefit more from bbp_forum_attributes_metabox_save really.
    Otherwise you will need some checks to see if it’s not a draft auto-save like in bbPress following code – also note the aforementioned hook being fired at the end:
    (/includes/admin/forums.php)

      /** 
       * Pass the forum attributes for processing 
       * 
       * @since bbPress (r2746) 
       * 
       * @param int $forum_id Forum id 
       * @uses current_user_can() To check if the current user is capable of 
       *                           editing the forum 
       * @uses bbp_get_forum() To get the forum 
       * @uses bbp_is_forum_closed() To check if the forum is closed 
       * @uses bbp_is_forum_category() To check if the forum is a category 
       * @uses bbp_is_forum_private() To check if the forum is private 
       * @uses bbp_close_forum() To close the forum 
       * @uses bbp_open_forum() To open the forum 
       * @uses bbp_categorize_forum() To make the forum a category 
       * @uses bbp_normalize_forum() To make the forum normal (not category) 
       * @uses bbp_privatize_forum() To mark the forum as private 
       * @uses bbp_publicize_forum() To mark the forum as public 
       * @uses do_action() Calls 'bbp_forum_attributes_metabox_save' with the 
       *                    forum id 
       * @return int Forum id 
       */ 
      public function attributes_metabox_save( $forum_id ) { 
     
          if ( $this->bail() ) return $forum_id; 
     
          // Bail if doing an autosave 
          if ( defined( 'DOING_AUTOSAVE' ) && DOING_AUTOSAVE ) 
              return $forum_id; 
     
          // Bail if not a post request 
          if ( ! bbp_is_post_request() ) 
              return $forum_id; 
     
          // Nonce check 
          if ( empty( $_POST['bbp_forum_metabox'] ) || !wp_verify_nonce( $_POST['bbp_forum_metabox'], 'bbp_forum_metabox_save' ) ) 
              return $forum_id; 
     
          // Only save for forum post-types 
          if ( ! bbp_is_forum( $forum_id ) ) 
              return $forum_id; 
     
          // Bail if current user cannot edit this forum 
          if ( !current_user_can( 'edit_forum', $forum_id ) ) 
              return $forum_id; 
     
          // Parent ID 
          $parent_id = ( !empty( $_POST['parent_id'] ) && is_numeric( $_POST['parent_id'] ) ) ? (int) $_POST['parent_id'] : 0; 
     
          // Update the forum meta bidness 
          bbp_update_forum( array( 
              'forum_id' => $forum_id,  
              'post_parent' => (int) $parent_id 
     ) ); 
     
          do_action( 'bbp_forum_attributes_metabox_save', $forum_id ); 
     
          return $forum_id; 
      } 

    antipole
    Participant

    @antipole

    @fterra – thanks for your suggestions.

    I have it working using ‘bbp_forum_attributes_metabox_save’ as you suggested but have found, perhaps not surprisingly, my function is fired on an update to the meta date. So with the code below I create a new forum and all users are subscribe; some choose to unsubscribe; I update something in the meta data and everyone is subscribed again – not good. Any idea how I can determine whether this is a first time save or an update?
    Also I do not want to auto subscribe to group forums. I have fixed this by hard-coding in the group forum’s id, but maybe there is a better way of testing whether a forum_id is a group forum?

    Any ideas welcome thanks.

    function ovni_subscribe_users_to_new_forum($forum_id){  // subscribe all users to a new forum unless it is a group forum
    	if (bbp_get_forum_parent_id( $forum_id ) == 969) return;  // if this is group forum, do nothing
    	$all_user_ids = get_users();
    	foreach ($all_user_ids as $this_user){
    		bbp_add_user_forum_subscription($this_user->id, $forum_id);
    		}
    	}
    add_action('bbp_forum_attributes_metabox_save', 'ovni_subscribe_users_to_new_forum');

    fterra
    Participant

    @fterra

    This is funny.
    I found out that pre_post_update hook – which at first sight seemed to fire only when updating – will fire if the post (or forum) has already been auto-saved AS A DRAFT.
    Similarly I have also been surprised that “save_post_{$post->post_type}” third parameter $update will always be true after it’s first auto-saved AS A DRAFT.
    So to know if it’s a new forum (or post), I guess you have to check its previous status. For new posts the previous status will be either ‘auto-draft’, ‘draft’ or I guess it’s also possible there will be no previous status yet.

    So you can do something like hook to pre_post_update and store its current (which soon in the same request will become “previous”) status. Like this:

    function my_pre_post_update_hook($post_ID) {
    	global $my_forum_old_status;
    	$my_forum_old_status = bbp_get_forum_visibility($post_ID);
    }
    add_action( 'pre_post_update', 'my_pre_post_update_hook', 10 );

    So in your bbp_forum_attributes_metabox_save action you can check if it’s a new forum with something like:

    	global $my_forum_old_status;
    	if (in_array($my_forum_old_status, array(NULL, 'auto-draft', 'draft')))

    Warning – this condition is enough because when bbp_forum_attributes_metabox_save action is fired we know the forum’s current status will not be any of these.

    I guess you could also do it with the transition_post_status hook, whose arguments are: $new_status, $old_status, $post.


    fterra
    Participant

    @fterra

    This would be more appropriate:

    function my_pre_post_update_hook($post_ID) {
    	if (bbp_is_forum($post_ID)) {
    		global $my_forum_old_status;
    		$my_forum_old_status = bbp_get_forum_visibility($post_ID);
    	}
    }
    add_action( 'pre_post_update', 'my_pre_post_update_hook' );

    antipole
    Participant

    @antipole

    Thanks @fterra for your suggestions.
    I cannot see that my action on pre_post_update is firing at all, even if I edit an existing forum. I have trace actions in and neither user 33 or 32 are getting subscribed.
    Puzzled.

    function ovni_pre_post_update_status($forum_id){
    	bbp_add_user_forum_subscription(33, $forum_id);	// debug **********
    	if (bbp_is_forum($forum_id)) {
    		global $ovni_forum_old_status;
    			bbp_add_user_forum_subscription(31, $forum_id);	// debug **********
    		$ovni_forum_old_status = bbp_get_forum_visibility($forum_id);
    		}
    	}
    add_action('pre_post_update', 'ovni_pre-post_update_status');

    fterra
    Participant

    @fterra

    Hey, your add_action’s second parameter is different from your function’s name!


    antipole
    Participant

    @antipole

    Thanks – yes – stupid error. It is always amazing what a fresh eye brings.
    Things started working then and I checked all my numerous trace actions and it is now working well. Here is the final version for your interest.

    thanks a million, Tony

    // (2)	When a new forum is created, all current users are automatically subscribed to it
    
    //	Note status of forum entry at start of any update
    function ovni_pre_post_update_status($forum_id){
    	if (bbp_is_forum($forum_id)) {
    		global $ovni_forum_old_status;
    		$ovni_forum_old_status = bbp_get_forum_visibility($forum_id);
    		}
    	}
    add_action('pre_post_update', 'ovni_pre_post_update_status');
    
    function ovni_subscribe_users_to_new_forum($forum_id){  // subscribe all users to a new forum unless it is a group forum
    	if (bbp_is_forum($forum_id)){
    		if (bbp_get_forum_parent_id( $forum_id ) == 969) return;  // if this is group forum, do nothing
    		global $ovni_forum_old_status;
    		if (in_array($ovni_forum_old_status, array(NULL, 'auto-draft', 'draft'))){
    			// forum has just been published
    			$all_user_ids = get_users();
    			foreach ($all_user_ids as $this_user){
    				bbp_add_user_forum_subscription($this_user->id, $forum_id);
    				}
    			}
    		}
    	}
    add_action('bbp_forum_attributes_metabox_save', 'ovni_subscribe_users_to_new_forum');
    
Viewing 19 replies - 1 through 19 (of 19 total)
  • You must be logged in to reply to this topic.
Skip to toolbar