Skip to main content
65 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,221
39 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,037
16 votes

Delete post with all files attached to it

I wrote the following function that uses the logic suggested by @rarst in his answer. add_action( 'before_delete_post', function( $id ) { $attachments = get_attached_media( '', $id ); foreach ($...
That Brazilian Guy'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
13 votes

How do I get the size of an attachment file?

I would do : $attachment_filesize = filesize( get_attached_file( $attachment_id ) ); Or with readable size like 423.82 KB $attachment_filesize = size_format( filesize( get_attached_file( $...
l2aelba's user avatar
  • 857
12 votes

Disable Attachment Pages Completely

You can filter default rewrite rules and remove those for attachments: function cleanup_default_rewrite_rules( $rules ) { foreach ( $rules as $regex => $query ) { if ( strpos( $regex, '...
Ihor Vorotnov'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

Disable Attachment Pages Completely

If you want to disable media pages completely, you should use a 404 response code instead of redirection. This can be done with the following code: function wpse237762_set_404() { if (...
fairport's user avatar
  • 251
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
8 votes
Accepted

Count the number of images uploded on the website

There's a handy built-in function, namely wp_count_attachments(). We can filter out images with wp_count_attachments( $mime_type = 'image' ) that returns an object like: stdClass Object ( [image/...
birgire's user avatar
  • 67.8k
8 votes
Accepted

Can i hide the attachments from media which I uploaded from front end?

WordPress has two different ways of displaying media. There's the media overlay/modal view and also the list view, which is triggered by navigating to Admin > Media > Library then by clicking ...
Dave Romsey's user avatar
  • 17.9k
7 votes

Disable Attachment Pages Completely

For those who might not use plugins or prefer light-weighted method. This might be of help. This method redirect attachment to the exact file instead of the attachment page and it is the method that ...
西門 正 Code Guy - JingCodeGuy's user avatar
7 votes

How do you get the full size url of an attachment image

I solved this using wp_get_attachment_url($imageID); But, you can also use wp_get_attachment_image_url($imageID, 'full')
Jeff Wilkerson's user avatar
6 votes

How do I get the size of an attachment file?

There's an easier solution, to get human readable file sizes. $attachment_id = $attachment->ID; $attachment_meta = wp_prepare_attachment_for_js($attachment_id); echo $attachment_meta['...
Zoltan Kiraly's user avatar
6 votes
Accepted

How to save custom fields for attachments

Here is an example that adds a custom media field named Buy Now. This example saves the value of the custom field on the media overlay screen via ajax, as well as the media edit screen (non ajax). ...
Dave Romsey's user avatar
  • 17.9k
6 votes

Programmatically adding images to media library

I had issues with @TrubinE's solution where image files were not getting loaded. Here is a complete example that worked for me: https://gist.github.com/m1r0/f22d5237ee93bcccb0d9 This is a similar idea ...
Lance Cleveland's user avatar
5 votes

Call to undefined function wp_generate_attachment_metadata()

Despite you already solved it (the "undefined" issue), it should be noted that wp_generate_attachment_metadata() is defined in wp-admin/includes/image.php, so you need to manually load that ...
Sally CJ's user avatar
  • 39.9k
4 votes

Delete post with all files attached to it

Expanding on other answers here for use with only a specific post type. add_action( 'before_delete_post', 'delete_all_attached_media' ); function delete_all_attached_media( $post_id ) { if( ...
adambware's user avatar
4 votes

How do I get attachment_id?

I am too late to answer this question but it will worked. $attachment_id = get_post_thumbnail_id( $post->ID ); $url = wp_get_attachment_image_src($attachment_id, 'desired-size');
Bilal's user avatar
  • 41
4 votes

How to get image title/alt attribute?

$image = get_post_meta(get_the_ID(), WPGRADE_PREFIX . 'homepage_slide_image', true); if (!empty($image)) { $image = json_decode($image); $image_id = $image->id; $img_meta ...
Benn's user avatar
  • 1,043
4 votes

How can i change an image's author?

In the screenshot you provided, click on the Edit more detail. this will redirect you to attachment's page. Then, click on the Screen's options on the top of screen, and check the Author box. A new ...
Johansson's user avatar
  • 15.4k
4 votes
Accepted

media_handle_upload for local files?

You want media_handle_sideload() Handles a side-loaded file in the same way as an uploaded file is handled by media_handle_upload(). // Array similar to a $_FILES upload array. $file_array = array(...
Jacob Peattie's user avatar
4 votes

Using delete_attachment to prevent an attachment not working

Updated answer for WordPress 5.5.0+ As of 5.5.0, a new filter has been added to wp_delete_attachment: /** * Filters whether an attachment deletion should take place. * * @since 5.5.0 * * @param ...
phatskat's user avatar
  • 3,174
4 votes

Need clear explanation about medias/files/attchments

Attachments in WP are stored as posts of type attachment which reference files physically stored in the uploads folder. This is why if you FTP a file into the uploads folder it doesn't appear in the ...
Tom J Nowell's user avatar
  • 60.6k
3 votes

Associate an existing image with a post

I have decided to post it as a separate post and not a comment because it will be lost. I've tried Ben's solution but it modifies the attachment's other data fields. I ended up using this. ...
Svetoslav Marinov's user avatar
3 votes

How to find attachment by it's name?

get_page_by_title() will do the trick. With a full URL as title: get_page_by_title( pathinfo( 'https://www.example.com/file.jpg' )['filename'], "OBJECT", 'attachment' ); Returns WP_Post Object or ...
Lukas Heuser's user avatar
3 votes

copy attachment title to description and alt text

Well, I was using lightbox gallery, and I needed to have the post title for my images as the alt text. What I did was create a query on the database to update the post_excerpt to match the post title....
Gabriel Castillo's user avatar
3 votes

Get wp_get_attachment_url outside of loop

Little late here, but I created a solution that is a bit cleaner and reusable with some of the suggestions in this thread. Add this anywhere in your functions.php file, then simply call this function ...
Isaac's user avatar
  • 43
3 votes

Turn a URL into an Attachment / Post ID

Ok I found the answer that no one has on the net I been looking for days now. Keep in mine this only works if your theme or plugin is using the WP_Customize_Image_Control() if you are using ...
DevTurtle's user avatar
  • 211
3 votes

how to get original image using wp_get_attachment_image_src

There's wp_get_attachment_image_url introduced in WordPress 4.4.0. It takes these arguments: int $attachment_id - Image attachment ID. string|array $size - Optional. Image size to retrieve. Accepts ...
ericek111's user avatar
  • 196

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