Deze plugin is niet getest met de laatste drie grote versies van WordPress. Mogelijk wordt het niet meer onderhouden of ondersteund. Er kunnen ook compatibiliteitsproblemen ontstaan wanneer het wordt gebruikt met recentere versies van WordPress.

Random File


This plugin provides template tags that allow you to retrieve the name, path (relative or absolute), url, or fully marked-up link to a randomly chosen file or files in a specified directory.

Arguments to the functions permit you to specify limit what file(s) can be randomly selected based on a given set of file extensions. You can also explicitly specify files that should not be randomly selected.

This functionality can be useful for displaying random images/logos or including text from random files onto your site (writing excerpts, multi-line quotes, etc). Other ideas: random ads, random CSS files, random theme template selection.


  • If you want to actually display the name of the random file, be sure to ‘echo’ the results:

  • Unless you limit the file search to only include a particular extension (via $extensions argument), all files in the specified $dir will be under consideration for random selection

  • Can be run inside or outside of “the loop”

Links: Plugin Homepage | Plugin Directory Page | GitHub | Author Homepage

Template Tags

The plugin provides two optional template tag for use in your theme templates.


  • <?php function c2c_random_file( $dir, $extensions = '', $reftype = 'relative', $exclusions = array() ) ?>
    This retrieves the name of a random file from a specified directory and returns information based on the file according to the $reftype value.

  • <?php function c2c_random_files( $number, $dir, $extensions = '', $reftype = 'relative', $exclusions = array() ) ?>
    This retrieves the name, path, or link to a specified number of randomly chosen files in a specified directory. All but the $number argument are passed along in calls to c2c_random_file().


  • $number (only for c2c_random_files())
    The number of random files to select from the specified directory. If less files are present in the specified directory, then all files in the directory will be returned (but will be listed in random order).

  • $dir
    The directory to search for a random file. The directory must exist at the directory structure level of your WordPress installation or below. (i.e., if your site is installed on your server at /usr/local/htdocs/, then the directory of random files you specified will assume that as its base… so a value of 'randomfiles' would be assumed to actually be: /usr/local/htdocs/

  • $extensions
    Optional argument. A space-separated list of extensions (case insensitive), i.e. ‘jpg gif png jpeg’.

  • $reftype
    Optional argument. Can be one of the following: ‘relative’ (which is the default), ‘absolute’, ‘url’, ‘filename’, ‘hyperlink’. See Examples section for more details and examples.

  • $exclusions
    Optional argument. If specified, MUST be an array of filenames to exclude from consideration as a random file.


  • The reference to the randomly selected file can be returned in one of five ways:
    [Assume your WordPress installation is at and you’ve invoked c2c_random_file('random/', 'txt', $reftype)]

    $reftype = ‘relative’

    => A location relative to the primary domain:


    [This is the default setting as it is the most applicable. Relative referencing is necessary if the random file is to be used as an argument to include() or virtual(). It’s also a valid way to reference a file for A HREF= and IMG SRC= linking.]

    $reftype = 'absolute'

    => An absolute location relative to the root of the server’s file system:


    $reftype = ‘url’

    => The URL of the random file:

    [If you desire the use of full URL, such as for a A HREF= or IMG SRC= link.]

    $reftype = ‘filename’

    => The filename of the random file:


    $reftype = ‘hyperlink’

    => The filename of the random file hyperlinked to that random file:

    <a href='' title='randomfile.txt'>randomfile.txt</a>
  • Include random logo or image on your site:

    <img alt="logo" class="logo" src="<?php echo c2c_random_file( 'wp-content/images/logos/' ); ?>" />
  • Insert text from a random file (i.e. for random multi-line quotes) (Apache web-server only, probably):

  • If you wanted to source a random .php file:

    <?php include( c2c_random_file( '/randomphp', 'php' ) ); ?>
  • List 5 random files:

      $random_files = c2c_random_files( 5, 'wp-content/files' );
      foreach ( $random_files as $f ) {
      echo "

    • $f
    • “;


The plugin exposes two filters for hooking. Typically, customizations utilizing these hooks would be put into your active theme’s functions.php file, or used by another plugin.

c2c_random_file (filter)

The ‘c2c_random_file’ hook allows you to use an alternative approach to safely invoke c2c_random_file() in such a way that if the plugin were deactivated or deleted, then your calls to the function won’t cause errors in your site.


  • same as for c2c_random_file()


Instead of:

<?php $file = c2c_random_file( 'wp-content/randomfiles' ); ?>


<?php $file = apply_filters( 'c2c_random_file', 'wp-content/randomfiles' ); ?>

c2c_random_files (filter)

The ‘c2c_random_files’ hook allows you to use an alternative approach to safely invoke c2c_random_files() in such a way that if the plugin were deactivated or deleted, then your calls to the function won’t cause errors in your site.


  • same as for c2c_random_files()


Instead of:

<?php $files = c2c_random_files( 5, 'wp-content/randomfiles' ); ?>


<?php $files = apply_filters( 'c2c_random_files', 5, 'wp-content/randomfiles' ); ?>


  1. Install via the built-in WordPress plugin installer. Or download and unzip inside the plugins directory for your site (typically wp-content/plugins/)
  2. Activate the plugin through the ‘Plugins’ admin menu in WordPress
  3. Make use of the c2c_random_file() or c2c_random_files() template function in your code or template (see examples below).


Does this plugin do dynamic random rotation within a loaded page (i.e. randomly rotating images within a loaded page)?

No. This plugin only selects a random file when the page is loaded. Once loaded, it does not currently add any dynamic functionality to automatically retrieve another random file on its own.

Does this plugin include unit tests?



Er zijn geen beoordelingen voor deze plugin.

Bijdragers & ontwikkelaars

“Random File” is open source software. De volgende personen hebben bijgedragen aan deze plugin.


Vertaal “Random File” naar jouw taal.

Interesse in ontwikkeling?

Bekijk de code, haal de SVN repository op, of abonneer je op het ontwikkellog via RSS.


1.8.11 (2021-09-18)

  • Change: Note compatibility through WP 5.8+
  • Unit tests:
    • Change: Restructure unit test directories
      • Change: Move phpunit/ into tests/
      • Change: Move phpunit/bin into tests/
    • Change: Remove ’test-‘ prefix from unit test file
    • Change: In bootstrap, store path to plugin file constant
    • Change: In bootstrap, add backcompat for PHPUnit pre-v6.0

1.8.10 (2021-04-23)

  • Change: Note compatibility through WP 5.7+
  • Change: Update copyright date (2021)

1.8.9 (2020-08-31)

  • New: Add and move existing TODO list from top of main plugin file into it
  • Change: Restructure unit test file structure
    • New: Create new subdirectory phpunit/ to house all files related to unit testing
    • Change: Move bin/ to phpunit/bin/
    • Change: Move tests/bootstrap.php to phpunit/
    • Change: Move tests/ to phpunit/tests/
    • Change: Rename phpunit.xml to phpunit.xml.dist per best practices
  • Change: Note compatibility through WP 5.5+
  • New: Add FAQ indicating presence of unit tests
  • Change: Unit tests: Change regex delimiters from / to ~ to avoid having to escape frequent use of / in patterns

Full changelog is available in