Home > WordPress > How to Add Custom Media Uploader Button in WordPress?

How to Add Custom Media Uploader Button in WordPress?

In this post, we will see how to add a custom media uploader button in wordpress. We can add a custom media button on meta boxes, a custom options page, or posts and pages sections.

WordPress has a built-in media upload feature that I am sure you have used when you upload any image in WordPress. When you click on the ‘Upload‘ button a popup will show the WordPress media library and you can upload a new image or can select from an existing one.

So what if we want the same functionality implemented on our custom plugin, custom theme, or any custom options page?

No worry!!!

In this tutorial, I will explain to you how you can implement custom media uploader button functionality anywhere.

Let’s get started!

Add Custom Media Uploader Button in WordPress

To add a custom media uploader button on your meta boxes, theme page, plugin options, or anywhere, you have to add the HTML structure for the ‘Upload‘ button and jQuery media library.

By taking the reference of the tutorial custom options page in WordPress, we will add a custom media uploader button on that options page.

I would recommend you to check this tutorial first how to create a custom options page in wordpress, so will be easy to continue here.

Add and Register Settings Fields

If you had a chance to check that tutorial, you see there were two input fields. Now we will add one more field ‘Theme Logo‘ and register a settings for it.

The above action hook will register the setting field for ‘Theme Logo‘. You can customize it as you want. Now we will add HTML structure of that field to show on the custom setting page.

HTML Structure for Field

In the above code, add_settings_fields() method have a third parameter ‘theme_logo_media_field_html‘ that is refrencing to HTML structure of the field.

So now we will make that HTML structure function to show that field on custom options setting page. Look at the following code.

Simply add the above code in functions.php file too and you will see the ‘Upload image’ button. But nothing will happen by clicking on it because we haven’t added the jQuery code yet.

jQuery Media Button Code

So let’s add the jquery part of media uploader button in wordpress. You can enqueue this code with action hook directly or you can create the separate .js file.

I would recommand to create a new separate file called ‘scripts.js‘ but name dose not matter you can take anything.

So I have created file ‘scripts.js‘ file with following code. You also put the following code in that file san save it.

So you have successfully added js code in file and now only one last thing is left to enqueue this file and media library file.

Enqueue Script Files

We will use the admin_enqueue_scripts action and wp_enqueue_script() method to enqueue the js script file and the media file. It will embed our js script file and wordpress media file in header of admin side only.

Add the following code in functions.php file and save it.

After saving the above code, go to that setting page and you will see all the fields there. Now when you click on the ‘Upload image’ button it will open the WordPress media library popup, you can select the image or you can upload there new file.

When you have selected the image and click on Save setting button, you will see your image is saving along with other fields.

Conclusion

So in this tutorial, you learned about how to add custom media uploader button in wordpress on custom options page. You can also add on meta boxes, posts or pages, etc.

First we register the settings for the field using the add_settings_field() and register_setting() methods. Then create the HTML structure for the field and then enqueue the custom script file and media library file using the wp_enqueue_media() and wp_enqueue_script() methods.

That’s it!!!

If you have any question please do let me know in the comment section, I will get back to you as soon as possible.

Photo of author

About Aman Mehra

Hey there! I'm Aman Mehra, a full-stack developer with over six years of hands-on experience in the industry. I've dedicated myself to mastering the ins and outs of PHP, WordPress, ReactJS, NodeJS, and AWS, so you can trust me to handle your web development needs with expertise and finesse. In 2021, I decided to share my knowledge and insights with the world by starting this blog. It's been an incredible journey so far, and I've had the opportunity to learn and grow alongside my readers. Whether you're a seasoned developer or just dipping your toes into the world of web development, I'm here to provide valuable content and solutions to help you succeed. So, stick around, explore the blog, and feel free to reach out if you have any questions or suggestions. Together, let's navigate the exciting world of web development!

Leave a Comment