Skip to content

Instantly share code, notes, and snippets.

@benjaminpick
Last active November 5, 2015 11:29
Show Gist options
  • Select an option

  • Save benjaminpick/fc8a86fb07e1d58801e4 to your computer and use it in GitHub Desktop.

Select an option

Save benjaminpick/fc8a86fb07e1d58801e4 to your computer and use it in GitHub Desktop.
ACF 5: Show a field group only if the user that is logged in can do a certain wordpress cap.
<?php
/*
Plugin Name: YT ACF Field Group Location Has Capability
Description: Show a field group only if the user that is logged in can do a certain wordpress cap.
Version: 0.1
Author: YellowTree (Benjamin Pick)
Author URI: http://yellowtree.de
License: GPLv2 or later
License URI: http://www.gnu.org/licenses/gpl-2.0.html
*/
/* -------------- ACF "Login User Role" Location : User has capability? ------ */
function yt_acf_location_capabilities_rule_types($choices) {
$choices[__("User",'acf')]['yt_user_has_capability'] = __('User Capability', 'yt');
return $choices;
}
add_filter('acf/location/rule_types', 'yt_acf_location_capabilities_rule_types');
function yt_acf_location_capabilities($choices) {
global $wp_roles;
$caps = array_keys($wp_roles->roles['administrator']['capabilities']);
sort($caps);
$choices = array_combine($caps, $caps);
return $choices;
}
add_filter( 'acf/location/rule_values/yt_user_has_capability', 'yt_acf_location_capabilities' );
function yt_acf_location_has_capability($allowed, $rule, $args) {
return current_user_can($rule['value']);
}
add_filter('acf/location/rule_match/yt_user_has_capability', 'yt_acf_location_has_capability', 10, 3);
@jarvo1980
Copy link

Hi Benjamin
This is great, but I have a question. If I create a capability and set a custom field to only appear if they have that capability it works great. If the capability is removed from that user, the custom field still seems to show.
Do I need to alter anything above?
Many thanks

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment