Skip to content

Instantly share code, notes, and snippets.

@mhasan3
Last active June 13, 2019 12:16
Show Gist options
  • Save mhasan3/1f6688a140bccbe36e160426cc778a3e to your computer and use it in GitHub Desktop.
Save mhasan3/1f6688a140bccbe36e160426cc778a3e to your computer and use it in GitHub Desktop.
<form name="artist_basic_profile" id="artist_basic_profile" action="" method="post" enctype="multipart/form-data">
<?php if($user->ID){ ?>
<input type="file" id="userProfileImage" name="userProfileImage" >
<input id="submit" type="submit" name="submit" value="Save" >
<?php } ?>
</form>
require_once(ABSPATH . '/wp-load.php');
require_once(ABSPATH . "wp-admin" . '/includes/file.php');
require_once(ABSPATH . "wp-admin" . '/includes/image.php');
$filename = $_FILES['userProfileImage']['name'];
if($filename){
$uploaddir = wp_upload_dir(); // get wordpress upload directory
$myDirPath = $uploaddir['path'];
$myDirUrl = $uploaddir['url'];
$MyImage = rand(0, 5000) . $_FILES['userProfileImage']['name'];
$image_path = $myDirPath . '/' . $MyImage;
move_uploaded_file($_FILES['userProfileImage']['tmp_name'], $image_path);
$file_array = array(
'name' => $_FILES['userProfileImage']['name'],
'type' => $_FILES['userProfileImage']['type'],
'tmp_name' => $_FILES['userProfileImage']['tmp_name'],
'error' => $_FILES['userProfileImage']['error'],
'size' => $_FILES['userProfileImage']['size'],
);
$file = $MyImage;
$uploadfile = $myDirPath . '/' . basename($file);
$filename = basename($uploadfile);
$wp_filetype = wp_check_filetype(basename($filename), null);
$attachment = array(
'post_mime_type' => $wp_filetype['type'],
'post_title' => preg_replace('/\.[^.]+$/', '', $_FILES['userProfileImage']['name']),
'post_content' => '',
'post_status' => 'inherit'
);
$attachment_id = wp_insert_attachment($attachment, $uploadfile);
$attach_data = wp_generate_attachment_metadata($attachment_id, $uploadfile);
$attachimage_url = $uploaddir['url'] . '/' . basename($filename);
wp_update_attachment_metadata($attachment_id, $attach_data);
update_field('profile_image', $attachment_id, "user_" . $current_user_id);
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment