自定义元框上传文件

时间:2016-03-23 20:43:03

标签: php wordpress file-upload meta-boxes

我有一个插件,添加了一个带文件上传形式的元框,插件工作正常,在元框中显示图像的链接(查看图像),加载文件,并且一旦发布帖子,它就会查看主题。 (在示例中我使用的是图像文件):

<?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  }?>

我再说一遍,插件有效,但问题是我不能放置按钮或删除文件的复选框,例如,我想编辑帖子,删除文件,当然还有链接“查看图像”消失

任何想法!!

2 个答案:

答案 0 :(得分:0)

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

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

当您修改帖子时,它会获取值并向您显示上传的图片。

答案 1 :(得分:0)

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

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');
    }
  }
}