自定义元框上载文件


Custom meta box upload file

我有一个插件,它添加了一个带有文件上传表单的元框,该插件工作得很好,在元框中显示图像的链接(查看图像),加载文件,一旦发布帖子,它就会在主题中查看。(在我使用图像文件的示例中):

<?php
/* Plugin Name: Custom Meta Upload Template*/
add_action('post_edit_form_tag', 'post_edit_form_tag');
function post_edit_form_tag() {
echo ' enctype="multipart/form-data"';
}
function custom_upload() {
add_meta_box( 'meta_upload', __( 'Upload Image', 'meta_upload_domain' ),'meta_upload_callback', 'post' );}
add_action( 'add_meta_boxes', 'custom_upload' );
function meta_upload_callback( $post ) {
global $post;
$custom         = get_post_custom($post->ID);
$download_image01    = get_post_meta($post->ID, '_image01', true);
echo '<p><label for="document_file">Upload document:</label><br />';
echo '<input type="file" name="document_file" id="document_file" /></p>';
echo '</p>';
if(!empty($download_image01) && $download_image01 != '0') {
echo '<p><a href="' . wp_get_attachment_url($download_image01) . '">View Image</a></p>';
   }
}
function meta_upload_save( $post_id ) {
global $post;
if(strtolower($_POST['post_type']) === 'page') {
    if(!current_user_can('edit_page', $post_id)) {
        return $post_id;
    }
}
else {
    if(!current_user_can('edit_post', $post_id)) {
        return $post_id;
    }
}
if(!empty($_FILES['document_file'])) {
    $file   = $_FILES['document_file'];
    $upload = wp_handle_upload($file, array('test_form' => false));
    if(!isset($upload['error']) && isset($upload['file'])) {
        $title      = $file['name'];
        $ext        = strrchr($title, '.');
        $title      = ($ext !== false) ? substr($title, 0, -strlen($ext)) : $title;
        $attachment = array(
            'post_mime_type'    => 'image',
            'post_title'        => addslashes($title),
            'post_content'      => '',
            'post_status'       => 'inherit',
            'post_parent'       => $post->ID
        );
        $attach_key = '_image01';
        $attach_id  = wp_insert_attachment($attachment, $upload['file']);
        $existing_download = (int) get_post_meta($post->ID, $attach_key, true);
        if(is_numeric($existing_download)) {
            wp_delete_attachment($existing_download);
        }
        update_post_meta($post->ID, $attach_key, $attach_id);
       }
    }
 }
 add_action( 'save_post', 'meta_upload_save' );

他调用了主题文件:

 <?php 
  $download_image01    = get_post_meta($post->ID, '_image01', true);
  if(!empty($download_image01) && $download_image01 != '0') { ?>
        <div class="section-content">
  <img src="<?php echo wp_get_attachment_url($download_image01); ?>" alt=""  />             
        </div>
<?php  }?>

我重复一遍,插件工作,但问题是,我不能放置一个按钮或复选框来删除文件时,例如,我想编辑帖子,并删除文件,当然,链接"查看图像"消失

任何想法!!

每当您想要编辑这些字段时,echo自定义字段的值如下所示。。。

<input type="" value="<img src="<?php echo wp_get_attachment_url($download_image01); ?>" alt=""  />">

当你编辑帖子时,它会得到值并向你显示上传的图片。

我不知道这是否有帮助,但当我删除自定义帖子时,我使用此代码排除附件:

add_action('before_delete_post', 'delete_all_attached_media');
function delete_all_attached_media($post_id)
{
  if (get_post_type($post_id) == "your-custom-post-type") {
    $attachments = get_attached_media('', $post_id);
    foreach ($attachments as $attachment) {
      wp_delete_attachment($attachment->ID, 'true');
    }
  }
}