Hook to allow me to change the "created_by" value before entry is saved to database? [RESOLVED]

I’m using the register user add-on, I’ve got it set up to NOT try to add a user if it already exists, the problem then becomes if the user orders another item but they’re not logged in, the creater of that entry is left blank despite the fact that the user supplied their email address. I do not want to force them to log back in to place an order, I just want to pick up their email address, find the existing user and add the item ordered to the account of that user. I will handle cleanup if they fat-finger their email address.

Is there a hook that will allow me to change the created_by id on an entry during form submission?

This is how you update the created_by property:

`GFAPI::update_entry_property( ` `$entry` `[` `'id'` `], ` `'created_by'` `, ` `$user_id` `);`

You can use that in a gform_after_submission hook, if you want. It’s up to you to determine their user ID. The entry ID is available in the gform_after_submission hook already.

This is what I came up with based on that, it works hand in hand with the register user add-on code snippet that prevents the form from trying to re-register an existing user.

Seems to be working as expected, thanks for the point in the right direction!

add_action( 'gform_after_submission', function( $entry, $form ) {
		if (function_exists( 'gf_user_registration' ) && gf_user_registration()->has_feed_type( 'create', $form )) {
		foreach ( $form['fields'] as $field ) {
			if ( 'email' === $field->type && 'username' === strtolower( $field->adminLabel ) ) {
				if (username_exists( rgpost( 'input_'.$field->id ) )) {
					$user = WP_User::get_data_by('email', rgpost( 'input_'.$field->id ));
					if ($user !== false) {
    					GFAPI::update_entry_property( $entry['id'], 'created_by', $user->ID );
					}				
				}
			}
		}
	}
	return $entry;
} ,10, 2);
2 Likes