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

Confused About My New Laptop: Did I Buy a Used One?

So, I bought a new laptop with Windows 11 about a month ago, and when I set it up, I noticed it already had...

Is Arch Linux Right for Me or Should I Switch to Debian?

I recently set up Linux on my computer as a dual boot to learn more about it, especially for my studies in Cybersecurity. I'm...

Is upgrading from Ryzen 7 5700X to 7700X worth a 17% performance boost?

I'm considering upgrading my PC, specifically swapping out my Ryzen 7 5700X for a new Ryzen 7 7700X, as recommended by a friend. However,...

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

Scavenger Hunt Team Randomizer

Planning a scavenger hunt and need to split participants into random teams? Whether you're organizing a school activity, a corporate team-building event, or a...

File Hash Generator Online – Get Instant MD5 and SHA-256 Hashes

Whether you are validating downloads, checking for corruption, or comparing files for duplicates, having a fast and secure way to generate file hashes is...

Visual CSS Editor for Modern Glass UI Effects

Modern UI design is all about clean, layered aesthetics, and few styles deliver this better than glassmorphism. If you're designing sleek user interfaces and...

Fast and Accurate Tap BPM Counter – Free Web Tool

Whether you're producing music, DJing live, or just figuring out the tempo of a song, knowing the BPM (beats per minute) can be critical....

Glassmorphism CSS Generator with Live Preview

Glassmorphism is one of the most visually striking design trends in modern UI. Its soft, frosted-glass effect adds depth and elegance to web interfaces,...

Add Custom Speech and Caption Boxes to Any Image Online

Creating comic-style images used to require complex design tools or specialist software. Whether you're making memes, teaching graphics, social media posts or lighthearted content,...

Latest Posts

Latest Questions