• Home
  • Features
    • Membership Software
    • Member Directory
    • Event Calendar & Registration
    • Member Billing & Online Payments
    • Donations, Shopping Carts & Forms
    • Job Board
    • Announcement Board
    • Classified Ads
    • Members Only Website
    • Website Integration
      • WordPress
      • Squarespace
      • Weebly
      • Without Code
      • Duda
      • Wix
      • HTML5
    • Integrations
    • International
  • Sign Up
    • Pricing & Sign Up
  • Customers
    • Customer Sign In
    • Getting Started
      • MembershipWorks Hosted Website
      • WordPress Plugin
      • Squarespace
      • Weebly
      • Without Code
      • Wix
      • HTML5
    • Customer Websites
  • Developers
    • Find a Developer
    • Create a Developer Listing
    • Manage Developer Listing
  • Demo
    • WordPress Demo
    • Squarespace Demo
    • Weebly Demo
    • Without Code Demo
    • Duda Demo
    • Wix Demo
    • Video Demo of Admin
    • Schedule a Personal Demo
  • About
    • Contact Us
    • Cancel/Suspend Account
    • Schedule a Personal Demo
    • Schedule a Personal Training Session
    • Our Blogs
      • Guides
      • Best Practices
      • Secret Confessions of an ED
      • WordPress with the Light On
    • New Features
    • Careers
    • System Status
Schedule Demo

Discover our membership software with a one-on-one personalized demo:

Schedule a Demo
CMS Integrations

Integrate with WordPress, SquareSpace, Weebly and more.

Learn More!
MembershipWorkslogo

Automatically Setting a Featured Image

Posted on May 9, 2013 by Aikmeng KuahDec 1, 2014
bridge

Its easy to forget to set a featured image when creating a new post or page, and honestly, it may not seem terribly important. But it can be a good idea to make sure this image is always set if you use the featured image in post lists for your theme. More importantly, SEO plugins or themes may need the featured image for rich snippets or Facebook open graph meta tags.

Not providing a rich snippet or meta tag for an image, can create problems when your website visitor tries to share the page or post on Google Plus or Facebook. Without this signal, Google or Facebook will use their own algorithms to determine which image on your page will be used to represent your page. And often, that is the first image on the page. If that is a small logo, that would get scaled up into a pixelated image, which would not represent the website well.

Automatically setting a featured image can be easily done in the theme. Simply hook onto the "save post" action, and check if a thumbnail has been set. If not, search the content for an image, and set the featured image to that image. That way, if the user forgets to set the featured image, the theme automatically sets it to the first image it finds in the content. If the user does set a featured image, it won't get over-written.

function st_save_post($post_id) {
if (!defined('DOING_AUTOSAVE')||!DOING_AUTOSAVE) { // skip if autosave
if (!($id=wp_is_post_revision($post_id)))
$id=$post_id;
if (isset($_POST['content'])&&!get_post_thumbnail_id($id)) { // set featured image
$match=array();
preg_match('/"[^"]*wp\-image\-(\d+)/',$_POST['content'],$match);
if (isset($match[1])) set_post_thumbnail($post_id,intval($match[1]));
}
}
}

add_action('save_post','st_save_post');

The only tricky part about this is working with the WordPress post revision system. If you attempt to get the post thumbnail using $post_id, which could be a revision post ID, WordPress may report that no thumbnail is set, even if the parent post has a thumbnail set. So we need to lookup the parent post ID, if exists, using wp_is_post_revision.

Adding this little bit of functionality to a theme, makes setting featured images one less thing your customers have to worry about, without intruding on their ability to set the featured image as they see fit.

GoToOlder PostNewer PostAll PostsWordPress with the Light On

About Aikmeng Kuah

I'm co-founder and CTO of MembershipWorks. I've been writing code since the Apple II, in a variety of languages including x86 assembly, 68000 assembly, Pascal, Prolog, dBase, C, LISP, SQL, PHP and Javascript. I work on the full stack, from the web site design, front-end web app, back-end servers, database and API. I am a graduate of Carnegie Mellon University (BS) and University of Texas at Austin (PhD).View all posts by Aikmeng Kuah

3 comments

  1. Name on May 13, 2013 at 4:48 pm
    This is great! I've been looking for a way to do this without plugins. Thanks for sharing.
  2. Name on May 13, 2013 at 4:53 pm
    Is there a way to load a default image if it cannot find featured image nor any image in the post?
    • Aikmeng Kuah on May 13, 2013 at 6:54 pm
      Yup, after the if statement "if (isset($match[1])) set_post_thumbnail($post_id,intval($match[1]));" you can add an "else set_post_thumbnail($post_id,$default_thumbnail_id)". So if it does not find a match, it will set the thumbnail image ID to some default given by $default_thumbnail_id. The only issue is you will need a default image and its associated ID.

Comments are closed.

  • Home
  • Features
  • Sign Up
  • Customer Sign-In
  • Terms of Service / Privacy
  • Developers
  • Contact