PLEASE NOTE: These forums are no longer utilized and are provided as an archive for informational purposes only. All support issues will be handled via email using our support ticket system. For more detailed information on this change, please see this blog post.

Save Newly Created User ID in Custom Post Type

  1. I've got a form that uses the User Registration Add-On to create a user account, and also created a Custom Post Type from other form data. One of the fields for the Custom Post Type is the User ID of the user who created the post.

    Since I'm allowing a user to register/subscribe and create the post in one step, I need a way to get the newly-created User ID and save it to the newly-created post. Seems like a gform_user_registered hook might be able to do this, but not sure how to make it all work together.

    Any help is greatly appreciated!

    Posted 11 years ago on Wednesday August 15, 2012 | Permalink
  2. When trying to use hooks like this, it's helpful to dump the $entry object and take a look at the information which is captured. With that, you can see that the post ID is captured, and the hook definitely has the user ID available. So, you can do something like this:

    [php]
    // run with priority 10 and accept 4 parameters
    add_action('gform_user_registered', 'save_user_id_as_custom_field', 10, 4);
    function save_user_id_as_custom_field($user_id, $config, $entry, $user_pass) {
    	// reference: http:// bit [dot] ly/NmVyE0
    	update_post_meta($entry['post_id'], 'meta_key_name_of_your_custom_field', $user_id);
    }

    Add that code to your theme's functions.php file. Be sure to add or remove opening or closing PHP tags as necessary, depending on where you paste it. You will have to change meta_key_name_of_your_custom_field to the real name of your meta key where the CPT stores the user ID.

    Does that work for you?

    Posted 11 years ago on Wednesday August 15, 2012 | Permalink
  3. Worked perfectly! I wasn't sure if the post was created before the user or vice-versa, and if the entry object was available to that hook.

    Thanks for the quick help!

    Posted 11 years ago on Wednesday August 15, 2012 | Permalink
  4. Glad that worked (it was untested, but looked like it might work.)

    Posted 11 years ago on Wednesday August 15, 2012 | Permalink

This topic has been resolved and has been closed to new replies.