2017-11-13 16 views
0

カスタムpost_typeがcircularであるプラグインを作成しています。メタボックスを使用してPDFまたは画像ファイルをアップロードしました。私のカスタムpost_typeメタフィールドからファイル名とサイズを取得する方法はありますか?あなたがする必要があります添付ファイルID番号($は以下attachment_id)を得ることができる場合はここでカスタム投稿タイプの添付ファイルのファイル名とファイルサイズを取得する方法

は私のメタボックスコードが

function add_custom_meta_boxes() { 

    // Define the custom attachment for posts 
    add_meta_box(
     'wp_custom_attachment', 
     'Custom Attachment', 
     'wp_custom_attachment', 
     'circular', 
     'side' 
    ); 

} // end add_custom_meta_boxes 
add_action('add_meta_boxes', 'add_custom_meta_boxes'); 

function wp_custom_attachment() { 

    wp_nonce_field(plugin_basename(__FILE__), 'wp_custom_attachment_nonce'); 

    $html = '<p class="description">'; 
     $html .= 'Upload your PDF here.'; 
    $html .= '</p>'; 
    $html .= '<input type="file" id="wp_custom_attachment" name="wp_custom_attachment" value="" size="25" />'; 

    echo $html; 

} // end wp_custom_attachment 


function save_custom_meta_data($id) { 

    /* --- security verification --- */ 
    if(!wp_verify_nonce($_POST['wp_custom_attachment_nonce'], plugin_basename(__FILE__))) { 
     return $id; 
    } // end if 

    if(defined('DOING_AUTOSAVE') && DOING_AUTOSAVE) { 
     return $id; 
    } // end if 

    if('circular' == $_POST['post_type']) { 
     if(!current_user_can('edit_page', $id)) { 
     return $id; 
     } // end if 
    } else { 
     if(!current_user_can('edit_page', $id)) { 
      return $id; 
     } // end if 
    } // end if 
    /* - end security verification - */ 

    // Make sure the file array isn't empty 
    if(!empty($_FILES['wp_custom_attachment']['name'])) { 

     // Setup the array of supported file types. In this case, it's just PDF. 
     $supported_types = array('application/pdf'); 

     // Get the file type of the upload 
     $arr_file_type = wp_check_filetype(basename($_FILES['wp_custom_attachment']['name'])); 
     $uploaded_type = $arr_file_type['type']; 

     // Check if the type is supported. If not, throw an error. 
     if(in_array($uploaded_type, $supported_types)) { 

      // Use the WordPress API to upload the file 
      $upload = wp_upload_bits($_FILES['wp_custom_attachment']['name'], null, file_get_contents($_FILES['wp_custom_attachment']['tmp_name'])); 

      if(isset($upload['error']) && $upload['error'] != 0) { 
       wp_die('There was an error uploading your file. The error is: ' . $upload['error']); 
      } else { 
       add_post_meta($id, 'wp_custom_attachment', $upload); 
       update_post_meta($id, 'wp_custom_attachment', $upload);  
      } // end if/else 

     } else { 
      wp_die("The file type that you've uploaded is not a PDF."); 
     } // end if/else 

    } // end if 

} // end save_custom_meta_data 
add_action('save_post', 'save_custom_meta_data'); 

function update_edit_form() { 
    echo ' enctype="multipart/form-data"'; 
} // end update_edit_form 
add_action('post_edit_form_tag', 'update_edit_form'); 

で、そのファイルのリンク

<?php $img = get_post_meta(get_the_ID(), 'wp_custom_attachment', true); ?> 
<a href="<?php echo $img['url']; ?>"> Download PDF Here</a> 

答えて

1

最初にファイルのURLを取得する必要があります私たちはサイズと名前を取得することができます。ここでwp_custom_attachmentはカスタムフィールドIDです。

// retrieve file of the custom field 
$file = get_post_meta(get_the_ID(), 'wp_custom_attachment', true); 

//get the url 
$url = $file['url']; 

//Replace url to directory path 
    $path = str_replace(site_url('/'), ABSPATH, esc_url($url)); 

    if (is_file($path)){ 
    $filesize = size_format(filesize($path)); 
    $filename = basename($path); 
    } 

      echo '<p>Name: ' . $filename . '</p>'; 
      echo '<p>Size: ' . $filesize . '</p>'; 
+0

'get_post_meta()'に '$ single'パラメータがtrueに設定されています。これは" [meta dataフィールドの値](https://developer.wordpress.org/reference/functions/get_post_meta/#return)の値を返します。 "私は '$ file'変数が画像ID番号かもしれないと思います。 'get_post_meta()'が返すものを見るために 'var_dump($ file)'を試すことができますか? –

+0

'size_format'はファイルサイズを正しく返す関数です – Firefog

+0

私は前の答えで述べた' getSize() '関数を試しましたか? –

0

アウトを入れてください名前/サイズを得るためにこのようなことをすることができます:

$attachment_id = 'YOUR_PDF_ID'; 
$attahment_file = get_attached_file($attachment_id); 

function getSize($file) { 
    $bytes = filesize($file); 
    $s = array('b', 'Kb', 'Mb', 'Gb'); 
    $e = floor(log($bytes)/log(1024)); 
    return sprintf('%.2f ' . $s[$e], ($bytes/pow(1024, floor($e)))); 
} 

echo '<p>Name: ' . basename($attahment_file) . '</p>'; 
echo '<p>Size: ' . getSize($attahment_file) . '</p>'; 

別の投稿hereに「getSize」関数が見つかりました。これは、WPメディアライブラリのメタに示されているサイズにマッチするという点で、ネイティブのPHPの "filesize"関数を使うよりも正確です。

+0

回答ありがとうございますが動作していません – Firefog

+0

どの部分が機能していませんか?私はそれを掲示する前にコードをテストしたと信じている、私はデバッグを助けることができるように、より多くの情報を与えることができますか? –

+0

私はあなたのコードを使用しましたが、その戻り値はnullなので、なぜカスタムポストタイプなのでしょうか?またはあなたのコード '$ attahment_file'は配列でなければならず、$ getSize($ file)'ここに$ファイルはありませんあなたのコードを修正しようとしましたが失敗しました。しかし、ベローの答えは完璧に働いています。サポート担当者に感謝します。 – Firefog

関連する問題