1

I'm taking input from a Caldera Forms form which includes a user upload. This is returned as an array containing the file name:

["company_logo"]=>
array(1) {
  [0]=>
  string(73) "https://example.com/wp-content/uploads/2018/02/logo-1.png"
}

I'm using ACF to manage the data elsewhere, which effectively just provides a nice way to get and update the data.

How can I store this in the same way WordPress does? I believe WordPress handles it in a very different way than just storing a URL, as resizing takes places and ACF returns various different sizes of the images to me.

Do I need to create an attachment?

Currently I'm adding it in the following way, which does save it as expected, but just as a text string. I need it to save in the same way that WordPress/ACF saves uploaded files when uploaded via admin area.

update_user_meta( $organisation_id, 'company_logo', $data['company_logo'][0] );

0

ACF doesn't store any file in Database, it simply provides meta fields with user friendly interface.

You can however, store them yourself. To store a field, you need to be familiar with PHP a bit. Here's how saving a meta field happens, by using the add_post_meta() function:

add_post_meta ( $post_id, 'meta_key', 'meta_value', $is_unique );

There are other functions such as update_post_meta() for various usage. If your forms contains a file, you can handle the upload by using the media_handle_sideload().

Let's say you have a form like this:

<form method="POST" enctype="multipart/form-data">

    <input type="file" name="some-name"/>

</form>

and you want to save the uploaded file as an attachment. I'm ignoring security and checkup ( they can be found in the codex ) and directly going on how to do it.

You can use the mentioned function to handle the uploaded image, and store the uploaded attachment's ID:

$image_id = media_handle_upload( 'some-name', $post_id );

update_user_meta( $organisation_id, 'company_logo', $image_id  );

Now, you have access to attachment's ID, which you can use to retrieve any size of the attachment.

  • Well I have code which is saving it as a text field, which isn't what I'm after, I need it saving as an attachment in the same way ACF would save it from an admin upload. I'll update question. – mikemike Feb 21 '18 at 11:25
  • @mikemike I've updated the answer for you. – Jack Johansson Feb 21 '18 at 11:37
  • Thanks, that's kind fo what I'm after except I don't have the field name to play with. At this point the file has already been uploaded and I'm left with the source URL only. There is no attachment ID. Am I able to get the attachment ID from the URL or create one? – mikemike Feb 21 '18 at 11:47
  • 1
    You can use the attachment_url_to_postid(), in such cases. – Jack Johansson Feb 21 '18 at 11:49

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.