• Hi! I’m trying to add a checkbox as a field in a custom post type but I can’t get WordPress to save the value of checked or unchecked. I don’t have the foggiest idea of how to make it work and I was hoping one of you guys could point me in the right direction!

    Inside my function to create the checkbox I have:
    $checkbox = $custom["checkbox"][0];

    Which works fine. And for the input field I have:

    <?php if($checkbox == "true") $checkbox = 'checked="checked"';?>
    <input type="checkbox" id="checkbox" name="checkbox"   value="<?php echo (isset($checkbox) && $checkbox != ''  ? 'checked' : ''); ?>"  />

    The if/else stuff was something I was trying out from a different forum topic. I don’t know if it should be there or not, but it doesn’t work even without it. The checkbox shows up, just doesn’t save.

    Then for the update function I have:`
    update_post_meta($post->ID, “checkbox”, $_POST[“checkbox”]);`

    Any help would be greatly appreciated!! Thanks!

Viewing 2 replies - 1 through 2 (of 2 total)
  • Thread Starter Jess

    (@jessn)

    Well I figured it out. Kind of. Using radio buttons instead of checkboxes, which actually works better for my purposes. If it helps anyone here is what I did:

    <input type="radio" id="something" name="something"  value="1" <?php
    if ($something == 1) echo 'checked';?>/>
    <input type="radio" id="something" name="something"  value="2" <?php
    if ($something == 2) echo 'checked';?>/>
    <input type="radio" id="something" name="something"  value="3" <?php
    if ($something == 3) echo 'checked';?>/>

    Then just the basic update code:

    update_post_meta($post->ID, "something", $_POST["something"]);

    Austin Ginder

    (@austinginder)

    I had the same problem. I ended up getting it to work doing something messy like this:

    if($_POST["checkbox"] == "on") {
    	$checkbox = "on";
      } else {
    	$checkbox = "off";
      }
      update_post_meta($post->ID, "checkbox", $checkbox);
Viewing 2 replies - 1 through 2 (of 2 total)

The topic ‘WordPress custom post type and checkboxes’ is closed to new replies.