【问题标题】:Custom meta box upload file自定义元框上传文件
【发布时间】:2020-06-12 05:55:30
【问题描述】:

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

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

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

任何想法!

【问题讨论】:

    标签: php wordpress file-upload meta-boxes


    【解决方案1】:

    当您想要编辑这些字段时,自定义字段的值看起来像这样...

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

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

    【讨论】:

      【解决方案2】:

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

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

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 2012-11-28
        • 1970-01-01
        • 2016-08-25
        • 2011-02-25
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多