2016-05-14 2 views
1

Речь идет о разработке темы для Wordpress.

Я добавил два пользовательских поля (текст и загружаю изображение) в категории, чтобы создать и изменить форму. Все хорошо работает в форме редактирования. В создании формы $ _POST хорошо работает, но $ _FILES пуст.

Я тестирую с помощью Wamp на Windows локально, поэтому достаточно места.

php.ini

;;;;;;;;;;;;;;;; 
; File Uploads ; 
;;;;;;;;;;;;;;;; 

; Whether to allow HTTP file uploads. 
; http://php.net/file-uploads 
file_uploads = On 

; Temporary directory for HTTP uploaded files (will use system default if not 
; specified). 
; http://php.net/upload-tmp-dir 
upload_tmp_dir = "D:/Program Files/wamp/tmp" 

; Maximum allowed size for uploaded files. 
; http://php.net/upload-max-filesize 
upload_max_filesize = 64M 

; Maximum number of files that can be uploaded via a single request 
max_file_uploads = 20 

post_max_size = 64M 

functions.php

/*===================================== 
=   category form   = 
=====================================*/ 

// add file upload permission to category edit form 
add_action ('category_edit_form', 'edit_form_tag'); 
function edit_form_tag() { 
    ?> 
    <script type="text/javascript"> 
     jQuery(document).ready(function(){ 
      jQuery('#edittag').attr("enctype", "multipart/form-data"); 
     }); 
    </script> 
    <?php 
} 

add_action ('category_add_form', 'create_form_tag'); 
function create_form_tag() { 
    ?> 
    <script type="text/javascript"> 
     jQuery(document).ready(function(){ 
      jQuery('#addtag').attr("enctype", "multipart/form-data"); 
     }); 
    </script> 
    <?php 
} 


//add extra fields to category create form hook 
add_action ('category_add_form_fields', 'create_extra_category_fields'); 
//add extra fields to category create form callback function 
function create_extra_category_fields($tag) { //check for existing featured ID 
    $t_id = $tag->term_id; 
    $cat_meta = get_option("category_{$t_id}"); 
    ?> 
    <div class="form-field"> 
     <label for="cat_style_type"><?php _e('Category Style Type'); ?></label> 
     <select name="Cat_meta[cat_style_type]" id="cat_style_type" > 
      <option value="text" <?php echo ($cat_meta['cat_style_type'] == 'text') ? 'selected' : ''; ?>><?php _e('text'); ?></option> 
      <option value="news" <?php echo ($cat_meta['cat_style_type'] == 'news') ? 'selected' : ''; ?>><?php _e('news'); ?></option> 
     </select> 
     <p class="description"><?php _e('Required. Category style type: text or news.'); ?></p> 
    </div> 
    <div class="form-field"> 
     <label for="cat_Image_url"><?php _e('Category Image Url'); ?></label> 
     <input type="file" name="cat_img" id="cat_Image_url" ><br /> 
     <p class="description"><?php _e('Image for category. If category\'s style is text, it\' optional; if category\'s style is news, it\'s required.'); ?></p> 
     <img src="<?php echo $cat_meta['cat_img'] ? $cat_meta['cat_img'] : ''; ?>" style="max-width: 300px;height: auto;margin-top: 10px;"> 
    </div> 
    <?php 
} 

//add extra fields to category edit form hook 
add_action ('category_edit_form_fields', 'edit_extra_category_fields'); 
//add extra fields to category edit form callback function 
function edit_extra_category_fields($tag) { //check for existing featured ID 
    $t_id = $tag->term_id; 
    $cat_meta = get_option("category_{$t_id}"); 
    ?> 
    <tr class="form-field"> 
     <th scope="row" valign="top"><label for="cat_style_type"><?php _e('Category Style Type'); ?></label></th> 
     <td> 
      <select name="Cat_meta[cat_style_type]" id="cat_style_type" > 
       <option value="text" <?php echo ($cat_meta['cat_style_type'] == 'text') ? 'selected' : ''; ?>><?php _e('text'); ?></option> 
       <option value="news" <?php echo ($cat_meta['cat_style_type'] == 'news') ? 'selected' : ''; ?>><?php _e('news'); ?></option> 
      </select> 
      <p class="description"><?php _e('Required. Category style type: text or news.'); ?></p> 
     </td> 
    </tr> 
    <tr class="form-field"> 
     <th scope="row" valign="top"><label for="cat_Image_url"><?php _e('Category Image Url'); ?></label></th> 
     <td> 
      <input type="file" name="cat_img" id="cat_Image_url" ><br /> 
      <p class="description"><?php _e('Image for category. If category\'s style is text, it\' optional; if category\'s style is news, it\'s required.'); ?></p> 
      <img src="<?php echo $cat_meta['cat_img'] ? $cat_meta['cat_img'] : ''; ?>" style="max-width: 300px;height: auto;margin-top: 10px;"> 
     </td> 
    </tr> 
    <?php 
} 

// save extra category extra fields hook 
add_action ('create_category', 'save_extra_category_fileds'); 
add_action ('edited_category', 'save_extra_category_fileds'); 
// save extra category extra fields callback function 
function save_extra_category_fileds($term_id) { 
    if (isset($_POST['Cat_meta'])) { 
     $t_id = $term_id; 
     $cat_meta = get_option("category_{$t_id}"); 

     $cat_meta['cat_style_type'] = $_POST['Cat_meta']['cat_style_type']; 

     if (! function_exists('wp_handle_upload')) { 
      require_once(ABSPATH . 'wp-admin/includes/file.php'); 
     } 

     $uploadedfile = $_FILES['cat_img']; 

     $movefile = wp_handle_upload($uploadedfile, array('test_form' => false)); 

     if ($movefile && ! isset($movefile['error'])) { 
      $cat_meta['cat_img'] = $movefile['url']; 
     } else { 
      echo $movefile['error']; 
     } 

     // save the option array 
     update_option("category_{$t_id}", $cat_meta); 
    } 
} 

/*===== End of category form ======*/ 

ответ

0

$_FILES['CatMetaImg'] использует входной идентификатор. Разве это не должно быть имя входа?

+0

Я исправил эту ошибку сейчас, все еще не работает. Я могу загрузить в форме редактирования, но не работать в форме создания. И раньше я использовал print_r ($ _ FILES), он возвращает пустой, так что на самом деле это не имеет никакого отношения к $ _FILES ['CatMetaImg'], хотя это неправильно. – GoneWithSin