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.

// Get the first embedded image from the content
function catch_the_first_image() {
global $post, $posts;
$first_img = '';
ob_start();
ob_end_clean();
$output = preg_match_all('/<img.+?src=[\'"]([^\'"]+)[\'"].*?>/i', $post->post_content, $matches);
if ( isset( $matches[1][0] ) ) {
$first_img = $matches[1][0];
}
return $first_img;
}
// Set auto featured image
function set_featured_image() {
global $post;
$post_id = $post->ID;
if ( strlen( catch_the_first_image() ) >= 5 && get_the_post_thumbnail( $post_id ) == '' ) {
$image_url = get_site_url() . catch_the_first_image();
$arrContextOptions = array(
"ssl" => array(
"verify_peer"=>false,
"verify_peer_name"=>false,
),
);
$upload_dir = wp_upload_dir();
if ( is_ssl() ) {
$image_data = file_get_contents( $image_url, false, stream_context_create( $arrContextOptions ) );
}
else {
$image_data = file_get_contents( $image_url );
}
$filename = basename( $image_url );
if( wp_mkdir_p( $upload_dir['path'] ) ) {
$file = $upload_dir['path'] . '/' . $filename;
}
else {
$file = $upload_dir['basedir'] . '/' . $filename;
}
file_put_contents( $file, $image_data );
$wp_filetype = wp_check_filetype( $filename, null );
$attachment = array(
'post_mime_type' => $wp_filetype['type'],
'post_title' => sanitize_file_name( $filename ),
'post_content' => '',
'post_status' => 'inherit'
);
$attach_id = wp_insert_attachment( $attachment, $file, $post_id );
require_once( ABSPATH . 'wp-admin/includes/image.php' );
$attach_data = wp_generate_attachment_metadata( $attach_id, $file );
wp_update_attachment_metadata( $attach_id, $attach_data );
set_post_thumbnail( $post_id, $attach_id );
}
}
// Use it temporary to generate all featured images
add_action('the_post', 'set_featured_image');

view raw
functions.php
hosted with ❤ by GitHub

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

Allow vendors to upload only specific file types in EDD Frontend Submissions

Easy Digital Downloads - Front-End Submissions - Allow only specific mime types upload

Frontend Submissions is an add-on that turns Easy Digital Downloads powered WordPress website into a complete multi-vendor marketplace. Based on your marketplace, you may need to restrict the certain file type uploads or allow specific file types upload only for your vendors.

I am sharing a code snippet that I just have written, and implemented on one of my client’s EDD FES website. Inserting it into WordPress theme’s functions.php will restrict vendors to upload any other file types and allow only zip file type upload.

// FES form field name : Prices and Files
add_filter( 'upload_mimes', 'dcg_restrict_mime_types', 1, 1 );
function dcg_restrict_mime_types( $mime_types )
{
$user = wp_get_current_user(); // get the current user
// if user is shop vendor or a shop manager
if ( in_array( 'shop_vendor', (array) $user->roles ) || in_array( 'shop_manager', (array) $user->roles ) ) {
// add the mime types you want to allow to upload
$mime_types = array(
'zip' => 'application/zip',
);
// Use unset to remove specific mime types uploads.
// unset( $mime_types['xls'] ); // Remove .xls extension
// unset( $mime_types['xlsx'] ); // Remove .xlsx extension
// unset( $mime_types['docx'] ); // Remove .docx extension
return $mime_types;
}
}

view raw
functions.php
hosted with ❤ by GitHub

Have you any better solutions? I would love to read your comments.

Fix : LayerSlider conflict with Fast Velocity Minify Plugin in WordPress

Do you use LayerSlider and Fast Velocity Minify WordPress plugins together? Did your Slider(s) stop to work after activating Fast Velocity Minify plugin? If the answer to any of the questions is Yes, you’ve to possibly exclude few LayerSlider scripts from Fast Velocity Minify.

Here are the steps you can follow to fix LayerSlider conflict with Fast Velocity Minify plugin in WordPress and get your Slider(s) working back.

(1) Go to Settings > Fast Velocity Minify

Step 1 - LayerSlider conflict with Fast Velocity Minify Plugin in WordPress

(2) Click on the Pro tab

Step 2 - LayerSlider conflict with Fast Velocity Minify Plugin in WordPress

(3) Add the following paths under Ignore List
/greensock.js
/layerslider.kreaturamedia.jquery.js
/layerslider.transitions.js

Step 3 - LayerSlider conflict with Fast Velocity Minify Plugin in WordPress

(4) Click on Save Changes

Step 4 - LayerSlider conflict with Fast Velocity Minify Plugin in WordPress

(5) Purge FVM Cache by clicking Delete button under Status tab

Step 5 - LayerSlider conflict with Fast Velocity Minify Plugin in WordPress

That’s it! Now check your site. Your Slider(s) should start to work again.

🗣 Do you need help with a new or existing WordPress site? Get in touch with me to discuss your requirements.