Skip to main content
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,181
38 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,027
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
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

Programmatically adding images to the media library with wp_generate_attachment_metadata randomly fails

I have checked your code, and I think you are missing the guid of the images. Please have a look at the code below: $post_id = 1234; $images = array('filename1.png', 'filename2.png', ... 'filename10....
Syed Fakhar Abbas's user avatar
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

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
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.5k
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
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.8k
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

Programmatically get images by URL and save in uploads folder

John's answer is correct re: using media_sideload_image, just note that you may need to require these scripts before if you aren't doing this within the context of WP Admin: require_once(ABSPATH . '...
bryceadams's user avatar
6 votes

Can I use the wp media uploader for my own plugin?

You can use wp_enqueue_media() in your admin_enqueue_scripts hook. In a javascript file hook it into a button and use insert event to capture the selected image's details $('.media-button').click(...
Mateusz Bartkowski'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.8k
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
6 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
5 votes
Accepted

Open media frame and select an attachment

Well, I found the answer myself. I hope it helps others: I replaced both instances of: if ( selected ) { selection.add( wp.media.attachment( selected ) ); } with: selection.reset( selected ? [ ...
leemon's user avatar
  • 1,920
5 votes
Accepted

Add a new data attribute in <img> tag

I found the solution to add that attribute to the img tag with this function: add_filter('the_content','new_content'); function new_content($content) { $content = str_replace('<img','<img ...
tw8sw8dw8'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.4k
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 to set an attachments parent post id in code?

In WordPress - Attachments are their own post-type which means you just need to update the post using wp_update_post(): $media_post = wp_update_post( array( 'ID' => $attachment_id, ...
Howdy_McGee's user avatar
  • 20.7k
4 votes
Accepted

How to upload image from front end and save in media library?

Now I have an answer for my own question.I solved this issue by using this code.I add this code just for sharing and helping other as it works for me. <input type="file" name="my_file_upload" id="...
raxa's user avatar
  • 610
4 votes
Accepted

Use Timber/Twig to pull an image by image ID

{{ post.thumbnail.ID }} will get you the image ID, but to answer what you're after.... <img src="{{ post.thumbnail.src('medium_16x9') | default( Image(1234).src('medium_16x9') ) }}"> Reference
Jared's user avatar
  • 201

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