Ask your WordPress questions! Pay money and get answers fast! Comodo Trusted Site Seal
Official PayPal Seal

SOLVED MYSELF If field is empty/filled - rewrite old/update WordPress

  • REFUNDED

I was getting some help on the gravity forms forum about a file upload (profile pic) I am using (courtesy of Arnav on this site) and want to add a check function to that. The file upload currently will update the post_meta everytime, regardless if the user uploads a file or not--so essentially the user would need to upload their profile pic every time they update their profile (no bueno). So for the file upload box I would like to add a check that essentially is if field is empty then rewrite the old data (missing part), if not update post meta with the new file (that part works already).

Here is gravity forms forum conversation for reference: [[LINK href="http://www.gravityhelp.com/forums/topic/pre-populate-file-upload"]]LINK[[/LINK]]

Here is the file:

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']]));

}


--The check should go in those last 4 lines


--------SOLVE ON OWN SOLUTION-------
Found the empty() function and that did the trick [[LINK href="http://www.dreamincode.net/forums/topic/79508-how-do-i-check-for-an-empty-field-in-a-form/"]]http://www.dreamincode.net/forums/topic/79508-how-do-i-check-for-an-empty-field-in-a-form/[[/LINK]]

Final Code for the bottom function above:

function GF_update_profile_photo ($entry, $form)
{
global $wpdb;

$gf_fields = GF_get_profile_photo_fields();
$photofield= $entry[$gf_fields['charter-photo']['gf_index']];

if (empty($photofield)) {

echo '';

} else {

update_post_meta(2, 'wpcf-profile-photo', sanitize_text_field($entry[$gf_fields['charter-photo']['gf_index']]));

}}

Answers (2)

2012-08-24

Pavel Petrov answers:

Can you insert
print_r($gf_fileds);
after
global $wpdb;
submit a form with empty photo and post the result.


Kyle comments:

Thanks for the reply

It was unsuccessful, it overrode the file


Pavel Petrov comments:

print_r won`t fix anything it will just tell me what the fields object contains, so that I can tell you how to check if the field is empty


Kyle comments:

Okay, it did not display anything

Since the hook executes after submission I think it is being overridden

2012-08-24

Arnav Joy answers:

is this the same site where i worked ?


Kyle comments:

Yes it is


Arnav Joy comments:

can you provide me access to the site?