Skip to main content
46 votes
Accepted

How do I select an image from Media Library in my plugin?

You should use wp.media to use the WordPress Media Manager dialog. First, you need to enqueue the scritps: // As you are dealing with plugin settings, // I assume you are in admin side add_action( '...
cybmeta's user avatar
  • 20.5k
36 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,007
17 votes
Accepted

Media not actually deleted on disk when click "Permanent Delete"

So, after several attempts... it is a problem (or a feature) of the WPML multilanguage plugin. To start with, I have set up correctly the user permissions, as @WebElaine mentioned. Just in case ...
mmystery's user avatar
  • 331
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

WordPress 5.3 update causes media library file upload to break - Cannot convert undefined or null to object

I had the same problem. In my case it was the Enhanced Media Library plugin, which looks as though it has not been updated in some time. I deactivated it, and all is good.
joakland's user avatar
  • 251
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
Accepted

How can I batch delete all unattached images with WP-CLI or other automated process?

You can try this (untested) modification to the answer you linked to by @something wp post delete $(wp post list --post_type='attachment' --format=ids --post_parent=0) to delete attachments without ...
birgire's user avatar
  • 67.1k
10 votes

List of available events for wp.media

If you're still looking for this kind of answer, one way of figuring out what is firing is by entering this in your browser console and using the media modal: wp.media.frame.on('all', function(e) { ...
thumbson's user avatar
  • 201
9 votes

Upload Multiple Files With media_handle_upload

If you want to implement this without using the functions file, you can use the streamlined version I came up with. This is the tested code works 100% <form id="file_upload" method="post" action="#...
Lucky's user avatar
  • 91
9 votes

How to upload an image in the plugin's options page

The better title for this would be: How to use the WordPress media uploader to upload images into settings page. 1- Add the necessary scripts to show the media uploader: add_action('admin_footer', ...
Ismail's user avatar
  • 1,936
9 votes
Accepted

How to modify wp.media to get and display multiple images

So, this works. I'd be happy to implement and credit improvements! /** * @Script: WordPress Multiple Image Selection in jQuery * @Version: 0.1 * @Author: CK MacLeod * @Author URI: http://...
CK MacLeod's user avatar
  • 1,857
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 do I select an image from Media Library in my plugin?

Easy for use just only copy paste the code in your required place <?php if ( isset( $_POST['submit_image_selector'] ) && isset( $_POST['image_attachment_id'] ) ) : update_option( '...
Rohit Kaushik's user avatar
8 votes
Accepted

Display image file size in media library

In your theme's functions.php add the following code to get the file size for all of your items in the Media library: add_filter( 'manage_upload_columns', 'wpse_237131_add_column_file_size' ); ...
Ethan O'Sullivan's user avatar
7 votes
Accepted

Proper way to hook wp_get_attachment_url or any other way to change that url in media library

The filter has 2 parameters, also the function name doesn't match. So your code should look like this: add_filter('wp_get_attachment_url', 'clrs_get_attachment_url', 10, 2); function ...
kraftner's user avatar
  • 5,598
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.7k
7 votes
Accepted

Protecting direct access to PDF and ZIP unless user logged in (without plugin)

RewriteCond %{REQUEST_FILENAME} -s RewriteRule ^wp-content/uploads/([^/]*\.(pdf|zip))$ filecheck.php?file=$1 [QSA,L] This actually looks OK, except if you have additional subdirectories within the /...
MrWhite's user avatar
  • 3,881
7 votes

Download external images if post is publish

Scheduled posts don't trigger publish_post, only updating the post itself will do that. Add an action for future_to_publish, see the reference on post status transitions. I don't believe that you'll ...
janh's user avatar
  • 2,728
7 votes
Accepted

How to wp_upload_bits() to a sub-folder?

wp_upload_bits() uses wp_upload_dir() which fires the upload_dir filter allowing you to modify the upload directory's path, sub-directory and URL. So you can use that filter to "force" wp_upload_bits(...
Sally CJ's user avatar
  • 39.2k
6 votes

How to disable Wordpress Media resize different size version?

WordPress has some standard images thumbnail,medium,medium_large,large for which image sizes you can set it under Settings --> Media except medium_large size whose default height set to 768px ( ...
bravokeyl's user avatar
  • 3,359
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.7k
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
Accepted

Media_handle_upload with custom upload folder?

If I'm not mistaken, then it should be possible. You want to use media_handle_upload(), which calls wp_handle_upload() to handle the file upload. wp_handle_upload() on itself won't be helpful, ...
Nicolai Grossherr's user avatar
5 votes

display image size in media library screen

Thanks, I was looking that for a while. And for sizes (meaning weight) add_filter( 'manage_media_columns', 'sk_media_columns_filesize' ); /** * Filter the Media list table columns to add a File ...
Marc's user avatar
  • 51
5 votes

Change default screen option value for media items per page (in media library)

Since 2.9.0 version of wordpress the right way to change default screen option value for media items per page (in media library) is as follows: function my_edit_media_per_page(){ $media_per_page =...
Abdeslam Mellal's user avatar
5 votes
Accepted

How to hide CPT files from media library programmatically

Media items are just like posts with post_type = attachment and post_status = inherit. when we are on upload.php page, we have two views: List View Grid View Grid view is populated via JavaScript ...
bravokeyl's user avatar
  • 3,359
5 votes

How to hide CPT files from media library programmatically

When you create an attachment, do the following: If attachments parent ID is for a post with an excluded post type, do nothing If it isn't in the list of excluded post types, assign it a tag in a ...
Tom J Nowell's user avatar
  • 59.3k
5 votes

How do I select an image from Media Library in my plugin?

So this answer worked perfectly. But in order to make it reusable, I converted the code to a function. So, to use this, you have to first check this out to enqueue the script. And then declare ...
Overcomer's user avatar
  • 151
5 votes

How to delete resized (cropped) image uploads and prevent future resizing?

A majority of the answers covered how to stop creating future default image sizes but this doesnt account for creating any custom sizes in your theme but here is another solution to add to functions....
user9447's user avatar
  • 1,772
5 votes

How to delete resized (cropped) image uploads and prevent future resizing?

You can easily delete files using "find" command on linux, just needed to do the same thing. You can run the following in bash console: cd /var/www/wordpress/wp-content/uploads find -type f -regex '.*...
mwdev's user avatar
  • 151

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