Gravity Forms Submit form as another user

A solution I have found is to manipulate the entry after submission using the following:

function submit_as_user_proxy( $form )
{
    $id = null;
    $proxy_user_meta = null;
    $user_role="";

    $field_existing_users = null;
    $field_signed_up_as = null;

    foreach ( $form['fields'] as &$field )
    {
        if ( false !== strpos( $field->cssClass, 'existing_users' ) )
        {
            $field_existing_users = &$field;
        }
        if ( false !== strpos( $field->cssClass, 'signed_up_as' ) )
        {
            $field_signed_up_as = &$field;
        }
    }

    $id = $_POST[ 'input_' . $field_existing_users->id ];
    $proxy_user_meta = get_userdata( $id );
    $roles = $proxy_user_meta->roles;
    if ( ! empty( $roles ) )
    {
        $user_role = $roles[0];
    }
    $_POST[ 'input_' . $field_signed_up_as->id ] = $user_role;

    $form['useCurrentUserAsAuthor'] = 0;
    $form['postAuthor'] = $id;

    add_action(
        'gform_after_submission_' . $form['id'],
        function( $entry ) use( $id )
        {
            $entry_id = $entry['id'];
            GFAPI::update_entry_property( $entry_id, 'created_by', $id );
            return $entry;
        }
    );

    return $form;
}

Leave a Comment