这是关于wordpress主题开发的。
我在类别创建和编辑表单中添加了两个自定义字段(文本和上传图像)。所有字段在编辑表单中都很好创建表单$_POST运行良好,但$_FILES为空
我正在Windows上本地测试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 ======*/
$_FILES['CatMetaImg']
正在使用输入id。这不应该是输入名称吗?