Skip to main content
Share Your Experience: Take the 2024 Developer Survey
64 votes
Accepted

How to get image title/alt attribute?

An attachment/image ID is all that's needed to retrieve its alt and title attributes. $image_id = get_post_thumbnail_id(); $image_alt = get_post_meta($image_id, '_wp_attachment_image_alt', TRUE); $...
leymannx's user avatar
  • 3,161
37 votes
Accepted

Programmatically adding images to media library

$image_url = 'adress img'; $upload_dir = wp_upload_dir(); $image_data = file_get_contents( $image_url ); $filename = basename( $image_url ); if ( wp_mkdir_p( $upload_dir['path'] ) ) { $file = $...
TrubinE's user avatar
  • 1,017
36 votes

How to get a list of all the possible thumbnail sizes set within a theme

WordPress core doesn't have a native method for getting intermediate image sizes (i.e. width and height), but the following helper function will get all registered image sizes along with their ...
rjb's user avatar
  • 1,387
21 votes

Prevent Wordpress from generating medium_large 768px size of image uploads?

To remove the medium_large image size you can try to remove it with the intermediate_image_sizes filter: add_filter( 'intermediate_image_sizes', function( $sizes ) { return array_filter( $sizes, ...
birgire's user avatar
  • 67.4k
20 votes
Accepted

How to extend the gallery block in Gutenberg?

Ok, I've been playing with this for a little bit and have managed to change the output of the Gallery block, with the following caveats: The preview does not match the output. I think this is ...
Jacob Peattie's user avatar
20 votes
Accepted

WordPress adding scaled images that don't exist (1536x1536 and 2048x2048)

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 ...
Sam's user avatar
  • 2,136
17 votes

How to get a list of all the possible thumbnail sizes set within a theme

Since WP 5.3 it is enough to use this function: wp_get_registered_image_subsizes(); Documentation: https://developer.wordpress.org/reference/functions/wp_get_registered_image_subsizes/
wp-mario.ru's user avatar
17 votes
Accepted

Get attachment/image info in JS

Long story short, you can get info about an attachment by using wp.media.attachment() function. This will give you complete data as long as this attachment is already loaded by another script or an wp....
Andrei Glingeanu's user avatar
17 votes
Accepted

Hook after image is uploaded and image sizes generated

Is there any hook that fires once the image is uploaded and the image sizes generated? wp_handle_upload fires after the image is uploaded. After the follow-up question, I discovered that images would ...
Nathan Johnson's user avatar
15 votes

Programmatically adding images to media library

If you use WordPress' sideload feature, you can do this more easily (and have WordPress handle all of the sanitization for you). <?php // example: // $file = 'http://www.example.com/image.png'; //...
Paul Schreiber's user avatar
14 votes

Getting alt text of featured image

The alt text is stored as post meta on the attachment, and can be retrieved with get_post_meta() combined with get_post_thumbnail_id(): <?php echo get_post_meta( get_post_thumbnail_id(), '...
Jacob Peattie's user avatar
13 votes

Image Upload from URL

You can use the functions download_url() and wp_handle_sideload(). download_url() Downloads a url to a local temporary file using the WordPress HTTP Class. Please note that the calling function ...
Rajilesh Panoli's user avatar
13 votes

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 ...
Ionut Staicu's user avatar
13 votes
Accepted

Can't extract and set SVG dimensions

I solved it!!! The filter in fix #3 (above) wasn't working because of the third condition of this if statement that triggers the dimension extraction and attachment: if (is_array($image) && ...
CoderScissorhands's user avatar
12 votes
Accepted

How can I set image sizes and still have responsive images using the srcset attribute?

The srcset attribute is constructed from images that are the same aspect ratio. Create a few of those and you'll be ok. add_image_size( 'compare-offer-box', 400, 300, true); add_image_size( 'compare-...
Andy Macaulay-Brook's user avatar
11 votes
Accepted

How to set the default embed image size

If you add this to functions.php, this will update the options as set. function custom_image_size() { // Set default values for the upload media box update_option('image_default_align', '...
Howard E's user avatar
  • 743
10 votes

Disabling auto-resizing of uploaded images

To disable resizing for all image sizes without manually removing each size with remove_image_size function, use the filter intermediate_image_sizes_advanced /** * @param array $sizes An ...
Rahil Wazir's user avatar
  • 1,021
10 votes
Accepted

Issue with wp_get_attachment_image() and SVG file type

WordPress has trouble with the width and height attributes for SVGs. The width and height attributes can be removed for SVGs using this code adapted from trac ticket #26256 . /** * Removes the width ...
Dave Romsey's user avatar
  • 17.7k
10 votes

Prevent Wordpress from generating medium_large 768px size of image uploads?

Remove image size the same way wordpress core code does it: add_filter('intermediate_image_sizes', function($sizes) { return array_diff($sizes, ['medium_large']); }); Keep in mind that ...
Dima Stefantsov's user avatar
10 votes

wp_get_attachment_image_attributes not working for me

Okay, the problem was in my interpretation of the filter. I was under the impression that this filter could be used for all images in a post, as they're all considered attachments in the db (post type ...
LubosB's user avatar
  • 321
10 votes
Accepted

Disable lazy loading on specific images

You can manage WordPress lazy loading with the loading parameter. Below code echo's your image without using the lazy load function. echo wp_get_attachment_image( $image, $size, false, [ 'class' =&...
DeltaG's user avatar
  • 899
9 votes

removing inline styles from wp-caption div

To remove the inline width in a clean PHP-way could be done with a filter, as described in the source code: https://core.trac.wordpress.org/browser/trunk/src/wp-includes/media.php#L1587 Returning a ...
Johan Hermansson's user avatar
9 votes

How to get image title/alt attribute?

I use a quick function in all my themes to get image attachment data: //get attachment meta if ( !function_exists('wp_get_attachment') ) { function wp_get_attachment( $attachment_id ) { ...
Dario Zadro's user avatar
9 votes

How to get all featured image sizes and their URLs?

After you get it's ID ( get_post_thumbnail_id($YOUR_POST_ID) ) you need to know the uploads base url and the attachment meta data. Uploads base url can be found in an array returned by wp_upload_dir()...
jave.web's user avatar
  • 477
9 votes

Getting Images from theme's directory in pages

<img src="<?php echo esc_url( get_template_directory_uri() . '/images/logo.jpg' ); ?>" alt="" > Try this. It should work. You have to concatenate the results from ...
Kaif Ahmad's user avatar
9 votes
Accepted

Regenerate missing _wp_attachment_metadata

Running the wp media regenerate command from WP-CLI with the --only-missing argument is quite fast (takes about 30 seconds for 4000 images) and rebuilds _wp_attachment_metadata correctly: wp media ...
That Brazilian Guy's user avatar
9 votes
Accepted

Change WordPress image URLs via filter

You will most likely want to use filters. The main issue is that developers will need to make sure to use the correct functions like "the_post_thumbnail()" etc. You can use wp_get_attachment_url ...
ggedde's user avatar
  • 245
9 votes

Why does wordpress generate more thumbnails than the 3 standard sizes?

WordPress generates more than 3 thumbnails or image sub sizes because there are actually more than 3 standard/core image sizes in WordPress. Moreover, themes and plugins can also register custom image ...
Sally CJ's user avatar
  • 39.3k
8 votes

How to find attachment by it's name?

Another possible approach is directly use $wpdb to do SQL query, in the posts table, with the searched post title, and with post_type "attachment". function get_attachment_url_by_title( $title ) { ...
Snade's user avatar
  • 81
8 votes

How To Delete Desired Image Sizes From wp-content/uploads?

That's easy! If you have SSH access, log in and do the following for each size. I am just showing 150x150 size only. Go to the desired uploads folder via command line. cd /your-site.com/wp-content/...
Ahmad Awais's user avatar

Only top scored, non community-wiki answers of a minimum length are eligible