fterra (@fterra)

Forum Replies Created

Viewing 7 replies - 1 through 7 (of 7 total)
  • In reply to: bbp_new_forum hook

    fterra
    Participant

    @fterra

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

    In reply to: bbp_new_forum hook

    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' );
    In reply to: bbp_new_forum hook

    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.

    In reply to: bbp_new_forum 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; 
      } 
    In reply to: bbp_new_forum hook

    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");

    In reply to: bbp_new_forum hook

    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.

    In reply to: bbp_new_forum hook

    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.

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