- Author: Scott Reilly
- Version: 2.0
- First released: 2004-12-21
- Last update: 2024-08-13
- Compatibility: WP 2.8 – 6.6.2
- Download: [ zip ]
- Description:
Retrieve the name, path, or link to a randomly chosen file or files in a specified directory.
-
Extended Description
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 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.
Notes:
-
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
Developer Documentation
Developer documentation can be found in DEVELOPER-DOCS.md. That documentation covers the template tags and hooks provided by the plugin.
As an overview, these are the template tags provided by the plugin:
c2c_random_file()
: Retrieves the name of a random file from a specified directory and returns information based on the file.c2c_random_files()
: Retrieves the name, path, or link to a specified number of randomly chosen files in a specified directory.
Theses are the hooks provided by the plugin:
c2c_random_file
: Filter to safely invokec2c_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.c2c_random_files
: Filter to safely invokec2c_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.
Find out more at the plugin’s WordPress Plugin Repository page.
-
-
Installation
- Install via the built-in WordPress plugin installer. Or download and unzip
random-file.zip
inside the plugins directory for your site (typicallywp-content/plugins/
) - Activate the plugin through the ‘Plugins’ admin menu in WordPress
- Make use of the
c2c_random_file()
orc2c_random_files()
template function in your code or template (see examples below).
- Install via the built-in WordPress plugin installer. Or download and unzip
-
Release Log
2.0 (2024-08-13)
- Change: Switch to using
wp_rand()
for more reliable randomization - Change: Allow
$extensions
argument to also accept an array of extensions - Change: Strip surrounding whitespace and leading periods from provided extensions
- Change: Explicitly return false if no file could be found
- Change: Switch to use a switch statement, which is more concise
- Change: Use cleaner
sprintf()
approach to outputting markup - Hardening: Escape text shown via ‘hyperlink’ output (though it’s never anything other than plaintext)
- Change: Note compatibility through WP 6.6+
- Change: Update copyright date (2024)
- Change: Tweak filter descriptions in
readme.txt
- New: Add
.gitignore
file - Change: Remove development and testing-related files from release packaging
- Unit tests:
- Hardening: Prevent direct web access to
bootstrap.php
- Allow tests to run against current versions of WordPress
- New: Add more unit tests
- New: Add
composer.json
for PHPUnit Polyfill dependency - Change: In bootstrap, store path to plugin directory in a constant
- Change: Rename a test
- Hardening: Prevent direct web access to
1.8.12 (2023-05-21)
- New: Add DEVELOPER-DOCS.md and move hooks documentation into it
- Change: Note compatibility through WP 6.3+
- Change: Update copyright date (2023)
1.8.11 (2021-09-18)
- Change: Note compatibility through WP 5.8+
- Unit tests:
- Change: Restructure unit test directories
- Change: Move
phpunit/
intotests/
- Change: Move
phpunit/bin
intotests/
- Change: Move
- 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
- Change: Restructure unit test directories
- Change: Switch to using
-
Copyright & Disclaimer
Copyright © 2004-2025 by Scott Reilly (aka coffee2code)This program is free software; you can redistribute it and/or
modify it under the terms of the GNU General Public License
as published by the Free Software Foundation; either version 2
of the License, or (at your option) any later version.This program is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU General Public License for more details.You should have received a copy of the GNU General Public License
along with this program; if not, write to the Free Software
Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA. -
Discussion / Support
Have any questions, comments, or suggestions? Please provide them via the plugin’s WordPress.org support forum. I’ll do my best to reply in a timely fashion and help as best I can.
Unfortunately, I cannot provide guaranteed support, nor do I provide support via any other means.
Was this plugin useful useful to you? Consider giving it a rating. If you’re inclined to give it a poor rating, please first post to the support forum to give me a chance to address or explain the situation.
Categories
7 replies on “Random File”
[…] coffee2code.com » Random File WP plugin to cycle thru random images or any random file in a specified directory. (tags: plugins wordpress images useful) […]
[…] WordPress Plugin Random File von Scott Reilly holt den nAmen, Pfad oder Link zu einer zufällig ausgewählten Datei in […]
[…] Random File 1.5 Retrieve the name, path, or link to a randomly chosen file in a specified directory. This is my favorite plugin, because it’s the one that allows ads and affiliate offers to be displayed in between posts and at the end of posts. Essentially, I have a folder with HTML files, banners, text files with affiliate links, etc. This plugin will randomly retrieve them, which I can add wherever I want within my blog’s templates. […]
[…] Random FileRetrieve the name, path, or link to a randomly chosen file in a specified directory. […]
[…] plugin that displays new verses daily. I set up an inspirational picture to go with the verse. The picture rotates so it makes a static column look dynamic. Video: Use embedded video to spruce up the site. Helps […]
[…] o plugin é fácil, basta seguir o procedimento padrão de fazer download do arquivo Random File nesta página, descompactar e trasnferí-lo para a pasta wp-content/plugins e ativá-lo. Feito isso, basta […]
[…] Random FileRetrieve the name, path, or link to a randomly chosen file in a specified directory. […]