Custom Image Upload in WordPress

Although WordPress provides a very decent feature to upload and crop images, still we may need to upload our own images in WordPress for some specific reasons. Uploading images in WordPress has been greatly eased by the image unploading functions of WordPress. Today we will see how to upload custom images in WordPress. Let us assume that we are uploading custom images to our posts. So we will need to make a reference of the uploaded image to the post. Here is the strategy for it: We will upload the image as an attachment to the post, so WordPress will return an attachment id to us. We will save this attachment id in the custom field with name ‘custom_image’ and value as the attachment id. So it will become easy to retrieve the custom image any time just by knowing the post id.

HTML Markup:

<p>
<label for="custom-upload">Upload New Image:</label><br />
<input type="file" tabindex="3" name="custom-upload" id="custom-upload" />
</p>
<?php
/*Retrieving the image*/
$attachment = get_post_meta($postid, 'custom_image');

if($attachment[0]!='')
{
echo wp_get_attachment_link($attachment[0], 'thumbnail', false, false);
}

?>

Uploading the image:

<?php
global $post; /*Global post object*/

$post_id = $post->ID; /*Geting current post id*/

$upload = $_FILES['upload']; /*Receive the uploaded image from form*/

add_custom_image($post_id, $upload); /*Call image uploader function*/

function add_custom_image($post_id, $upload)
{
$uploads = wp_upload_dir(); /*Get path of upload dir of wordpress*/

if (is_writable($uploads['path'])) /*Check if upload dir is writable*/
{
if ((!empty($upload['tmp_name']))) /*Check if uploaded image is not empty*/
{
if ($upload['tmp_name']) /*Check if image has been uploaded in temp directory*/
{
$file=handle_image_upload($upload); /*Call our custom function to ACTUALLY upload the image*/

$attachment = array /*Create attachment for our post*/
(
'post_mime_type' => $file['type'], /*Type of attachment*/
'post_parent' => $post_id, /*Post id*/
);

$aid = wp_insert_attachment($attachment, $file['file'], $post_id); /*Insert post attachment and return the attachment id*/
$a = wp_generate_attachment_metadata($aid, $file['file'] ); /*Generate metadata for new attacment*/
$prev_img = get_post_meta($post_id, 'custom_image'); /*Get previously uploaded image*/
if(is_array($prev_img))
{
if($prev_img[0] != '') /*If image exists*/
{
wp_delete_attachment($prev_img[0]); /*Delete previous image*/
}
}
update_post_meta($post_id, 'custom_image', $aid); /*Save the attachment id in meta data*/

if ( !is_wp_error($aid) )
{
wp_update_attachment_metadata($aid, wp_generate_attachment_metadata($aid, $file['file'] ) ); /*If there is no error, update the metadata of the newly uploaded image*/
}
}
}
else
{
echo 'Please upload the image.';
}
}
}

function handle_image_upload($upload)
{
global $post;

if (file_is_displayable_image( $upload['tmp_name'] )) /*Check if image*/
{
/*handle the uploaded file*/
$overrides = array('test_form' => false);
$file=wp_handle_upload($upload, $overrides);
}
return $file;
}
?>

Written by Arvind Bhardwaj

Arvind is a certified Magento 2 expert with more than 10 years of industry-wide experience.

Website: http://www.webspeaks.in/

5 thoughts on “Custom Image Upload in WordPress

  1. Also note, as stated in the file.php that using overrides voids your warranty…

  2. Thanks for this share! I used it for an external Logo-to-profile uploader, outside of the WordPress root.

    // To remove the theme
    define(‘WP_USE_THEMES’, false);

    // To use WordPress posts outside the root dir
    require_once(‘../public_html/wp-blog-header.php’);

    // Needed for the image upload functions
    require_once(‘../public_html/wp-admin/includes/admin.php’);

    And after the upload I made the image the featured, just add after the update_post_meta:

    set_post_thumbnail( $post_id, $aid );

Comments are closed.