Skip to content

Instantly share code, notes, and snippets.

@allyssonharry
Last active November 24, 2022 05:17
Show Gist options
  • Save allyssonharry/6b46281c4ee2a1a16d1ecfe10c7eac13 to your computer and use it in GitHub Desktop.
Save allyssonharry/6b46281c4ee2a1a16d1ecfe10c7eac13 to your computer and use it in GitHub Desktop.
Adds hidden field type to Advanced Custom Fields
1 - Create folders:
~$ mkdir -p acf-field-hidden/includes
2 - Copy "acf-field-hidden.php", into "acf-field-hidden" folder
3 - Copy "class-acf-field-hidden.php", into "includes" folder
4 - Active the plugin
<?php
/**
* Plugin Name: Hidden Field for ACF
* Plugin URI: https://gist.github.com/allyssonharry/6b46281c4ee2a1a16d1ecfe10c7eac13
* Description: Adds hidden type field to Advanced Custom Fields.
* Author: Allysson M
* Text Domain: acf-field-hidden
* Domain Path: /languages
* Version: 0.1.0
* Requires PHP: 7.3
*/
defined('ABSPATH') || exit;
/**
* Define constants
*/
const ACF_MINIMUM_VERSION = '5.0.0';
/**
* Load text domain
*/
// load_plugin_textdomain( 'acf-field-hidden', false, dirname( plugin_basename(__FILE__) ) . '/languages/' );
/**
* Initialize the plugin
*
* @return ACF|void
*/
function register_acf_field_hidden()
{
if ( !can_load_plugin() ) {
// Show the admin notice
add_action( 'admin_init', __NAMESPACE__ . 'show_admin_notice' );
deactivate_plugins( plugin_basename( __FILE__ ) );
return;
}
include_once dirname( __FILE__ ) . '/includes/class-acf-field-hidden.php';
}
add_action( 'acf/include_field_types', 'register_acf_field_hidden' );
/**
* Show admin notice to admins if this plugin is active but either ACF are not active
*
* @return bool
*/
function show_admin_notice()
{
/**
* For users with lower capabilities, don't show the notice
*/
if ( !current_user_can('manage_options') ) {
return false;
}
add_action('admin_notices', function () {
?>
<div class="error notice">
<p><?php esc_html_e(sprintf('Advanced Custom Fields v5.0+ must be active for "acf-field-hidden" to work.', ACF_MINIMUM_VERSION), 'acf-field-hidden');?></p>
</div>
<?php
});
}
/**
* Check whether ACF are active, and whether the minimum version requirement has been met
*
* @return bool
*/
function can_load_plugin()
{
if ( !class_exists('ACF') ) {
return false;
}
if ( version_compare(get_option('acf_version'), ACF_MINIMUM_VERSION, '<=') ) {
return false;
}
return true;
}
<?php
/**
* Register field hidden type
*/
class acf_field_hidden_register extends acf_field
{
public function __construct()
{
$this->name = 'acf_hidden';
$this->label = __('Hidden', 'acf');
$this->category = 'basic';
$this->defaults = [
'default_value' => '',
];
parent::__construct();
}
public function prepare_field($field)
{
$field['wrapper']['class'] = 'acf-hidden';
return $field;
}
public function render_field($field)
{
?>
<input type="hidden" name="<?php echo esc_attr($field['name']) ?>" value="<?php echo esc_attr($field['value']) ?>" />
<?php
}
public function render_field_settings($field)
{
acf_render_field_setting($field, [
'label' => __('Value', 'acf'),
'instructions' => __('Default value in the hidden input', 'acf'),
'type' => 'text',
'name' => 'default_value',
]);
}
}
acf_register_field_type('acf_field_hidden_register');
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment