• omidparkour

    (@omidparkour)


    Hi guys
    i want to create a field in user profile to upload image for use in author page .
    and so i write the following codes but image url insert in database but photo can not be saved in upload folder
    please help me!

    add_action( ‘show_user_profile’, ‘my_show_extra_profile_fields’ );
    add_action( ‘edit_user_profile’, ‘my_show_extra_profile_fields’ );

    function my_show_extra_profile_fields( $user ) { ?>

    <h3>Extra profile information</h3>

    <table class=”form-table”>

    <tr>
    <th><label for=”Linkedin”>Header Photo</label></th>

    <td>
    <input type=”file” name=”Header” id=”Header” value=”<?php echo esc_attr( get_the_author_meta( ‘Header’, $user->ID ) ); ?>” class=”regular-text” />
    <span class=”description”>عکس هدر صفحه پروفایل خود را آپلود کنید</span>
    </td>
    </tr>
    </table>
    <?php }
    add_action( ‘personal_options_update’, ‘my_save_extra_profile_fields’ );
    add_action( ‘edit_user_profile_update’, ‘my_save_extra_profile_fields’ );

    function my_save_extra_profile_fields( $user_id ) {
    $uploadUrl= wp_get_upload_dir();
    if ( !current_user_can( ‘edit_user’, $user_id ) )
    return false;

    /* Copy and paste this line for additional fields. Make sure to change ‘twitter’ to the field ID. */
    update_usermeta( $user_id, ‘Header’, $uploadUrl[‘url’].”/”.$_POST[‘Header’] );
    }

Viewing 5 replies - 1 through 5 (of 5 total)
  • Hello,

    To upload a file programmatically into the upload dir you should use wp_handle_upload

    Thread Starter omidparkour

    (@omidparkour)

    More Help Please !!!
    I can not use this function

    Hello omidparkour,

    I had a closer look on this and I created a little snippet to store the user “avatar” into the upload dir, save it as user meta ( you should change the key of the meta ) and also show it on user profile.

    First of all, you had not able to store the file into the upload dir because the profile form is not multipart, so I added an additional hook

    function my_profile_multipart() {
        echo 'enctype="multipart/form-data"';
    }
    add_action('user_edit_form_tag', 'my_profile_multipart' );

    With that we can use the global var $_FILES to move the file into the upload dir.

    For the path for the file I used uploads/users-avatar/{user_login} and for the file name the name of the original file. I think you should consider to use always the same name to prevent user to upload multiple files.

    Here the code https://codepad.co/snippet/xjVvxCJ4 tested in a unix local server and WordPress version 4.5.2

    Let me know if you encounter some issue.

    Alternatively there are a bounce of plugins that add this feature.

    Thread Starter omidparkour

    (@omidparkour)

    thank you so much 🙂 :*

    I have the same issue. Here’s my code which is not working:

    function my_profile_multipart() {
    echo ‘enctype=”multipart/form-data”‘;
    }
    add_action(‘user_edit_form_tag’, ‘my_profile_multipart’ );

    function my_show_extra_profile_fields( $user ) { ?>

    <h3>Credentials</h3>
    <table class=”form-table”>
    <tr>
    <th><label for=”drivers_license”>Driver’s License</label></th>

    <td>
    ID ) ?>”/>
    <input type=”file” name=”drivers_license” id=”drivers_license” value=”<?php echo esc_attr( get_the_author_meta( ‘drivers_license’, $user->ID ) ); ?>” />
    </td>
    </tr>
    <tr>
    <th><label for=”state_credentials”>State Credentials</label></th>

    <td>
    <input type=”file” name=”state_credentials” id=”state_credentials” value=”<?php echo esc_attr( get_the_author_meta( ‘state_credentials’, $user->ID ) ); ?>” />
    </td>
    </tr>
    </table>

    <?php }
    add_action( ‘personal_options_update’, ‘my_save_extra_profile_fields’ );
    add_action( ‘edit_user_profile_update’, ‘my_save_extra_profile_fields’ );

    function my_save_extra_profile_fields( $user_id ) {

    if ( !current_user_can( ‘edit_user’, $user_id ) )
    return false;

    $uploadUrl = wp_get_upload_dir();
    $drivers_license = $_POST[“drivers_license”];
    $state_credentials = $_POST[“state_credentials”];
    update_usermeta( $user_id, ‘drivers_license’, $uploadUrl[‘url’].’/agents/’.$drivers_license );
    update_usermeta( $user_id, ‘state_credentials’, $uploadUrl[‘url’].’/agents/’.$state_credentials );
    }
    add_action( ‘show_user_profile’, ‘my_show_extra_profile_fields’ );
    add_action( ‘edit_user_profile’, ‘my_show_extra_profile_fields’ );

    • This reply was modified 9 years, 5 months ago by Michele.
Viewing 5 replies - 1 through 5 (of 5 total)

The topic ‘save image in upload folder’ is closed to new replies.