Support

Account

Home Forums Backend Issues (wp-admin) Checkboxes with an 'Other' text field

Solving

Checkboxes with an 'Other' text field

  • Hi,

    I’d like to create a field that is a series of checkboxes. One of those checkboxes will be labeled “other”. When someone selects ‘other’ I’d like a plain text input to show up so that they can describe what they mean by ‘other’.

    Is this possible?

    If not, I was planning to hack it together via JS and CSS hackery. Have an text-field input for other that is hidden on page load. Listen for ‘other’ being checked and then show it. On the front end, I’ll need to loop through the ‘real’ checkboxes and also check to see if the text field is !empty. Not ideal! Particularly because my content entry form will have 3 or 4 of these +other sets.

    I reeeeaallly want one of you kind folks to tell me I can do this already! If not, is there a better way to go about it than my JS/CSS fudge-it plan? If I have to make it, might as well make it ‘right’ and share it back out via github. I’m new to ACF and have been out of the WP for about a year so I’m not sure the ‘right’ way. 🙂

    Cheers! And thanks for ACF!

  • Hi @willthemoor

    I do not think it is possible with checkboxes by default, but you can do that with radio boxes.

    I hope that helps,

  • Thanks very much for your reply @reardestani. Unfortunately, these fields require multiple selection as an option. I will check out the radio button code though and see if that offers me any clues.

  • Thanks again for that clue @reardestani. I’ve spent some time today trying to copy from/reconcile code from radio.php into checkbox.php. It’s almost working but not quite. I think I have the code in the create_field() method working ok but it all seems to fall apart within the update_value() method.

    function update_value( $value, $post_id, $field )
    {
    	var_dump($value);
    	... <snip> ...
    }
    

    Produces (with the ‘other’ and another box checked)

    array(2) {
      [0]=>
      string(8) "firstVAL"
      [1]=>
      string(5) "other"
    }
    

    Then, I loop through it

    foreach($value as $key => $v)
    {
    	if ( !in_array($v, $field['choices']) )
    	{
    		echo "key: " . $key . "      /     v: " . $v . "\n\n";
    		array_push($field['choices'], $v);
    	}
    }
    

    and that of course produces:

    key: 0      /     v: firstVAL
    key: 1      /     v: other
    

    Not the value of the textinput but the value of the checkbox.

    Can someone set me straight here? How can I find the value of that text input I’ve created within the create_field() function?

    This is with the Save ‘other’ values to the field’s choices set to on FWIW. In the end, I won’t actually want that (awesome) functionality – there will be many different editors of this ‘form’ and they don’t all need to see the values saved by others. I tried turning it off but when I reloaded my my edit (custom) post, the value wasn’t saved. For now anyway, I’m going down this path.

    Once I sort it out, I’ll clean up my mayhem and submit it as a pull request in case it’s useful to anyone else.

    Cheers,
    Will

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

The topic ‘Checkboxes with an 'Other' text field’ is closed to new replies.