如何在 Wordpress 的 post 类型中插入附件和更新自定义字段
How to insert an attachment and update a custom field in a post type in Wordpress
我有一个名为 ['notifications'] 的自定义 post 类型和一个名为 ['attachment'] 的自定义字段,用于 ['notifications' 中的所有 post ].
- 我希望用户从前端上传一个附件到库中
- 如果上传成功
- 获取附件的文件名
- 然后在自定义 post 类型 ['notifications'] 中通过其 ID
更新 post
- 将 post 中的自定义字段 ['attachment'] 更新为文件名
自定义字段的 meta_key 是 ['_ct_text_57fc8ec4573cd']
这是我目前所拥有的
前端
<?php
if (isset($_POST['upload'])) {
if (!empty($_FILES)) {
$file = $_FILES['file'];
$attachment_id = upload_user_file($file);
}
}
?>
<form action="" enctype="multipart/form-data" method="post">
<input name="file" type="file">
</form>
内部 FUNCTIONS.PHP
function upload_user_file($file = array()) {
require_once(ABSPATH. 'wp-admin/includes/admin.php');
$file_return = wp_handle_upload($file, array('test_form' => false));
if (isset($file_return['error']) || isset($file_return['upload_error_handler'])) {
return false;
} else {
$filename = $file_return['file'];
$post_ID_attachment = 33;
$attachment = array('post_mime_type' => $file_return['type'],
'post_title' => $post_ID_attachment,
'post_content' => '',
'post_status' => 'inherit',
'guid' => $file_return['url']
);
$attachment_id = wp_insert_attachment($attachment, $file_return['url']);
require_once(ABSPATH. 'wp-admin/includes/image.php');
$attachment_data = wp_generate_attachment_metadata($attachment_id, $filename);
wp_update_attachment_metadata($attachment_id, $attachment_data);
if (0 < intval($attachment_id)) {
return $attachment_id;
}
/* UPDATE ATTACHMENT BELOW*/
update_post_meta($post_ID_attachment, '_ct_text_57fc8ec4573cd', $filename);
}
return false;
}
不确定我是否做对了。
上面的代码成功插入了附件,但它没有更新 post 类型 ['notifications']
中的自定义字段
您的函数中有一个 return 语句,在 update_post_meta
查询之前。试试下面的代码:
function upload_user_file($file = array()) {
require_once(ABSPATH. 'wp-admin/includes/admin.php');
$file_return = wp_handle_upload($file, array('test_form' => false));
if (isset($file_return['error']) || isset($file_return['upload_error_handler'])) {
return false;
} else {
$filename = $file_return['file'];
$post_ID_attachment = 33;
$attachment = array('post_mime_type' => $file_return['type'],
'post_title' => $post_ID_attachment,
'post_content' => '',
'post_status' => 'inherit',
'guid' => $file_return['url']
);
$attachment_id = wp_insert_attachment($attachment, $file_return['url']);
require_once(ABSPATH. 'wp-admin/includes/image.php');
$attachment_data = wp_generate_attachment_metadata($attachment_id, $filename);
wp_update_attachment_metadata($attachment_id, $attachment_data);
/* UPDATE ATTACHMENT BELOW*/
update_post_meta($post_ID_attachment, '_ct_text_57fc8ec4573cd', $filename);
if (0 < intval($attachment_id)) {
return $attachment_id;
}
}
return false;
}
此外,我不确定您的代码中是否需要这些 require_once
-s,因为它在 function.php
中,所有内容都应该加载。
我有一个名为 ['notifications'] 的自定义 post 类型和一个名为 ['attachment'] 的自定义字段,用于 ['notifications' 中的所有 post ].
- 我希望用户从前端上传一个附件到库中
- 如果上传成功
- 获取附件的文件名
- 然后在自定义 post 类型 ['notifications'] 中通过其 ID 更新 post
- 将 post 中的自定义字段 ['attachment'] 更新为文件名
自定义字段的 meta_key 是 ['_ct_text_57fc8ec4573cd']
这是我目前所拥有的
前端
<?php
if (isset($_POST['upload'])) {
if (!empty($_FILES)) {
$file = $_FILES['file'];
$attachment_id = upload_user_file($file);
}
}
?>
<form action="" enctype="multipart/form-data" method="post">
<input name="file" type="file">
</form>
内部 FUNCTIONS.PHP
function upload_user_file($file = array()) {
require_once(ABSPATH. 'wp-admin/includes/admin.php');
$file_return = wp_handle_upload($file, array('test_form' => false));
if (isset($file_return['error']) || isset($file_return['upload_error_handler'])) {
return false;
} else {
$filename = $file_return['file'];
$post_ID_attachment = 33;
$attachment = array('post_mime_type' => $file_return['type'],
'post_title' => $post_ID_attachment,
'post_content' => '',
'post_status' => 'inherit',
'guid' => $file_return['url']
);
$attachment_id = wp_insert_attachment($attachment, $file_return['url']);
require_once(ABSPATH. 'wp-admin/includes/image.php');
$attachment_data = wp_generate_attachment_metadata($attachment_id, $filename);
wp_update_attachment_metadata($attachment_id, $attachment_data);
if (0 < intval($attachment_id)) {
return $attachment_id;
}
/* UPDATE ATTACHMENT BELOW*/
update_post_meta($post_ID_attachment, '_ct_text_57fc8ec4573cd', $filename);
}
return false;
}
不确定我是否做对了。 上面的代码成功插入了附件,但它没有更新 post 类型 ['notifications']
中的自定义字段您的函数中有一个 return 语句,在 update_post_meta
查询之前。试试下面的代码:
function upload_user_file($file = array()) {
require_once(ABSPATH. 'wp-admin/includes/admin.php');
$file_return = wp_handle_upload($file, array('test_form' => false));
if (isset($file_return['error']) || isset($file_return['upload_error_handler'])) {
return false;
} else {
$filename = $file_return['file'];
$post_ID_attachment = 33;
$attachment = array('post_mime_type' => $file_return['type'],
'post_title' => $post_ID_attachment,
'post_content' => '',
'post_status' => 'inherit',
'guid' => $file_return['url']
);
$attachment_id = wp_insert_attachment($attachment, $file_return['url']);
require_once(ABSPATH. 'wp-admin/includes/image.php');
$attachment_data = wp_generate_attachment_metadata($attachment_id, $filename);
wp_update_attachment_metadata($attachment_id, $attachment_data);
/* UPDATE ATTACHMENT BELOW*/
update_post_meta($post_ID_attachment, '_ct_text_57fc8ec4573cd', $filename);
if (0 < intval($attachment_id)) {
return $attachment_id;
}
}
return false;
}
此外,我不确定您的代码中是否需要这些 require_once
-s,因为它在 function.php
中,所有内容都应该加载。