Rename image file by post title

I think what you’re looking for is the filter: wp_handle_upload_prefilter. From the Codex: The single parameter, $file, represent a single element of the $_FILES array. The wp_handle_upload_prefilter provides you with an opportunity to examine or alter the filename before the file is moved to its final location. Example code from the Codex: add_filter(‘wp_handle_upload_prefilter’, ‘custom_upload_filter’ ); … Read more

Get full image array

The “full array” is constructed by Advanced Custom Fields from various sources. It’s not a format that occurs natively in WordPress. If you want to output an image tag for an image with all the correct attributes, just use the ID with wp_get_attachment_image(). That returns an HTML <img> tag with the src, width, height, alt, … Read more

hardcrop images in gutenberg “latest posts” block

The Latest Posts block uses (wp.data.select( ‘core/block-editor’ ).getSettings() to get) the image sizes in the editor settings which can be filtered via the block_editor_settings hook in PHP: apply_filters( ‘block_editor_settings’, array $editor_settings, WP_Post $post ) Filters the settings to pass to the block editor. And the setting name used for the image sizes is imageSizes. So … Read more

How to copy/paste web content with photos into my blog

A little tip a lot of people don’t know about. You can copy/paste an image url into the upload field and it will be auto uploaded into your blog. Right click the image you want to save and select copy image URL (chrome) In your blog click Add Image–> Select Files ( from computer)–>paste url … Read more

Hook to get image filename when it is uploaded

The handle_upload hook is called after wp_handle_upload is run, and it sends in a named array consisting of ‘file’, ‘url’ & ‘type’. You could possibly use code like this to utilise it, depending on what you need to achieve: function process_images($results) { if( $results[‘type’] === ‘image/jpeg’ ) { // or /png or /tif / /whatever … Read more

Use ‘medium’ size with catch_that_image() function

When the first image is a WordPress image attachment. in 3.6, there is an easier way. function get_first_image_medium_size_url($post_id) { if(!$images = get_attached_images($post_id)) return false; $first_image = current($images); if(!$src = wp_get_attachment_image_src($first_image->ID,’medium’)) return false; $medium_url = current($src); return $medium_url; } get_attached_images is available in 3.6. wp_get_attachment_image_src is available since 2.5.0 which will automatically get or scale the … Read more

Is there a way to get attachment data?

Don’t forget that attachments are just posts (though they are native posts and those tend to be a special case). Thus simply get_post() on attachment ID should get you typical post object for it. 🙂 Similarly all API functions that work on posts should work on attachments.