1
這是關於wordpress主題開發。
我在類別創建和編輯窗體中添加了兩個自定義字段(文本和上傳圖像)。 編輯表格的效果都很好。 創建表單$ _POST效果很好,但$ _FILES爲空。
我使用Wamp在本地測試Wamp,所以有足夠的空間。
的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 ======*/
我現在修正了這個錯誤,仍然不起作用。我可以以編輯形式上傳,但不能以創建形式工作。我之前使用print_r($ _ FILES),它返回空白,所以實際上它與$ _FILES ['CatMetaImg']沒有任何關係,儘管它是錯誤的。 – GoneWithSin