0

I Want to loop all attachment url and save new html. Sorry, I only script kiddies. I only understand some code.

This my script php

function get_all_image_clickable($content) {
global $post;
$args_img = array(
    'order'          => 'ASC',
    'post_type'      => 'attachment',
    'post_parent'    => $post->ID,
    'post_mime_type' => 'image',
    'post_status'    => null,
    "what_to_show"   =>"posts",
    "showposts"      =>-1,
    "post_mime_type" =>"image"
);
$attachments = get_posts($args_img);
$i = 0;
$dom = new DOMDocument('UTF-8');
@$dom->loadHTML( utf8_decode($content) ); // Decode to simple ISO to avoid accent errors
$dom->preserveWhiteSpace = false;
$images = $dom->getElementsByTagName('img');
if( count($images) > 0 ) {
    foreach ($images as $image) {
        $width = $image->getAttribute('width'); // get the widths of each image
        if( $width >= 860) { // if image is less than 860, add their old classes back in plus our new class
            $clone_img = $image->cloneNode(); //clone node images
            // Create DIV container

            foreach ($attachments as $attachment) {
                $parent = get_post($post->post_parent);
                if($parent->post_status == "publish"){
                    $image_wrap =  $dom->createElement('a');
                    $image_wrap->setAttribute('class', 'img-gallery' );
                    $image_wrap->setAttribute('href', get_attachment_link($attachment->ID));
                }
                $i++;
                wp_reset_postdata();
            }
            $image_wrap->appendChild( $clone_img ); // Add to image to new container
            $image->parentNode->replaceChild( $image_wrap, $image ); // Replace img object with all new elements
        }
    } // end if count
    $content = $dom->saveHTML();
}
return $content;

} add_filter( 'the_content' , 'get_all_image_clickable' , 15 );

i want to get all attachment url on its own image. Sorry, my english bad.

  • Please trim your code to make it easier to find your problem. Follow these guidelines to create a [minimal reproducible example](https://stackoverflow.com/help/minimal-reproducible-example). – Community Nov 19 '22 at 09:28

0 Answers0