Skip to content

Instantly share code, notes, and snippets.

@mhasan3
Last active June 13, 2019 12:16
Show Gist options
  • Select an option

  • Save mhasan3/1f6688a140bccbe36e160426cc778a3e to your computer and use it in GitHub Desktop.

Select an option

Save mhasan3/1f6688a140bccbe36e160426cc778a3e to your computer and use it in GitHub Desktop.

Revisions

  1. mhasan3 revised this gist Jun 13, 2019. 1 changed file with 5 additions and 5 deletions.
    10 changes: 5 additions & 5 deletions uploads.html
    Original file line number Diff line number Diff line change
    @@ -1,6 +1,6 @@
    <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>
    <?php if($user->ID){ ?>
    <input type="file" id="userProfileImage" name="userProfileImage" >
    <input id="submit" type="submit" name="submit" value="Save" >
    <?php } ?>
    </form>
  2. mhasan3 revised this gist Jun 13, 2019. 1 changed file with 6 additions and 0 deletions.
    6 changes: 6 additions & 0 deletions uploads.html
    Original file line number Diff line number Diff line change
    @@ -0,0 +1,6 @@
    <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>
  3. mhasan3 created this gist Jun 13, 2019.
    38 changes: 38 additions & 0 deletions uploads.php
    Original file line number Diff line number Diff line change
    @@ -0,0 +1,38 @@
    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);
    }