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] );
1 Answer
Reset to default 1ACF 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
版权声明:本文标题:custom field - How can I store a file in the database in the same way WordPress and ACF do? 内容由网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://www.betaflare.com/web/1741481806a2381214.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
发表评论