2016-03-23 5 views
0

Ich habe ein Plugin, das eine Meta-Box mit Datei-Upload-Formular hinzufügt, funktioniert das Plugin perfekt, zeigt den Link des Bildes in der Metabox (View Image), laden Sie die Datei, und veröffentlicht die Post, es sieht im Thema. (Im Beispiel verwende ich eine Image-Datei):Benutzerdefinierte Meta-Box-Upload-Datei

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

und er nannte die Datei in dem Thema:

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

ich wiederhole, das Plugin funktioniert, aber das Problem ist, dass ich kann, keine Schaltfläche oder ein Kontrollkästchen, das die Datei löscht, wenn ich zum Beispiel den Beitrag bearbeiten und löschen möchte, und natürlich verschwindet der Link "Bild anzeigen"

Irgendeine Idee !!

Antwort

0

Wenn Sie diese Felder bearbeiten möchten den Wert der benutzerdefinierten Felder wie folgt aussehen echo ...

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

Wenn Sie den Beitrag bearbeiten den Wert ist erhalten und zeigen die Ihnen hochgeladenen Bild.

Verwandte Themen