Using WordPress ‘file_mod_allowed’ PHP filter

The file_mod_allowed WordPress PHP filter allows you to control whether file modifications are permitted or not in a specific context.

Usage

apply_filters('file_mod_allowed', $file_mod_allowed, $context);

Parameters

  • $file_mod_allowed (bool) – Indicates if file modifications are allowed.
  • $context (string) – Describes the usage context.

More information

See WordPress Developer Resources: file_mod_allowed

Examples

Disable file modifications for all contexts

Prevent any file modifications regardless of the context.

add_filter('file_mod_allowed', '__return_false');

Enable file modifications only for plugins

Allow file modifications only for plugin-related actions.

function allow_plugin_modifications($file_mod_allowed, $context) {
  if ($context == 'plugin') {
    return true;
  }
  return $file_mod_allowed;
}
add_filter('file_mod_allowed', 'allow_plugin_modifications', 10, 2);

Disable file modifications for themes

Prevent file modifications for theme-related actions.

function disable_theme_modifications($file_mod_allowed, $context) {
  if ($context == 'theme') {
    return false;
  }
  return $file_mod_allowed;
}
add_filter('file_mod_allowed', 'disable_theme_modifications', 10, 2);

Enable file modifications based on user role

Allow file modifications only for administrators.

function allow_modifications_for_admins($file_mod_allowed, $context) {
  if (current_user_can('manage_options')) {
    return true;
  }
  return $file_mod_allowed;
}
add_filter('file_mod_allowed', 'allow_modifications_for_admins', 10, 2);

Disable file modifications based on custom condition

Prevent file modifications if a specific condition is met.

function disable_modifications_on_condition($file_mod_allowed, $context) {
  if (some_custom_condition()) {
    return false;
  }
  return $file_mod_allowed;
}
add_filter('file_mod_allowed', 'disable_modifications_on_condition', 10, 2);