Set WordPress Featured Image Using Javascript

Setting featured images for a post in WordPress can be rather time-consuming, especially, when you do not care too much what the images. For example, if you are writing about a movie, the movie poster will probably make a perfect featured image. You just have to go looking through the gallery to find it. This gave me the idea to build a small meta box that would attempt to suggest a featured image to use for the post automatically. One-click and the featured image is set, saving you 30 seconds of valuable time that can be better spent producing awesome content for your blog.

Setting a featured image for a post in the back end is a fairly easy task to perform and is well documented. Setting it via JavaScript is not something you see so often, as most people don’t see the need for it when the WordPress editor already has a way to set a featured image. After doing some digging, I eventually got to the bottom of it and was able to create the tool I needed that would make life super simple setting featured images in WordPress using Javascript.

wp.media.featuredImage.set(attachmentId);

Yep, that’s it! The code above was all I needed to set a featured image inside the WordPress post editor page. I wrote a javascript function that would make an Ajax call to a PHP script. The call would pass information about the post I was writing. The PHP script would then scan the wordpress post database to find similarly tagged posts. It would then fetch the featured image of those posts and if it existed, it would return it. The tool then displayed the list of images. When the image was clicked, I would call the line of code above and BAM. The UI automatically updated to set that featured image for me without any additional code. Very simple idea and a handy solution for those like me who just want some nice art to put at the top of blog posts.

Related Articles

Related Questions

WordPress Table of Contents Plus Not Working

I have been using this plugin for a while and i really like it. It seems to have completely stopped working recently. I can...

Function Keys Reversing Between Fn Actions And Normal

My keyboard has the usual F1 to F12 keys along the top. I use these for shortcuts in various applications. These keys also have...

Whirlpool Oven F6E6: Appliance Manager 1 Board Communication

I have a brand new Whirlpool oven W11I OM1 4MS2 H or (859991549450). I bought it alongside the microwave combi oven. I have had...

LEAVE A REPLY

Please enter your comment!
Please enter your name here

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

Latest Tools

Markdown To Html Converter

Welcome to our web-based tool designed to make your life easier by converting Markdown to HTML in a matter of seconds. Our user-friendly interface...

AI Content Detector

We've got this awesome free tool that'll help you figure out if that content you're looking at was written by a human or some...

Image Saturation

Are you looking for an easy-to-use, free app to modify your image saturation levels and make your pictures truly pop? Look no further! Our...

Pixelate Image Tool

Introducing the ultimate free online image pixelator tool that allows you to easily transform your images into stunning pixel art in just a few...

Image RGB Level Adjustment Tool

Introducing the ultimate image color adjustment tool for all your photo editing needs. Our free online tool lets you take full control of your...

Image Color Inverter

Looking for a quick and efficient way to convert your images into negatives? Our Free Image to Negative Converter is the answer! Our online...

Latest Posts

Latest Questions