Using WordPress ‘deprecated_file_included’ PHP action

The deprecated_file_included WordPress PHP action fires when a deprecated file is called.

Usage

add_action('deprecated_file_included', 'your_custom_function', 10, 4);
function your_custom_function($file, $replacement, $version, $message) {
  // your custom code here
}

Parameters

  • $file (string) – The file that was called.
  • $replacement (string) – The file that should have been included based on ABSPATH.
  • $version (string) – The version of WordPress that deprecated the file.
  • $message (string) – A message regarding the change.

More information

See WordPress Developer Resources: deprecated_file_included

Examples

Log Deprecated File

Log information about the deprecated file to a custom log file.

add_action('deprecated_file_included', 'log_deprecated_file', 10, 4);
function log_deprecated_file($file, $replacement, $version, $message) {
  // Log deprecated file information to a custom log file
  error_log("Deprecated file: {$file} | Replacement: {$replacement} | Version: {$version} | Message: {$message}", 3, "/path/to/your/custom.log");
}

Send Email Alert

Send an email alert when a deprecated file is called.

add_action('deprecated_file_included', 'email_deprecated_file_alert', 10, 4);
function email_deprecated_file_alert($file, $replacement, $version, $message) {
  // Compose email message
  $email_subject = "Deprecated File Alert";
  $email_body = "Deprecated file: {$file}\nReplacement: {$replacement}\nVersion: {$version}\nMessage: {$message}";

  // Send email to the administrator
  wp_mail('[email protected]', $email_subject, $email_body);
}

Add Admin Notice

Display an admin notice when a deprecated file is called.

add_action('deprecated_file_included', 'admin_notice_deprecated_file', 10, 4);
function admin_notice_deprecated_file($file, $replacement, $version, $message) {
  // Store the notice message in a transient
  set_transient('deprecated_file_notice', "Deprecated file: {$file} | Replacement: {$replacement} | Version: {$version} | Message: {$message}", 60);

  // Display the notice on admin pages
  add_action('admin_notices', 'show_deprecated_file_notice');
}

function show_deprecated_file_notice() {
  // Get the notice message from the transient
  $notice = get_transient('deprecated_file_notice');

  // If the notice exists, display it and delete the transient
  if ($notice) {
    echo "<div class='notice notice-error'><p>{$notice}</p></div>";
    delete_transient('deprecated_file_notice');
  }
}

Disable Deprecated File Usage

Prevent the use of deprecated files in the website.

add_action('deprecated_file_included', 'disable_deprecated_file', 10, 4);
function disable_deprecated_file($file, $replacement, $version, $message) {
  // Exit the script execution with a message
  die("A deprecated file ({$file}) was called. Please update your code to use the replacement file ({$replacement}).");
}

Track Deprecated File Usage

Keep track of deprecated file usage by updating an option in the WordPress database.

add_action('deprecated_file_included', 'track_deprecated_file_usage', 10, 4); function track_deprecated_file_usage($file, $replacement, $version, $message) { // Get the current deprecated files usage from the database $deprecated_files_usage = get_option('deprecated_files_usage', array()); // Increment the usage count for the deprecated file if (isset($deprecated_files_usage[$file])) { $deprecated_files_usage[$file]++; } else { $deprecated_files_usage[$file] = 1; } // Update the deprecated files usage in the database update_option('deprecated_files_usage', $deprecated_files_usage); }

This example tracks the number of times each deprecated file is called by incrementing the count in the deprecated_files_usage option in the WordPress database.