douou6807
2017-03-28 22:02
浏览 73
已采纳

如何在wordpress中向用户配置文件添加自定义文件字段?

I've found endless examples online on how to add extra custom fields to user profiles in wordpress. But non of them have shown how to add fields to upload files.

Heres what I got for my extra field:

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 Information", "blank"); ?></h3>
    <table class="form-table">

        <tr>
            <th><label for="my_document"><?php _e("My Document"); ?></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
}

Then for the form submit:

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 ( !current_user_can( 'edit_user', $user_id ) )
        return false;

    if(!empty($_FILES['my_document']['name'])) {

        // Use the WordPress API to upload the file
        $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 was an error uploading your file. The error is: ' . $upload['error']);
        } else {
            add_post_meta($user_id, 'my_document', $upload);
            update_post_meta($user_id, 'my_document', $upload);
        } // end if/else

    } // end if

}

The document isn't saving, I suspect the form in the edit profile hasn't the tags to upload files. I also don't know how to retrieve the document in the front end once its saved, as to show the user he has the file uploaded.

图片转代码服务由CSDN问答提供 功能建议

我在网上找到了无数的例子,说明如何在wordpress中为用户配置文件添加额外的自定义字段。 但是他们没有展示如何添加字段来上传文件。

以下是我的额外字段:

  add_action(  'show_user_profile','extra_user_profile_fields'); 
add_action('edit_user_profile','extra_user_profile_fields'); 
 
function extra_user_profile_fields($ user){
?&gt; 
&lt; h3&gt;&lt;?php _e(“Extra 信息“,”空白“);  ?&gt;&lt; / h3&gt; 
&lt; table class =“form-table”&gt; 
 
&lt; tr&gt; 
&lt; th&gt;&lt; label for =“my_document”&gt;&lt;?php  _e(“我的文件”);  ?&gt;&lt; / label&gt;&lt; / th&gt; 
&lt; td&gt; 
&lt; input type =“file”name =“my_document”id =“my_document”value =“&lt;?php echo esc_attr(get_the_author_meta  ('my_document',$ user-&gt; ID));?&gt;“  /&gt; 
&lt; / td&gt; 
&lt; / tr&gt; 
 
&lt; / table&gt; 
 
&lt;?php 
} 
   
 
  

然后对于表单提交:

  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'])&amp;&amp; $ 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 
 
} //结束如果
 
} 
   
 
  

文档未保存,我怀疑编辑个人资料中的表单没有上传文件的标签。 我也不知道如何在保存后检索前端的文档,以便向用户显示他上传的文件。

  • 点赞
  • 写回答
  • 关注问题
  • 收藏
  • 邀请回答

2条回答 默认 最新

  • doubianxian6557 2017-03-29 00:07
    已采纳

    This works for me:

    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 Information", "blank"); ?></h3>
        <table class="form-table">
            <tr>
                <th scope="row">My Document</th>
                <td><input type="file" name="my_document" value="" />
                <?php
                    $doc = get_user_meta( $user->ID, 'my_document', true );
                    if (!isset($doc['error'])) {
                        $doc = $doc['url'];
                        echo "<img src='$doc' />";
                    } else {
                        $doc = $doc['error'];
                        echo $doc;
                    }
                ?>
                </td>
            </tr>
        </table>
    
    <?php
    }
    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 ) {
        if ( !current_user_can( 'edit_user', $user_id ) )
            return false;
        if( $_FILES['my_document']['error'] === UPLOAD_ERR_OK ) {
            $_POST['action'] = 'wp_handle_upload';
            $upload_overrides = array( 'test_form' => false );
            $upload = wp_handle_upload( $_FILES['my_document'], $upload_overrides );
            update_user_meta( $user_id, 'my_document', $upload );
        }
    }
    
    点赞 打赏 评论
  • douliexu5623 2017-03-28 23:15

    What do you see in your console when you dump the output?

    var_dump($upload);
    

    It could be related to the /tmp directory permissions (or existence).

    I apologize if this isn't what you're asking, but have you tried ACF?

    Here is how to get fields from a User field group.

    点赞 打赏 评论

相关推荐 更多相似问题