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

getting attachement images src and add classes

If you only want to add an extra class, then you should use wp_get_attachment_image. It has few extra params, and the last one is used for setting class names. Sample usage: <?php echo wp_get_attachment_image( get_the_ID(), ‘thumbnail’, “”, [“class” => “my-custom-class”] ); ?> The main advantage of this aproach is that you will also get the … Read more

what the best way to include images from the template’s images folder?

The easiest and simplest way to do it is define a unique variable in your theme’s functions.php file. Such as: <?php $theme_name_images = get_bloginfo(‘stylesheet_directory’) . ‘/images/’; ?> No need for classes as a previous answer suggested. EDIT: It should be get_bloginfo, instead of bloginfo(), as Viper007Bond kindly pointed out.

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

How to custom crop each image size?

You can customize or change WordPress image sizes using this function: http://codex.wordpress.org/Function_Reference/add_image_size <?php add_image_size( $name, $width, $height, $crop ); ?> The $crop parameter can be set to false for proportional or true for hard crop (it will hard crop from the center). If you use this function on already existing images you will need to … 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

Adding Category/Tag/Taxonomy Support to Images/Media

Here’s how I recently added a custom taxonomy to the media library as a sortable column: // Add a new column add_filter(‘manage_media_columns’, ‘add_topic_column’); function add_topic_column($posts_columns) { $posts_columns[‘att_topic’] = _x(‘Topic’, ‘column name’); return $posts_columns; } // Register the column as sortable function topic_column_register_sortable( $columns ) { $columns[‘att_topic’] = ‘att_topic’; return $columns; } add_filter( ‘manage_upload_sortable_columns’, ‘topic_column_register_sortable’ ); … Read more