Support

Account

Home Forums General Issues Custom checkbox won't save to DB

Solved

Custom checkbox won't save to DB

  • I have a custom checkbox that I cannot get the info to save to the database. The information about the checkbox will display fine.

    Code:

    $this->grid_divider_options = apply_filters('acf_section_styles_grid_divider_options', array(
    'yes'=> __( 'yes', 'acf-section_styles' ),
    ) );
    
    function render_field_settings( $field ) {
    //Grid Divider
    acf_render_field_wrap(array(
    	'label'			=> __('Grid divider','acf-section_styles'),
    	'type'			=> 'checkbox',
    	'name'			=> 'grid_divider',
    	'prefix'		=> $field['prefix'],
    	'value'			=> $field['grid_divider'],
    	'toggle'		=> true,
    	'choices'		=> $this->grid_divider_options,
    ), 'tr');
    }
    <?php foreach ( $this->grid_divider_options as $v => $label ): ?>
    <input type="checkbox" id="<?php echo $field['id'].'_grid_divider';?>" value="<?php echo $v; ?>"<?php if ( !empty( $field['value']['grid_divider'] ) && $field['value']['grid_divider'] == $v ) echo ' checked="checked"'; ?>><?php echo $label; endforeach; ?>
  • Are you trying to create a custom setting on an existing field or are you created a custom field type?

  • Custom setting on a field.

  • The reason why I asked and what confused me is that you calling acf_render_field_wrap() and it should be acf_render_field_setting() according to the documentation. https://www.advancedcustomfields.com/resources/adding-custom-settings-fields/

    Is there some reason that you’re using acf_render_field_wrap()?

  • Other than that, the question is why isn’t is saving. It may be because you’re using a checkbox field, I’m not even sure what some of your settings are. For example 'toggle', seems to be a new setting on the checkbox field that I wasn’t aware of that lets you toggle all the choices on and off, but you only have one choice of “Yes”. It looks like you’re creating a “true/false” type of field so I’m a little lost tying to understand exactly what you’re doing.

    If I was going to create a true/false field as a setting in another field then I would use a true/false type of field. I’ve just exported a true/false field from ACF to use as an example here.

    
    acf_render_field_setting(array(
      'default_value' => 0,
      'message' => '',
      'ui' => 1,
      'ui_on_text' => 'On',
      'ui_off_text' => 'Off',
      'label' => 'test field',
      'name' => 'test_field',
      'type' => 'true_false',
    ));
    

    If I’ve completely missed the mark with all of this let me know where I’m misunderstanding. To this point I have not had any problems creating custom settings for fields and having the values save when creating a new field so I’m really just guessing at why you’re seeing this given the information you’ve provided.

  • I am using function render_field_settings but because I have multiple fields (I only posted for ease of reading) I am wrapping each field in acf_render_field_wrap. I saw this done a few time in other people projects and figured it was the right way :D.

    You are right in the fact that I probably want a true false as I do only have a Yes option. How would I go about adding the true false to my template?

  • The one great thing I’ve found when creating new field types, or adding settings to existing field types is that any field type already defined by ACF can be used as a setting field for another field type, or even itself for that matter. For example, the radio field in ACF uses ACF radio fields for settings and the ACF true/false field uses true/false fields for settings.

    What I generally to is create a field in ACF of the type I want to use and then get the export code and look at that and then just use the needed settings when I call render_field_settings().

    I have not actually used a true/false field for a setting. This field type uses a checkbox and even a repeater https://github.com/Hube2/acf-medium-editor.

    This example that I posted on github uses the ACF number field and it is also an example of having multiple fields in one wrapper but uses acf_render_field_setting(). I just copied how E created the fields for image size settings.

    I basically just dug through ACF to see how E does it. Take a look at any render_field_settings() methods in any of the existing ACF field types and you’ll find examples of how to add a true/false field as a field setting.

  • I appreciate the help. I’ll keep digging.

  • There could be a conflict between the code in the tutorial and the plugin.

    If you post the code that you’ve created maybe I can see why it’s not working.

  • I apologize if the code is terrible. First time really going at something like this 🙂

    https://gist.github.com/rameden/ea3d6d7bea4443713f6c8d1062644366

  • Shoot side note: I don’t have the Checkbox in there right now. I moved to a select as I had that working.

Viewing 11 posts - 1 through 11 (of 11 total)

The topic ‘Custom checkbox won't save to DB’ is closed to new replies.