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

Viewing 7 replies - 1 through 7 (of 7 total)
  • Moderator Ipstenu (Mika Epstein)

    (@ipstenu)

    🏳️‍🌈 Advisor and Activist

    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)

    Thread Starter visceral concepts

    (@visceral-concepts)

    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.

    Moderator Ipstenu (Mika Epstein)

    (@ipstenu)

    🏳️‍🌈 Advisor and Activist

    In theory, it’s supposed to, but it doesn’t

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

    Thread Starter visceral concepts

    (@visceral-concepts)

    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.

    Moderator Ipstenu (Mika Epstein)

    (@ipstenu)

    🏳️‍🌈 Advisor and Activist

    Have it just echo the path then to see what you’re getting 🙂

    Thread Starter visceral concepts

    (@visceral-concepts)

    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(); ?>

    Moderator Ipstenu (Mika Epstein)

    (@ipstenu)

    🏳️‍🌈 Advisor and Activist

    Well for one, there’s no return.

    <?php function imageDir() {
        $uploads = wp_upload_dir();
        $img_dir = ( $uploads['baseurl'] . $uploads['subdir'] );
        return $img_dir;
    };
    ?>
Viewing 7 replies - 1 through 7 (of 7 total)
  • The topic ‘Random Full-Size BG Image Code for plugin creation’ is closed to new replies.