2

I am migrating a client website orginally created in ASP into a WordPress driven CMS. The orginal site has about 200 separate directories, each with photos for a specific event and each directory is named with a unique number.

In the orginal site, there was an old (8 years +) JavaScript that would parse thru a given directory and create a Lightbox slideshow for it on the page (In additional, for each image within one of these event directories was a text file TXT with the same file name as each image which was used to create a caption - not that important, but worth mentioning). Each event directory has a different number of total images within each.

The goal will be to allow Editor users of the new WordPress site going forward to create gallery themselves using WP Bakery and the Image Gallery element, which is no problem.

My current challenge (and question) pertains to these past events...

I need to figure out:

  • How to incorprate a simple and current script (JavaScript) that can parse thru a given directory and return all the file names (ideally using the same lightbox as the WordPress/WP Bakery site does)

  • Write a WordPress loop to generate the corresponding WB Bakery slideshow code

So for example, I have manually uploaded via FTP a folder called 345 into the WordPreds Upload directory:

/wp-content/uplaods/show/345

In the dirctory 345 there are several images:

image01.jpg image02.jpg image03.jpg image04.jpg etc

Assuming at had a variable:

$LEGACYSHOWID = filename of the specfic show images directory (i.e. 345)

The code that WB Bakery genearted when a Slideshow is manually created is:

<ul class="slides">

<li style="width: 100%; float: left; margin-right: -100%; position: relative; opacity: 0; display: block; z-index: 1;" class="" data-thumb-alt="">

<a class="" href="https://DOMAINNAME/wp-content/uploads/SHOW/$LEGACYSHOWID/FILENAME01.jpg" data-lightbox="lightbox[rel-1949-3827996796]"><img class="" src="https://DOMAINNAME/wp-content/uploads/SHOW/$LEGACYSHOWID/FILENAME01.jpg-800x400.jpg" width="800" height="400" alt="FILENAME01" title="FILENAME01" draggable="false"></a></li>
</a>
</li>


<li style="width: 100%; float: left; margin-right: -100%; position: relative; opacity: 0; display: block; z-index: 1;" class="" data-thumb-alt="">

<a class="" href="https://DOMAINNAME/wp-content/uploads/SHOW/$LEGACYSHOWID/FILENAME02.jpg" data-lightbox="lightbox[rel-1949-3827996796]"><img class="" src="https://DOMAINNAME/wp-content/uploads/SHOW/$LEGACYSHOWID/FILENAME02.jpg-800x400.jpg" width="800" height="400" alt="FILENAME02" title="FILENAME02" draggable="false"></a></li>
</a>
</li>

(and so on...)

</ul>

I hope what I am asking makes sense. Can anyone offer any suggestions or point me in the right direction?

omarkoza
  • 41
  • 5

1 Answers1

1

you want to use the glob function. so add this to your functions.php

function get_images( $folder ){
        $base_directory = trailingslashit( get_template_directory() );
        $directory      = $folder;
        $images   = glob( $base_directory . $directory . '*.jpg');
        $output = array();
        foreach($images as $image) {
            $url = get_theme_file_uri($directory.basename($image));
            $output[] = $url;
        }
        
        return $output;
    }

then in your theme:

<?php $images = get_images('345/'); ?>
<?php if( $images ): ?>
        
        <ul class="slides">
            <?php foreach($images as $image): ?>
                <li>
                    <a class="" href="<?php echo esc_url($image); ?>">
                        <img class="" src="<?php echo esc_url($image); ?>">
                    </a>
                </li>
            <?php endforeach; ?>
        </ul>
        
    <?php endif; ?>

based on your comments to get the specific folder for each iteration:

    function get_images_from_directory( $folder, $directory ){
        $output = array();
        
        $upload_dir   = wp_get_upload_dir();
        $base_directory = trailingslashit($upload_dir['basedir'] . '/' . $directory);
        $images = glob( $base_directory . $folder . '*.jpg');
        
        foreach($images as $image) {
            $url = untrailingslashit($upload_dir['baseurl']. '/' . $directory . $folder . basename($image));
        
            $output[] = $url;
        }
        
        return $output;
   }

then in the theme:

<?php $folder   = '110/'; ?>
<?php $directory = 'shows/'; ?>

<?php $images = get_images_from_directory($folder, $directory); ?>
<?php if( $images ): ?>

        <ul class="slides">
            <?php foreach($images as $image): ?>
                <li>
                    <a class="" href="<?php echo esc_url($image); ?>">
                        <img class="" src="<?php echo esc_url($image); ?>">
                    </a>
                </li>
            <?php endforeach; ?>
        </ul>

    <?php endif; ?>
Moishy
  • 3,560
  • 3
  • 23
  • 42
  • First of all thank you so much for your answer! I used your code exact as presented and it worked as is, but I have another questions: – omarkoza Oct 16 '22 at 02:46
  • Using get_template_directory assumes that my images directory will live in the parent theme folder. I am using a child theme so I assume IF I wanted to store my additional images folder with the child theme folder I would use get_stylesheet_directory. HOWEVER I wish the locationl to be theme independent & live out side a theme folder. Specially I which to store the folder for each show (show_110, show_111, show_112 etc) to live in a folder names "show" inside the main Uploads folder fo WordPRess (/wp-content/uploads/show) . Would I use wp_upload_dir and if so, what would the proper syntax be? – omarkoza Oct 16 '22 at 02:54
  • I have been trying variations of wp_upload_dir but nothing seems to work – omarkoza Oct 19 '22 at 03:35
  • I updated my answer accordingly – Moishy Oct 24 '22 at 18:08
  • 1
    Thanks Moisy!!! You're the best! It worked. I see now I was missing the untrailingslashit for my function when I was attempting. Thanks again! – omarkoza Oct 27 '22 at 03:45
  • Great! feel free to accept the answer – Moishy Oct 28 '22 at 01:16