我在网上找到了无数的例子,说明如何在wordpress中为用户配置文件添加额外的自定义字段。 但是他们没有展示如何添加字段来上传文件。 p>
以下是我的额外字段: p>
add_action( 'show_user_profile','extra_user_profile_fields');
add_action('edit_user_profile','extra_user_profile_fields');
function extra_user_profile_fields($ user){
?>
< h3><?php _e(“Extra 信息“,”空白“); ?>< / h3>
< table class =“form-table”>
< tr>
< th>< label for =“my_document”><?php _e(“我的文件”); ?>< / label>< / th>
< td>
< input type =“file”name =“my_document”id =“my_document”value =“<?php echo esc_attr(get_the_author_meta ('my_document',$ user-> ID));?>“ />
< / td>
< / tr>
< / table>
<?php
}
code> pre>
然后对于表单提交: p>
add_action('personal_options_update','yoursite_save_extra_user_profile_fields');
add_action('edit_user_profile_update','yoursite_save_extra_user_profile_fields');
function yoursite_save_extra_user_profile_fields($ user_id){
$ saved = false;
if if(!current_user_can('edit_user',$ user_id))
返回false;
if(!empty($ _ FILES ['my_document' ] ['name'])){
//使用WordPress API上传文件
$ upload = wp_upload_bits($ _ FILES ['my_document'] ['name'],null,file_get_contents($ _ FILES [ 'my_document'] ['tmp_name']));
if(isset($ upload ['error'])&& $ upload ['error']!= 0){
wp_die('There 上传文件时出错。错误是:'。$ upload ['error']);
} else {
add_post_meta($ user_id,'my_document',$ upload);
update_po st_meta($ user_id,'my_document',$ upload);
} //结束if / else
} //结束如果
}
code> pre>
文档未保存,我怀疑编辑个人资料中的表单没有上传文件的标签。 我也不知道如何在保存后检索前端的文档,以便向用户显示他上传的文件。 p>
div>