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.

Pre Populate File Upload

  1. Is it possible to pre-populate a file upload field?

    I have an gravity forms manage a profile with lots of custom fields by having an 'edit profile' that goes is pre-populated with the info, then after submission runs update_post_meta on all the fields. I have a file upload box for the profile photo, and it works with updating the custom profile photo field, but it can't pre-populate with it--so every time a user updates their profile they need to re-upload the photo. Is there a way to pre-populate file uploads?

    Posted 11 years ago on Thursday August 16, 2012 | Permalink
  2. Can you post the code you're using to update the post meta? Maybe we can work around it and not blank out the profile photo and not require the user to upload the same photo every time.

    I'm not sure without looking into it how to pre-populate an upload dialog. I'm inclined to say it's not possible. However, you could probably display the current profile photo, and offer a checkbox to ask "update your profile photo" and when checked, show a file upload dialog and use that value to update the profile. If that box is not checked, if you have to update all the profile fields, maybe you just update it with the same information?

    Posted 11 years ago on Friday August 17, 2012 | Permalink
  3. Here is the code:

    GF_profile_photo_actions_filters();
    function GF_profile_photo_actions_filters() {
        $_gf_profile_photo_id =  RGFormsModel::get_form_id('Change Profile Photo');
        add_action('gform_after_submission_' . $_gf_profile_photo_id, 'GF_update_profile_photo', 100, 2);
      }
    
    function GF_get_profile_photo_fields ()
    {
        $_fields['charter-photo'] = array ( 'gf_index' => '1', 'wp_meta' => 'wpcf-profile-photo' );
        return $_fields;
    }
    
    function GF_update_profile_photo ($entry, $form)
    {
      global $wpdb;
    
      $gf_fields = GF_get_profile_photo_fields();
    
      update_post_meta(2, 'wpcf-profile-photo', sanitize_text_field($entry[$gf_fields['charter-photo']['gf_index']]));
    
    }

    My thought was to do a check on the update_post_meta line alongs the lines of: if the entry is blank then exit else update_post_meta

    Posted 11 years ago on Friday August 17, 2012 | Permalink
  4. My thought was to do a check on the update_post_meta line alongs the lines of: if the entry is blank then exit else update_post_meta

    Cam you explain more what you mean here? I was thinking more like this:

    If the existing listing contains a post image, display the image. Offer the visitor the ability to upload a new photo. If one is uploaded, update the profile. If not, rewrite the original information to the profile.

    Are we on the same page basically?

    Posted 11 years ago on Monday August 20, 2012 | Permalink
  5. Yes, I think we are. I believe you are saying to pre-populate the original information and rewrite if no change. While I said to leave it blank and only rewrite if it is not-blank. So same result different path.

    With that, I could not get the file upload box to prepopulate with my function that worked on normal text boxes:

    function GF_prepopulate_profile_photo($form)
      {
     foreach($form["fields"] as &$field){
    	if($field["id"] == 1){
    		$field["defaultValue"] = get_post_meta(2, 'wpcf-profile-photo', true);
    	}
     }
     return $form;
    }
    Posted 11 years ago on Monday August 20, 2012 | Permalink
  6. I don't think you will be able to pre-populate the file upload dialog. It's a security thing with the browser. See this discussion: http://stackoverflow.com/questions/4684013/is-it-possible-to-re-populate-a-file-select-form-field-with-what-the-user-previo

    However, I was not suggesting you pre-populate the file upload dialog. I was suggesting displaying the image, and if there is a change, maybe have a file upload dialog as a non-required field, with some explanatory text that the visitor can update their profile picture if they would like, by selecting a new file from their computer.

    Then, when you write the values, if the file upload field is empty, rewrite the old data (which is a path on your server, not a path on their personal computer.) If the file upload field contains data, then upload this profile field with the new image information.

    Does that make sense?

    Posted 11 years ago on Tuesday August 21, 2012 | Permalink
  7. Yes, that sounds like it would work perfectly. Do you know how to check a field for data? I have never used a function like that, like if empty {} if data {}

    Posted 11 years ago on Tuesday August 21, 2012 | Permalink