How can I bulk delete media and attachments using WP-CLI?
From the WP-CLI documentation about wp post delete: wp post delete –force $(wp post list –post_type=”attachment” –format=ids) See wp post list for additional information.
From the WP-CLI documentation about wp post delete: wp post delete –force $(wp post list –post_type=”attachment” –format=ids) See wp post list for additional information.
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 wp_get_attachment_image( get_the_ID(), ‘thumbnail’, “”, [“class” => “my-custom-class”] ); ?> The main advantage of this aproach is that you will also get the … Read more
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.png’); // Get the path to the upload directory. $wp_upload_dir = wp_upload_dir(); foreach($images as $name) { $attachment = array( ‘guid’=> $wp_upload_dir[‘url’] . “https://wordpress.stackexchange.com/” … Read more
For the first one, you can add ‘exclude’ => get_post_thumbnail_id() as a parameter (as shown here). For the second one, you can add ‘post_mime_type’ => ‘application/pdf’, but I’m not sure that would always work, afaik, pdfs have more than one mime type.
You can checkout this link https://codex.wordpress.org/Javascript_Reference/wp.media jQuery(function($){ // Set all variables to be used in scope var frame, metaBox = $(‘#meta-box-id.postbox’), // Your meta box id here addImgLink = metaBox.find(‘.upload-custom-img’), delImgLink = metaBox.find( ‘.delete-custom-img’), imgContainer = metaBox.find( ‘.custom-img-container’), imgIdInput = metaBox.find( ‘.custom-img-id’ ); // ADD IMAGE LINK addImgLink.on( ‘click’, function( event ){ event.preventDefault(); // If … Read more
I would strongly advise against using $post->guid – WordPress now generates them in the form; http:/example.com/?attachment_id=ID Use the same method that many of the attachment-related functions use; $filename = basename ( get_attached_file( $data->ID ) );
This will allow you to rename an attachment as soon as its uploaded: add_action(‘add_attachment’, ‘rename_attachment’); function rename_attachment($post_ID){ $file = get_attached_file($post_ID); $path = pathinfo($file); //dirname = File Path //basename = Filename.Extension //extension = Extension //filename = Filename $newfilename = “NEW FILE NAME HERE”; $newfile = $path[‘dirname’].”https://wordpress.stackexchange.com/”.$newfilename.”.”.$path[‘extension’]; rename($file, $newfile); update_attached_file( $post_ID, $newfile ); }
Edit: 12/09/2017 See this answer for a more up to date solution to this: How to use taxonomies on attachments with the new Media Library? I’m going to answer my own question here as I have managed to figure out a solution to what I’ve been trying to do. I came to the conclusion that … Read more
I was part of the panic show. Happy to say that we can stop the panic, there is a way to view this by choosing “Uploaded to this post” option in dropdown in Media Library: (props to Michael Fields)
I recently did some research for a client project recently so lo-and-behold I get to use it here! After the text you’ll see a categorized list of most (all?) of the image handling functions from within WordPress 3.0.1 (I grouped them in some semblance of order but don’t put too much credence in my categorization.) … Read more