Skip to:
Content
Pages
Categories
Search
Top
Bottom

Gravity Forms to bbPress (Force Topics to Specified Forum)


  • jsteckler1
    Participant

    @jsteckler1

    Using Gravity Forms and bbPress, I am attempting to create a new topic, within a specific forum, upon form submission.

    I have tried to incorporate the Gravity Forms Custom Post Type add-on to accomplish this, but, presumably due to the non-heirarchial structure of bbPress forums, I only seem able to either:

    A) Create a new FORUM with the submitted data
    or
    B) Create a new TOPIC that goes unassigned to any forum

    So, basically, unless a topic can be created as a child of a forum, I’m not sure there’s a way to get this working with that add-on.

    Is there any way I can force the topic to be created within a specified forum using php? Any other thoughts or alternatives? Maybe a workaround that forces all topics created via Gravity Forms submission to default to a specified forum?

    For what it’s worth, I am using Gravity Forms for users to submit event proposals. My idea is to then have the submission data routed into a forum like bbPress where the powers that be may vote on and discuss each proposal.

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

  • Robin W
    Moderator

    @robin-w

    a topic’s forum is simply its post_parent

    so your topic has a post_id and your forum has a post_id

    so

    // Update topic (post) 1234  forum (post) 321
      $my_post = array(
          'ID'           => 1234,
          'post_parent'   => 321,
           );
    
    // Update the post into the database
      wp_update_post( $my_post );

    jsteckler1
    Participant

    @jsteckler1

    Awesome, I appreciate the quick response tremendously, Robin!

    Since the Gravity Forms submission will — each time — be creating the forum topic (post) itself, however, how could I include the topic (post) ID? Would that be the form ID in this case?


    Robin W
    Moderator

    @robin-w

    the plugin says that you can

    ◾Assign parent post for generated post (Drop Down field only)

    I haven’t used this plugin, so can’t comment further !!


    jsteckler1
    Participant

    @jsteckler1

    Understood, but maybe this, below, is a straightforward solution?

    Is there a function that could force all new topics to be created under a specified forum ID (in this case, that would be 31086)?

    To your point about the plugin, it also states:

    Try to set parent

    If this is checked, and the form creates a post type, then the parent for the newly created post type will be set from the value of this field. Please note that this only works for heirarcical post typs e.g. pages

    This would not be a hierarchical post type, because Forums and Topics are totally separate custom post types, correct?


    Robin W
    Moderator

    @robin-w

    Forums and Topics are totally separate custom post types, correct?

    Yes.

    I am reticent to start delving into this plugin – there are hundreds of thousand of plugins in wordpress and I only go into those that are specifically bbpress and then only a few of those – it can take a day easily to look at and understand a plugins code 🙂

    There is a hook in topic creation that you could use

    The following code is untested, but you could put this in your functions file. You must be able to edit that file outside of wordpress (ie NOT using dashboard>appearance>editor) in case it errors so you can amend it.

    add_action( 'bbp_new_topic_pre_extras', 'new_topic_id', 10, 1 );
    
    function new_topic_id ($forum_id=0) {
    	if (empty ($forum_id)) $forum_id  = 31086 ;
    	return $forum_id ;
    }
    

    This will put any topic that doesn’t have a forum into that forum.


    Robin W
    Moderator

    @robin-w

    or that could be total rubbish if gravity forms aren’t using bbpress to create, which they probably aren’t – let me think further !!


    jsteckler1
    Participant

    @jsteckler1

    Robin,

    You are a saint and I can’t even begin to express how much I appreciate you taking the time to help me out with this. Really.

    I assure you I am “proficient enough” to use an FTP client for any potential changes; I’m just very elementary in terms of writing the code myself.

    I added the code to my functions.php, but, as you suspected, it did not yield the desired result.

    I’m also totally open to ditching that plugin solution entirely, unless there is some other way you know of that I could restructure my forum in such a way that it becomes accordingly hierarchical. Or perhaps some implementation revolved around categories or topic tags? I’m open to anything that will get these submissions routed into a specified forum.

    Again, thank you so much, and please let me know if there’s anything I can provide you with that might fill in any potential blanks.

    To that end, maybe the Gravity Forms submission hooks might help? The documentation can be found here.


    Robin W
    Moderator

    @robin-w

    ok, let’s see if this works

    This just adds an action on any post save and if it is a forum and has no post parent, we add your default one

    AGAIN THIS IS UNTESTED !!

    add_action( 'save_post', 'add_forum_id' );
    
    function add_forum_id( $post_id ) {
    	//get post type and post parent id
    	$post_type = get_post_type($post_id) ;
    	$post_parent = wp_get_post_parent_id ($post_id) ;
    	//if it's a forum and has no post parent (allows for normal topic creation, and other posts type creation etc.)
    	if ($post_type == bbp_get_forum_post_type() && (empty ($post_parent))) {
    		// Update topic (post) 1234  forum (post) 321
    		$my_post = array(
    			'ID'           => $post_id,
    			'post_parent'   => 31086,
    		);
    
    		// Update the post into the database
    		wp_update_post( $my_post );
    	}
    
    }

    jsteckler1
    Participant

    @jsteckler1

    Still no luck, I’m sorry to say.


    jsteckler1
    Participant

    @jsteckler1

    Might it be useful to leverage any of the Gravity Forms “pre” and “after” submission hooks?

    add_action( 'gform_pre_submission', 'pre_submission' );

    add_action( 'gform_after_submission', 'after_submission', 10, 2 );

    Full documentation here and here.


    Robin W
    Moderator

    @robin-w

    it may well be that my code needs debugging, but I have stacks of work os – so very limited in helping you 🙂


    jsteckler1
    Participant

    @jsteckler1

    I completely understand and I am beyond appreciative for the time you have already taken.

    If you get the chance to take another look, fantastic. If not, and you or anyone else can point me in the right direction or take another stab at it, that would be great too.

    Thank you for everything, Robin.


    Robin W
    Moderator

    @robin-w

    did you try the gravity hook ?


    jsteckler1
    Participant

    @jsteckler1

    Ah, sorry! I missed that reply!

    But unfortunately that code broke the site (all good now that I removed the code, of course, so no worries whatsoever).


    Robin W
    Moderator

    @robin-w

    ok, I can’t see any reason why that should not work

    Can you contact me via my website

    http://www.rewweb.co.uk


    jsteckler1
    Participant

    @jsteckler1

    Thanks, Robin. Just reached out.


    Robin W
    Moderator

    @robin-w

    ok, just posting for anyone searching in future

    THIS IS A WORKING VERSION

    This code allows a standard gravity form lets it be saved as a gravity forum, but on saving also creates a new topic form for a specific forum

    add_action( 'gform_after_submission', 'set_post_content', 10, 2 );
    
    function set_post_content( $entry, $form ) {
    	//set the forum ID that the topic will be saved to
    	$forum_ID = '31086' ;
    	
    	if (!function_exists('bbp_insert_topic')) {
                require_once '/includes/topics/functions.php';
            }
    		
    		//getting post
    		$post_form = get_post($entry['post_id']) ;
    		$title = $post_form->post_title;
    		$content = $post_form->post_content;
    
            // The topic data.
            $topic_data = array(
            'post_parent'       => $forum_ID, // forum ID of Projects  hard coded
            'post_status'       => bbp_get_public_status_id(),   // Subscribed users can see this
            'post_content'      => $content,
            'post_title'        => $title,
            'comment_status'    => 'open',
            'menu_order'        => 0
            );
    
            // The topic meta.
            $topic_meta = array(
            'forum_id'    =>  $forum_ID,
            'reply_count' => 0
    		);
    
            $post_id = bbp_insert_topic($topic_data, $topic_meta);
               
    }

    erich199
    Participant

    @erich199

    @robin-w,

    I think this is great but I wanted to ask you about this.

    Is this function for ALL Gravity Form submissions or is it limited to any you choose?
    I’m setting up a site where the Gravity Form information should be created as a topic but I have other Gravity Forms on the site that I don’t need this function for.


    Robin W
    Moderator

    @robin-w

    to only have it for a specified form, you add the form id

    eg

    add_action( 'gform_after_submission_5', 'set_post_content', 10, 2 );

    will make it fire only for gravity form ID 5


    erich199
    Participant

    @erich199

    @robin-w ,
    Thanks so much for the follow up. I looked over the docs last night and saw that. This is really awesome.

    I do have one question.

    When I’m posting a topic from Gravity Forms, for some odd reason it is making duplicate post. Does this happen when you use the code?


    Robin W
    Moderator

    @robin-w

    yes, it makes a gravity post and a topic post.

    You could code to remove the latter as part of this function by adding further code, but in all honesty I wouldn’t worry, you’d need to be creating thousands of topics a month to make an impact on performance


    erich199
    Participant

    @erich199

    Hi @robin-w,

    For some odd reason it’s making two post entries in bbpress. It’s showing duplicate topics on a “Recent Topic” widget and the url’s are different. It’s strange. I can let you take a look at my website if you have time.

    Thanks again for this, it really is great and helps me a lot.


    erich199
    Participant

    @erich199

    Here is an image of what I’m talking about.

    It’s leaving the post that isn’t attached to a forum there.

    https://ibb.co/hzOsVH


    reedy
    Participant

    @reedy

    This is very interesting. Is there any way to incorporate this into a registration form for bbpress so that members can register and post to the bbpress forum at the same time?

    I implemented this as described here, but posts made to bbpress using the registration form are given the username ‘anonymous’. I assume this is because no reference is made to userid in the php snippet — and perhaps also because users do not get logged in upon registration?


    roseh822
    Participant

    @roseh822

    Does anyone know how to

    1. Take the forum list (not the topics) and have them put into a drop down or filtered by one thing or another?
    2. To connect what is selected in the drop down and post the topic in that forum?

    Thanks in advance!

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