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 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

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.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

Get all post attachments except featured image

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.

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, ‘attachment’ ) || strpos( $query, ‘attachment’ ) ) { unset( $rules[ $regex ] ); } } return $rules; } add_filter( ‘rewrite_rules_array’, ‘cleanup_default_rewrite_rules’ ); Don’t forget to … Read more

Delete Associated Media Upon Page Deletion

How about this? It adapts an example on the get_posts() function reference page. function delete_post_media( $post_id ) { $attachments = get_posts( array( ‘post_type’ => ‘attachment’, ‘posts_per_page’ => -1, ‘post_status’ => ‘any’, ‘post_parent’ => $post_id ) ); foreach ( $attachments as $attachment ) { if ( false === wp_delete_attachment( $attachment->ID ) ) { // Log failure … Read more

Delete original image – keep thumbnail?

add_filter( ‘wp_generate_attachment_metadata’, ‘delete_fullsize_image’ ); function delete_fullsize_image( $metadata ) { $upload_dir = wp_upload_dir(); $full_image_path = trailingslashit( $upload_dir[‘basedir’] ) . $metadata[‘file’]; $deleted = unlink( $full_image_path ); return $metadata; }

Remove Dimension from wp_get_attachment_image

Your arguments for both wp_get_attachment_image_url() and wp_get_attachment_image() are in the wrong order – check the linked documentation for details. Additionally, wp_get_attachment_image_url() returns a URL – not an actual image element. Removing the width and height attributes from <img> elements is inadvisable: if the layout of the page is in any way influenced by the size … Read more

Hata!: SQLSTATE[HY000] [1045] Access denied for user 'divattrend_liink'@'localhost' (using password: YES)