How to change image type for specific size?

You can use the wp_generate_attachment_metadata filter: function wpse_183835_to_jpeg( $meta, $post_id ) { $sizes_to_convert = array( ‘thumbnail’, ); $path = dirname( get_attached_file( $post_id ) ); foreach ( $sizes_to_convert as $size ) { if ( ! empty( $meta[‘sizes’][ $size ] ) ) { $data = $meta[‘sizes’][ $size ]; if ( $data[‘mime-type’] === ‘image/png’ && is_file( $file = … Read more

Retrieve featured image

The featured image attachment id is stored in the post_meta table, with the meta_key _thumbnail_id. Use that post id to find the post meta with meta_key _wp_attachment_metadata. The value of that is a serialized array with keys thumb, file, width, height, sizes (an array), and image_meta (an array). The file value is relative to the … Read more

Overide Gallery Default Link to Settings

Unfortunately there is no legal way to control it. But there is a dirty way to do it… If you select this route, then you will need to : clone the standard gallery_shortcode function add a default value for $attr[‘link’] option hook your cloned function into post_gallery filter The final result will look like this: … Read more

Prevent WordPress from adding image’ title automatically

You can try the following to clear the image attachment’s title when it’s inserted but not updated: /** * Empty the image attachment’s title only when inserted not updated */ add_filter( ‘wp_insert_attachment_data’, function( $data, $postarr ) { if( empty( $postarr[‘ID’] ) && isset( $postarr[‘post_mime_type’] ) && wp_match_mime_types( ‘image’, $postarr[‘post_mime_type’] ) ) $data[‘post_title’] = ”; return … Read more

WP 4.4. responsive images browser choosing the “wrong” one

Concerning documentation there is this blog post on the Make Blog: Responsive Images in WordPress 4.4 To increase the 1600px limit mentioned in the comments try this: add_filter(‘max_srcset_image_width’, function($max_srcset_image_width, $size_array){ return 2000; }, 10, 2); Finally as already mentioned you should fix your calls to add_image_size add_image_size(‘news-large’, 1024, false); needs to be add_image_size(‘news-large’, 1024, 0, … Read more

Download an image from a webpage to the default uploads folder

I recently had to do this via a nightly cron script for a social media stream. $parent_id is the ID of the post you want to attach the image to. function uploadRemoteImageAndAttach($image_url, $parent_id){ $image = $image_url; $get = wp_remote_get( $image ); $type = wp_remote_retrieve_header( $get, ‘content-type’ ); if (!$type) return false; $mirror = wp_upload_bits( basename( … Read more

WordPress adding scaled images that don’t exist (1536×1536 and 2048×2048)

I found the culprit! WordPress 5.3 introduced additional image sizes which can be found via /wp-includes/media.php. Updating my function, like so, removed the extra sizes: function remove_default_image_sizes( $sizes) { unset( $sizes[‘large’]); // Added to remove 1024 unset( $sizes[‘thumbnail’]); unset( $sizes[‘medium’]); unset( $sizes[‘medium_large’]); unset( $sizes[‘1536×1536’]); unset( $sizes[‘2048×2048’]); return $sizes; } add_filter(‘intermediate_image_sizes_advanced’, ‘remove_default_image_sizes’);