Upload an Image Using WordPress Custom Field

We use WordPress custom field to enter more information on a post or a page. But sometimes the project demands to have other features such as image, video, audio, etc. And your clients are not familiar with adding HTML image tags or video URL.

The solution is enable them to upload a file or an image using custom field. I’m lucky to found this script and I made some minor adjustment so it’s easier to understand by beginners.

To get started, copy the code below and paste it in functions.php file of your template:

Yeah you’re right, it’s a bit long. If you want you may put it in a different file and just use require or include commands if possible.

Also, see these lines:

The yellow highlighted text will be the title of new custom field. See the image below:

The green highlighted text are the custom field names that you use in the post or page template. To know more about getting the value of the custom field name you can read How to Get WordPress Custom Field Value.

Now, the user have two options in adding an image. To upload an image or enter the image URL where uploading image is a lot easier.