Skip to content

Instantly share code, notes, and snippets.

@arenagroove
Created April 17, 2025 03:04
Show Gist options
  • Select an option

  • Save arenagroove/b9df08c70311139da305d177c5988d1b to your computer and use it in GitHub Desktop.

Select an option

Save arenagroove/b9df08c70311139da305d177c5988d1b to your computer and use it in GitHub Desktop.
MU Plugin to add missing ACF/ACFE capabilities to administrators and enables global user_role logic in location rules.
<?php
/**
* Plugin Name: LR ACF & ACFE Administrator Permission Fixes
* Description: Adds missing ACF/ACFE capabilities to administrators and enables global user_role logic in location rules.
* Version: 1.0
* Author: Luis Martinez
* Author URI: https://www.lessrain.com
*/
if (!defined('ABSPATH')) {
exit; // Exit if accessed directly.
}
// Add missing ACF/ACFE capabilities to administrator
add_action('init', function () {
$role = get_role('administrator');
if ($role) {
$role->add_cap('acf_edit_fields');
$role->add_cap('acf_edit_field_groups');
$role->add_cap('acfe_admin_settings');
$role->add_cap('acfe_admin_tools');
}
});
// Extend ACF to make user_role location rules work everywhere
add_filter('acf/location/rule_match/user_role', function ($match, $rule, $screen) {
if (!is_user_logged_in()) {
return false;
}
$current_user = wp_get_current_user();
$roles = (array) $current_user->roles;
if ($rule['operator'] === '==') {
$match = in_array($rule['value'], $roles, true);
} elseif ($rule['operator'] === '!=') {
$match = !in_array($rule['value'], $roles, true);
}
return $match;
}, 10, 3);
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment