Media Upload Folder – how to manage

A simple answer is that you can remove 2 unused sizes by enter 0 for their width or height. For example, you need only the small thumbnail 150×150 and full-sized image, so enter 0 for width and height of Medium and Large sizez.

How to find attachment by it’s name?

You have to write custom code to get the attachment id and post_parent by name/ slug or filename(if it has not been changed during the uploading of files). Put the below code in your theme’s functions.php file if( ! ( function_exists( ‘wp_get_attachment_by_post_name’ ) ) ) { function wp_get_attachment_by_post_name( $post_name ) { $args = array( ‘posts_per_page’ … Read more

Show two different sized featured images on homepage

With get_post_thumbnail() you have the ability to insert a featured image in your theme. the_post_thumbnail( $size, $attr ); holds to parameter 1. The size of your image and the ID. If you would like to have 2 image sizes of featured images on your home page you could do it like this: if(is_front_page(){ // only … Read more

How to insert images into posts without using Add Media dialog

The WordPress default media management system is pretty good and far better than what you want. However, you can use the Advanced Custom Fields to add image upload boxes in your post editor page. Once the image is uploaded, you can display it in the editor using a shortcode [acf field=”{$field_name}”]. Check out ACF Documentation … Read more

Thumbnails of same size with different crop

You can define your own cropping sizes but you can create a function that accepts image size + image position and then load your images accordingly. For example- <?php $size=”medium”; $pos = array( ‘top’ => ‘100’, ‘left’ => ‘100’ ); function load_image_with_pos( $img_id, $size, $pos ) { $img_src = wp_get_attachment_image_src( $img_id, $size ); $new_img = … Read more

Edit srcset and sizes attributes in Gutenberg image, cover and gallery – blocks

Try the suggestion below: This assumes the <figure> element holding an image has a data-display-alignment attribute to match the alignment class applied attached that is somehow surfaced as a parameter in the wp_calculate_image_sizes hook: /** * Add custom image sizes attribute to enhance responsive image functionality * for content images. * * @param string $sizes … Read more

How to check if image is already stored in a site’s post database? (network)

Yes, a database query is possible with WordPress: function is_image_in_network( $image_name ) { global $wpdb; $image = “uploads/2012/01/{$image_name}”; $value=”%”.like_escape( $image ).’%’; $blog_id = get_current_blog_id(); $wpdb->set_blog_id( $blog_id ); $image = $wpdb->get_var( $wpdb->prepare( “SELECT ID FROM {$wpdb->posts} WHERE post_status=”publish” AND post_content LIKE ‘%s'” , $value ) ); return is_null( $image ) ? false : true; } You … Read more