WordPress : Set first image of the content as featured image

If you’ve migrated your blog from another platform to WordPress or changed your theme, you may be wondering why some post thumbnails aren’t being displayed anymore. This could be because the featured image for that particular blog-post isn’t set. If you’ve very few posts, you can do it by editing those posts and set a featured image manually, but what if you’ve hundreds or thousands of blog-posts?

In that case, rather than to edit each post and set a featured image manually, you can use the following code snippet. This snippet will set the first embedded image from the content as the featured image – only if there is no featured image been set. For any external images, it also creates a media file and stores it in a WordPress Media Library. This is very useful to generate and set featured images in bulk.

Add the below code snippet in the functions.php file of your active WordPress theme, and load All Posts ( {YOUR-DOMAIN}/wp-admin/edit.php ) page or any single post page, and it will do the magic.

Once you verify that all the featured images been set properly, you can remove the above code snippet from the functions.php.

* Photo credit: Guilherme Vasconcelos on Unsplash

Debug WordPress White Screen of Death and other Errors

After so many requests asking about “WordPress white screen of death”, I would like to quickly share my strategy I use in most of the WordPress installs I manage which allow me to quickly switch on debug mode when required.

In the file wp-config.php, which can be found in your WordPress root, search for variable WP_DEBUG and replace it with

if (isset($_GET['debug']) && $_GET['debug']=='debug_mode') {
    error_reporting(E_ALL); ini_set('display_errors', 1);
    define('WP_DEBUG', true);
}

Now you can open any page, and if something goes wrong there, like a white screen of death, you can add ?debug=debug_mode to the end of its URL and check what’s causing the trouble. Eg. http://www.mywordpresssite.com?debug=debug_mode

I hope this helps.