Hello,
To upload a file programmatically into the upload dir you should use wp_handle_upload
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.
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.