WordPress.org

Forums

Random Full-Size BG Image Code for plugin creation (8 posts)

  1. visceral concepts
    Member
    Posted 1 year ago #

    I asked this on StackOverflow as well and haven't received an answer.

    I'm having an issue creating a full screen background for my WordPress theme that uses random images from the image library. I want to write a PHP function that can be used on multiple sites, so I can't simply use the direct path in the code. It also needs to work on MultiSite in such a way that it only pulls images that are uploaded to that site. Here's the code I'm working with:
    HTML for my Background Div

    <div id="background" class="background" style="background-image:url(<?php displayBackground();?>);">
    </div>

    PHP to randomize my image folder

    <? php
    function displayBackground()
    {
    $uploads = wp_upload_dir();
    $img_dir = ( $uploads['baseurl'] . $uploads['subdir'] );
    $cnt = 0;
    $bgArray= array();
    
            /*if we can load the directory*/
    if ($handle = opendir($img_dir)) {
    
        /* Loop through the directory here */
        while (false !== ($entry = readdir($handle))) {
    
        $pathToFile = $img_dir.$entry;
        if(is_file($pathToFile)) //if the files exists
        {   
    
            //make sure the file is an image...there might be a better way to do this
            if(getimagesize($pathToFile)!=FALSE)
            {
                //add it to the array
                $bgArray[$cnt]= $pathToFile;
                $cnt = $cnt+1;
    
            }
    
        }   
    
    }
    //create a random number, then use the image whos key matches the number
    $myRand = rand(0,($cnt-1));
    $val = $bgArray[$myRand];
    
    }
    closedir($handle);
    echo('"'.$val.'"');
    
    }

    I know that my CSS markup is correct because if I give the DIV a fixed image location, I get a fullscreen image. Can anyone tell me what to do to fix it?

    Also, I'm doing it this way because I don't like the way the WPSupersize.js script affects the site styling.

  2. There aren't any images in the upload directory, though. I mean, there ARE, but they're in subfolders.

    Does this work on single installs of WP? The logic should be exactly the same, since wp_upload_dir is going to dynamically change per site (the locations are all site defined, so if I call it in site 2 it'll be /wp-content/uploads/sites/2/ and so on)

  3. visceral concepts
    Member
    Posted 1 year ago #

    In theory, it's supposed to, but it doesn't. I'm certain I've done something wrong writing the code, possibly having to do with the use of the wp_upload_dir functions, but I'm not sure.

    I found the original script, designed for sites not built on WordPress here (http://apexlogic.net/code-bank/php/random-background-images-with-css-and-php/) and modified it, so that may be at the core of the problem.

  4. In theory, it's supposed to, but it doesn't

    Okay, so what DOES it do? What is it spitting out? :)

  5. visceral concepts
    Member
    Posted 1 year ago #

    It (I assume) isn't finding the image, because I'm not getting a background image, just a white background, and there are several images uploaded, both in the MultiSite version and in the single install. It also doesn't spit out any error, so the function at least thinks it's producing a result.

  6. Have it just echo the path then to see what you're getting :)

  7. visceral concepts
    Member
    Posted 1 year ago #

    I'm definitely doing something wrong with the function, because it isn't echoing anything back... Check this code for me?

    <?php function imageDir() {
        $uploads = wp_upload_dir();
        $img_dir = ( $uploads['baseurl'] . $uploads['subdir'] );
    };
    ?>
    <?php echo imageDir(); ?>
  8. Well for one, there's no return.

    <?php function imageDir() {
        $uploads = wp_upload_dir();
        $img_dir = ( $uploads['baseurl'] . $uploads['subdir'] );
        return $img_dir;
    };
    ?>

Topic Closed

This topic has been closed to new replies.

About this Topic