Back to Top

How to use media upload in WordPress form?

To change File/Image Upload Path in Wordpress

I’ve been receiving a lot of requests for a tutorial showing How to upload image WordPress form so I finally gave in. Hopefully, this tutorial will save you the same confusion I suffered.

WordPress provides many functions which make any functionality easy and robust.Here same for image uploading functionality, we can easily manage using WordPress functions.

Read: Tags in WordPress

First of all let’s write basic HTML code which gives interface to choose image/file

`     
class="submit" name="submit" value="" />

You can write above code into any template file you have created into your theme.

Now the most important, PHP code which will upload image/file into uploads folder of WordPress which is in wp-contents.

if (isset($_POST) && $_POST['submit'] != '') {

    if (isset($_FILES['profile-image']) && !empty($_FILES['profile-image']['name'])) {
        $data['profile-image-name'] = $_FILES['profile-image']['name']; //to assign name to variable
        $allowed = array('png', 'gif', 'jpg', 'jpeg'); // check valid extension
        $extension = strtolower(substr(strrchr($_FILES['profile-image']['name'], "."), 1)); // to get extension value
        if (!in_array($extension, $allowed)) {
            $errors - > add('submit_error', __('ERROR: Only jpg, gif, and png images are allowed.'));

        } else {
            /** WordPress Administration File API */
            include_once(ABSPATH.
                'wp-admin/includes/file.php');

            /** WordPress Media Administration API */
            include_once(ABSPATH.
                'wp-admin/includes/media.php');

            $time = current_time('mysql');
            $overrides = array('test_form' => false);
            $file = wp_handle_upload($_FILES['profile-image'], $overrides, $time);

            if (!isset($file['error'])) {
                $data['profile-image'] = $file['url'];
                $data['profile-image-type'] = $file['type'];
                $data['profile-image-file'] = $file['file'];
            } else {
                $errors - > add('submit_error', __('ERROR: ').$file['error']);
            }
        }
    }
}

Must Read:ShortCode in WordPress

Here, you can add above code into your template file your functions.php file you require.

This tutorials was aimed at those just not clear about image/file uploading in WordPress so if you are confused about anything please leave a comment & I’ll try my best to help rescue you from the castle of confusion. As always, thanks for reading. Don’t Forget to Follow us on Twitter or Subscribe us to Get the Latest Updates.

Comments (1)

  1. […] is a continuation from my last post on File/Image Upload in WordPress. I realized after I had written that tutorial that I would need […]

Leave a Reply

Your email address will not be published. Required fields are marked *

This site uses Akismet to reduce spam. Learn how your comment data is processed.

Most Popular Posts

Branching and Merging in Git

Posted on 1 year ago

Bhumi

How to use Template Parameter in Joomla

Posted on 6 years ago

Bhumi

AWS Cloud Database Migration Service

Posted on 2 years ago

Bhumi

LESS CSS – Nested Rules

Posted on 4 years ago

Bhumi

The Reader’s Poll – June 2015

Posted on 3 years ago

Bhumi

The Reader’s Poll – August 2014

Posted on 4 years ago

Bhumi

The Reader’s Poll – June 2014

Posted on 4 years ago

Bhumi

The Readers’ Poll – May 2014

Posted on 5 years ago

Bhumi

The Readers’ Poll – August 2013

Posted on 5 years ago

Bhumi