Last active
September 11, 2019 17:17
-
-
Save ddemuth/065079766a8d117cbbeed3784d1c9f46 to your computer and use it in GitHub Desktop.
Advanced Custom Field (ACF) Read Only Field
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
/** | |
* Advanced Custom Field (ACF) Read Only Field | |
* | |
* Use prepare_field to display two fields but keep them read-only for non-admin users | |
* | |
* Check for Edit Screen: Non admin users can still add a value to inputs when adding post but | |
* cannot edit the input once created. | |
* | |
*/ | |
add_filter('acf/prepare_field/name=class_total', 'fka_disable_acf_prepare_field'); | |
add_filter('acf/prepare_field/name=total', 'fka_disable_acf_prepare_field'); | |
function fka_disable_acf_prepare_field( $field ) { | |
//Should only happen in WP Admin | |
if ( !is_admin() ) | |
return false; | |
$screen = get_current_screen(); | |
// When editing a post, the url search filter includes action=edit | |
if (isset($_GET['action'] ) ) { | |
$action = $_GET['action']; | |
} else { | |
$action = $screen->action; | |
} | |
// Check to see this applies to our post type in case this field gets added to another CPT. Be sure it's edit screen too. | |
if ($screen->post_type == 'fka_registrations' && $action == 'edit' ) { | |
// Only admins and fka admins can edit field | |
$user = wp_get_current_user(); | |
$allowed_roles = array('administrator', 'fka_administrator'); | |
if( array_intersect($allowed_roles, $user->roles ) ) { | |
//Do Nothing | |
} else { | |
$field['disabled'] = true; | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment