Disable access to Advanced Custom fields admin menu

If you in a situation where you need to disable access to Advanced Custom fields WordPress admin GUI, you may need to do this to prevent your client from miss post fields by mistake.

Advanced Custom Fields in WordPress menu

You can use the following code

add_action( 'acf/init', 'text_domain_show_admin );
function text_domain_show_admin() {
	add_filter( 'acf/settings/show_admin', '__return_false' );
	if(isset($_GET['post_type']) && ($_GET['post_type'] == 'acf-field-group')){
		wp_redirect(admin_url());
	}
}

You may also need to disable it conditionally, only when the WP_ENVIRONMENT_TYPE constant is set to local or development.

add_action( 'acf/init', 'text_domain_show_admin );
function text_domain_show_admin() {
	if ( wp_get_environment_type() != 'local' && wp_get_environment_type() != 'development'){
		add_filter( 'acf/settings/show_admin', '__return_false' );
		if(isset($_GET['post_type']) && ($_GET['post_type'] == 'acf-field-group')){
			wp_redirect(admin_url());
		}
	}
}

References

https://www.advancedcustomfields.com/resources/how-to-hide-acf-menu-from-clients/
https://developer.wordpress.org/reference/functions/wp_get_environment_type/
https://localwp.com/help-docs/advanced/using-wp_get_environment_type-to-code-for-different-environments/

Leave a Comment

Your email address will not be published. Required fields are marked *

Scroll to Top