admin管理员组

文章数量:1289844

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) ".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] );

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

Share Improve this question edited Feb 21, 2018 at 11:26 mikemike asked Feb 21, 2018 at 11:11 mikemikemikemike 2651 gold badge2 silver badges11 bronze badges
Add a comment  | 

1 Answer 1

Reset to default 1

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.

本文标签: custom fieldHow can I store a file in the database in the same way WordPress and ACF do