Add a default value to Advanced Custom Fields hidden field

Advanced Custom Fields hidden field does not have a default value input.
Add one by implementing the code below. Personally I was using this to mark a registration form for a particular user type.

<?php
// add default value to hidden field
add_action('acf/render_field_settings/type=hidden', 'tb_hidden_default_render_field_settings');
add_filter('acf/prepare_field/type=hidden', 'tb_hidden_default_prepare_field');
      
function tb_hidden_default_render_field_settings($field){
acf_render_field_setting( $field, array(
    'label'			=> __('Default Value'),
    'instructions'	=> '',
    'name'			=> 'hidden_default',
    'type'			=> 'text',
    'ui'			=> 1,
  ), false);
}
function tb_hidden_default_prepare_field( $field ) {
  if( empty($field['hidden_default']) ) return $field;
  $field['value'] = $field["hidden_default"];
  $field['label'] = '';
  
  return $field;
}

Leave a Reply

Your email address will not be published.