Categories

If File Exists

Author: Scott Reilly
Version: 2.3.1
First released: 2009-06-12
Last update: 2023-08-21
Compatibility: WP 2.7 – 6.3.4
Download: [ zip ]
Description:

Check if a file exists and return true/false or display a string containing information about the file.

Extended Description

This plugin provides the functions c2c_if_file_exists(), c2c_if_theme_file_exists(), c2c_if_plugin_file_exists() that check if a file exists and either return true/false or display a string containing information about the file.

  • If a format string is not passed to it, the functions return a simple boolean (true or false) indicating if the specified file exists.
  • Otherwise, the format string provided to it will be used to construct a response string, which can be customized to display information about the file (such as file_name, file_url, or file_path). If the $echo argument is true, that string is displayed on the page. Regardless of the value of $echo, the response string is returned by the function.

By default, c2c_if_file_exists() assumes you are looking for the file relative to the default WordPress upload directory. If you wish to search another directory, specify it as the $dir argument. c2c_if_theme_file_exists() assumes you are looking for a file relative to the currently active theme’s home directory. c2c_if_plugin_file_exists() assumes you are looking for a file relative to the directory that contains WordPress plugins.

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_if_file_exists() : Checks if a file exists and returns true/false or displays a string containing information about the file.
  • c2c_if_plugin_file_exists() : Checks if a file exists (relative to the plugins directory) and returns true/false or displays a string containing information about the file.
  • c2c_if_theme_file_exists() : Checks if a file exists (relative to the current theme’s directory) and returns true/false or displays a string containing information about the file. If the current theme is a child theme, then the function will check if the file exists first in the child theme’s directory, and if not there, then it will check the parent theme’s directory.

Theses are the hooks provided by the plugin:

  • c2c_if_file_exists : Filter that allows use of an alternative approach to safely invoke c2c_if_file_exists() 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_if_plugin_file_exists : Filter that allows use of an alternative approach to safely invoke c2c_if_plugin_file_exists() 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_if_theme_file_exists : Filter that allows use of an alternative approach to safely invoke c2c_if_theme_file_exists() 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

  1. Install via the built-in WordPress plugin installer. Or install the plugin code inside the plugins directory for your site (typically /wp-content/plugins/).
  2. Activate the plugin through the ‘Plugins’ admin menu in WordPress
  3. In one or more of your templates, utilize one of the template tags provided by this plugin (see examples)

Release Log

2.3.1 (2023-05-18)

  • Change: Note compatibility through WP 6.3+
  • Change: Update copyright date (2023)
  • New: Add a potential TODO item

2.3 (2021-10-09)

  • Change: Remove support for long-deprecated if_file_exists()
  • New: Add DEVELOPER-DOCS.md and move template tags and hooks documentation into it
  • Change: Note compatibility through WP 5.8+
  • Change: Tweak installation instruction
  • Change: Pare down tags in readme.txt header
  • Unit tests:
    • New: Add dataProvider get_file_formatting_placeholders() and use it instead of explicitly listing assertions for each placeholder
    • 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: Remove get_echo_output() and replaces its use with built-in expectOutputRegex()
    • Change: In bootstrap, store path to plugin file constant
    • Change: In bootstrap, add backcompat for PHPUnit pre-v6.0

2.2.10 (2021-04-14)

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

Copyright & Disclaimer

Copyright © 2009-2024 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.

One reply on “If File Exists”