Title: Adding a PHP function to all wordpress pages
Last modified: August 19, 2016

---

# Adding a PHP function to all wordpress pages

 *  [timglen22](https://wordpress.org/support/users/timglen22/)
 * (@timglen22)
 * [17 years, 2 months ago](https://wordpress.org/support/topic/adding-a-php-function-to-all-wordpress-pages/)
 * Hi! I’ve just finished configuring my new wordpress blog but wanted to add some
   functionality to it. I need to randomly get an image from a directory and display
   it on a page! Unfortunately I couldn’t find a plugin to fit my needs so I found
   the following PHP script and did a bit of editing to make it work for me. However,
   I don’t know where I can add it so that it is accessible through all of my wordpress
   pages D:! I tried to add it to the function.php file, but that simply broke my
   blog, and it didn’t display anything. Is there an easy way that I can add this
   to one of the core or template specific .php files so that I can call the previewImage()
   function at will in my posts?
 * Thanks in advance for any help or advice!
 * Heres mah script:
    <?php
 * function previewImage($directory){
    $folder = $directory;
 * // Space seperated list of extensions, you probably won’t have to change this.
   
   $exts = ‘jpg jpeg png gif’;
 * $files = array(); $i = -1; // Initialize some variables
    if (” == $folder) $folder
   = ‘./’;
 * $handle = opendir($folder);
    $exts = explode(‘ ‘, $exts); while (false !== ($
   file = readdir($handle))) { foreach($exts as $ext) { // for each extension check
   the extension if (preg_match(‘/\.’.$ext.’$/i’, $file, $test)) { // faster than
   ereg, case insensitive $files[] = $file; // it’s good ++$i; } } } closedir($handle);//
   We’re not using it anymore mt_srand((double)microtime()*1000000); // seed for
   PHP < 4.2 $rand = mt_rand(0, $i); // $i was incremented as we went along
 * $random_pic = $folder.$files[$rand];
    } ?>

Viewing 1 replies (of 1 total)

 *  [MichaelH](https://wordpress.org/support/users/michaelh/)
 * (@michaelh)
 * [17 years, 2 months ago](https://wordpress.org/support/topic/adding-a-php-function-to-all-wordpress-pages/#post-1051288)
 * May need to add it to the appropriate templates used by your theme. Review [Template Hierarchy](http://codex.wordpress.org/Template_Hierarchy)
   to get a feel for what templates your theme uses.

Viewing 1 replies (of 1 total)

The topic ‘Adding a PHP function to all wordpress pages’ is closed to new replies.

## Tags

 * [configure](https://wordpress.org/support/topic-tag/configure/)
 * [functions](https://wordpress.org/support/topic-tag/functions/)
 * [pages](https://wordpress.org/support/topic-tag/pages/)
 * [php](https://wordpress.org/support/topic-tag/php/)

 * 1 reply
 * 2 participants
 * Last reply from: [MichaelH](https://wordpress.org/support/users/michaelh/)
 * Last activity: [17 years, 2 months ago](https://wordpress.org/support/topic/adding-a-php-function-to-all-wordpress-pages/#post-1051288)
 * Status: not resolved

## Topics

### Topics with no replies

### Non-support topics

### Resolved topics

### Unresolved topics

### All topics
