If File Exists

Mô tả

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

Template Tags

The plugin provides three template tags for use in your theme templates.

Functions

  • <?php function c2c_if_file_exists($filename, $format = '', $echo = true, $dir = '') ?>
    Checks if a file exists and returns true/false or displays a string containing information about the file.
  • <?php function c2c_if_plugin_file_exists( $filename, $format = '', $echo = true, $dir = '', $show_if_not_exists = '' ) ?>
    Checks if a file exists (relative to the plugins directory) and returns true/false or displays a string containing information about the file.
  • <?php function c2c_if_theme_file_exists( $filename, $format = '', $echo = true, $dir = '', $show_if_not_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.

Arguments

  • $filename
    String. Name of the filename whose existence is being checked. Do not include path information.

  • $format
    (optional) String. Text to be displayed or returned when $filename exists. Leave blank to return true or false. The following percent-tag substitutions are available for optional use in the $format string:

    • %file_directory% : the directory of the file, i.e. “/usr/local/www/yoursite/wp-content/uploads/”
    • %file_extension% : the extension of the file, i.e. “zip”`
    • %file_name% : the name of the file, i.e. “pictures.zip”
    • %file_url% : the URL of the file, i.e. “http://yoursite.com/wp-content/uploads/pictures.zip”
    • %file_path%: the filesystem path to the file, i.e. “/usr/local/www/yoursite/wp-content/uploads/pictures.zip”
  • $echo
    (optional) Boolean. Should $format be echoed when the filename exists? NOTE: the string always gets returned unless file does not exist). Default is true.

  • $dir
    (optional) String|Boolean. The directory (relative to the root of the site) to check for $filename. If empty, the WordPress upload directory is assumed (if using c2c_if_file_exists()). If ‘true’, then it indicates the filename includes the directory.

  • $show_if_not_exists
    (optional) String. Text to display if the file does not exists. $format must also be specified. Format is the same as $format argument.

Ví dụ

  • <?php
    $format = "<a href='%file_url%'>Download %file_name% now!</a>";
    $file_name = 'pictures-' . get_the_ID() . '.zip';
    c2c_if_file_exists($file_name, $format);
    ?>

  • <?php
    if ( c2c_if_file_exists($file_name) ) {
    // Do stuff here
    }
    ?>

  • <?php c2c_if_file_exists($file_name, '%file_name% exists!'); ?>

  • <?php c2c_if_file_exists($file_name, '%file_name% also exists in upload2 directory', true, 'wp-content/uploads2'); ?>

  • <?php c2c_if_file_exists($file_name, '%file_name% also exists in upload2 directory', true, 'wp-content/uploads2', '%file_name% did not exist!'); ?>

  • <?php c2c_if_plugin_file_exists('akismet.php', 'Akismet is present', true, 'akismet'); ?>

  • <?php c2c_if_plugin_file_exists('akismet/akismet.php', 'Akismet is present', true, true); ?>

  • <?php c2c_if_theme_file_exists('home.php', 'Home template is present', true, '', 'Home template does not exist.'); ?>

Hooks

The plugin exposes three filters for hooking. Code using these filters should ideally be put into a mu-plugin or site-specific plugin (which is beyond the scope of this readme to explain). Less ideally, you could put them in your active theme’s functions.php file.

c2c_if_file_exists (filter)

The ‘c2c_if_file_exists’ hook allows you to use 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.

Arguments:

  • same as for c2c_if_file_exists()

ví dụ:

Instead of:

<?php c2c_if_file_exists( $file, '%file_url%' ); ?>

Do:

<?php apply_filters( 'c2c_if_file_exists', $file, '%file_url%' ); ?>

c2c_if_plugin_file_exists (filter)

The ‘c2c_if_plugin_file_exists’ hook allows you to use 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.

Arguments:

  • same as for c2c_if_plugin_file_exists()

ví dụ:

Instead of:

<?php $exists = c2c_if_plugin_file_exists( $file ); ?>

Do:

<?php $exists = apply_filters( 'c2c_if_plugin_file_exists', $file ); ?>

c2c_if_theme_file_exists (filter)

The ‘c2c_if_theme_file_exists’ hook allows you to use 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.

Arguments:

  • same as for c2c_if_theme_file_exists()

ví dụ:

Instead of:

<?php $exists = c2c_if_theme_file_exists( $file ); ?>

Do:

<?php $exists = apply_filters( 'c2c_if_theme_file_exists', $file ); ?>

Cài đặt

  1. Install via the built-in WordPress plugin installer. Or download and unzip if-file-exists.zip 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)

Đánh giá

Đọc tất cả 1 đánh giá

Người đóng góp & Lập trình viên

“If File Exists” là mã nguồn mở. Những người sau đã đóng góp vào plugin này.

Những người đóng góp

“If File Exists” đã được dịch qua 1 ngôn ngữ. Cảm ơn những người tham gia dịch vì đóng góp của họ.

Dịch “If File Exists” sang ngôn ngữ của bạn.

Muốn tham gia phát triển?

Duyệt code, check out SVN repository, hoặc theo dõi nhật ký phát triển qua RSS.

Nhật ký thay đổi

2.2.9 (2020-09-02)

  • New: Add TODO.md for newly added potential TODO items
  • 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+

2.2.8 (2020-04-30)

  • Fix: Use full path to CHANGELOG.md in the Changelog section of readme.txt
  • Change: Use HTTPS for link to WP SVN repository in bin script for configuring unit tests
  • Change: Note compatibility through WP 5.4+
  • Change: Update links to coffee2code.com to be HTTPS
  • Change: Use backticks to denote code in readme.txt

2.2.7 (2019-11-22)

  • Change: Note compatibility through WP 5.3+
  • Change: Update copyright date (2020)

Full changelog is available in CHANGELOG.md.