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

Rename files on upload

no need to use a custom table, use an option, and the add_attachment hook: function wpa59168_rename_attachment( $post_ID ) { $post = get_post( $post_ID ); $file = get_attached_file( $post_ID ); $path = pathinfo( $file ); $count = get_option( ‘wpa59168_counter’, 1 ); // change to $new_name = $count; if you want just the count as filename $new_name … Read more

How do I hook into WordPress to save an uploaded photo as an alternate size to an existing photo?

This can be done by using: wp_get_attachment_metadata(); wp_update_attachment_metadata(); You’d get the meta data for it and change it around then save it. The $data array you get contains another array named sizes, this array contains all the different size images connected to the attachment. You want this to update automatically when you upload images that … Read more

Add default Backgrounds

There’s no default way to do that yet, though there is a Trac ticket. I found this (untested) that may work for you: http://www.devblog.fr/2011/05/16/plusieurs-fonds-personnalises-wordpress-add_custom_background/ It’s also worth noting the 3.4’s new way of defining a default background (still just one, though). Edit Here’s the code: <?php function wpse48332_setup_theme() { // Add support for custom backgrounds … Read more

Maintaining image color and quality when uploading using custom sizes

Use the jpeg_quality and wp_editor_set_quality filters to set the quality of resized images: add_filter( ‘wp_editor_set_quality’, ‘wpse246186_image_quality’ ); add_filter( ‘jpeg_quality’, ‘wpse246186_image_quality’ ); function wpse246186_image_quality( $quality ) { return 100; // 0 – 100% quality } I have not been able to reproduce the issue reported; the original PNG image looks the same as the thumbnails. Tested … Read more