Support » Plugins » Adding a checkbox to custom write panels

  • Resolved stevecoy

    (@stevecoy)


    Has anyone successfully added a custom write panel with a checkbox? I’ve been able to figure out how to get the checkbox to save the custom field value when I check it and save a post, but if I want to uncheck it, it doesn’t update. Here is an abridged version of the code:

    Here is the HTML for the form:

    <input type="checkbox" name="<?php echo $metabox["name"]?>" id="<?php echo $metabox["name"]?>" value="yes"';
    <?php if (get_post_meta($post->id, $metabox["name"], true) == "yes" ) { echo ' checked="checked"'; } ?> />

    And then the POST function:

    function metabox_insert($pID) {
    	global $metaboxes;
    	foreach ($metaboxes as $metabox) {
    		$var = $metabox["name"];
    		if (isset($_POST[$var])) {
    			if( get_post_meta( $pID, $metabox["name"] ) == "" )
    				add_post_meta($pID, $metabox["name"], $_POST[$var], true );
    			elseif($_POST[$var] != get_post_meta($pID, $metabox["name"], true))
    				update_post_meta($pID, $metabox["name"], $_POST[$var]);
    			elseif($_POST[$var] == "")
    				delete_post_meta($pID, $metabox["name"], get_post_meta($pID, $metabox["name"], true));
    		}
    	}
    }
Viewing 7 replies - 1 through 7 (of 7 total)
  • I have.

    I used Liam McKay’s tutorial for them. (as a note, he has updated this year-old tutorial to streamline it, and it’s terrific. I have it running on WP 3.0 beta – but I haven’t tried to add checkboxes or anything to it yet – so what I’m about to share is for the older stuff)

    Anyway, I had a site where I needed checkboxes, textareas, dropdowns, radio buttons and I got it to work. Now I have to dig for it…. ah – here it is.

    If you’re following Liam’s tutorial, then in the “new_meta_boxes()” function (be aware that I changed some of his names because I wanted to use a different set of field for pages over posts, and they couldn’t have the same names) – put the following:

    function new_meta_boxes() {
      global $post, $new_meta_boxes;  
    
      foreach($new_meta_boxes as $meta_box) {
    	$meta_box_value = get_post_meta($post->ID, $meta_box['name'] . '_value', true);  
    
    	if($meta_box_value == "")
    	$meta_box_value = $meta_box['std'];  
    
    	// we want different types of fields - not just text input.  Let's create the variety of types.
    	$hidden = '<input type="hidden" name="' . $meta_box['name'] . '_noncename" id="' . $meta_box['name'] .'_noncename" value="' . wp_create_nonce( plugin_basename(__FILE__) ) . '" />';
    	$label = '<span style="font-size:1.1em; color:#999; margin-bottom:0; padding-bottom:0;">' . $meta_box['title'] . '</span>';
    
    	if($meta_box['type'] == 'text') {
      	  $input_type = '<input type="text" name="' . $meta_box['name'] . '_value" value="' . $meta_box_value . '" style="width:' . $meta_box['width'] . ';" />';
    	} else if($meta_box['type'] == 'checkbox') {
    	  if($meta_box_value == "yes") $checked = 'checked="checked"';
    		$input_type = '<input type="checkbox" name="' . $meta_box['name'] . '_value" value="yes"' . $checked . ' />';
    	}
    
    	$description = '<span style="font-style:italic; color:#999; margin-top:0; padding-top:0;"><label for="' . $meta_box['name'] . '_value">' . $meta_box['description'] . '</label></span>';
    
        echo '<div style="font-size:1em; width:48%; text-align:right; padding:3px 5px 0 0; vertical-align:top; float:left;">' . "\n";
    	echo $hidden . $label . '<br />' . $description . "\n";
    	echo '</div><div style="width:50%; float:left;">' . "\n";
    	echo $input_type . "\n";
    	echo '</div>' . "\n" . '<br style="clear:both;" />' . "\n";
      }
    }

    Hope that helps!

    Thread Starter stevecoy

    (@stevecoy)

    Aw man, Liam’s new tutorial is soooooooo much better than the way I was doing it! Took me 5 minutes to whip up some checkboxes, textareas, and selects. Thanks so much, you’re the best. Well, I guess Liam is the best, too. Here’s my full code (like you, I have different fields for posts and pages):

    [Code moderated as per the Forum Rules. Please use the pastebin]

    Glad it worked for you! 🙂

    Just want to know when and where the data from the textbox and checkbox elements is stored?

    I have tried this code but it doesnot work for me… im stuck please help.

    Thread Starter stevecoy

    (@stevecoy)

    rmughal, go to http://postmodernsublime.com/contact/ and send me a message. I can then zip up my code and send you the files, and tell you where to put the code.

    Thank you so much for your help regarding the various input fields. I have been able to create one checkbox and it is saving the value but I need multiple checkboxes. Do I need a foreach loop around this?

    if($meta_box[‘type’] == ‘checkbox’) {
    if($meta_box_value == “yes”) $checked = ‘checked=”checked”‘;
    $input_type = ‘<input type=”checkbox” name=”‘ . $meta_box[‘name’] . ‘_value” value=”yes”‘ . $checked . ‘ />’;
    }

    Thread Starter stevecoy

    (@stevecoy)

    Multiple checkboxes are tricky. I remember getting it to work once doing something like this:

    if ($meta_box[‘type’] == “multicheck”){
    $boxes = array(“value1”, “value2”, “value3”);
    //you could also store the options as an array in when you are setting up each custom field and then pull them in here

    foreach ($boxes as $box) {

    echo ‘<label>’;
    echo ‘<input type=”checkbox” name=”‘. $meta_box[“name”].'[]” id=”‘.$meta_box[“name”].$box.'” value=”‘.$box.'”‘;
    if ($meta_box_value <> “” ) {if ( in_array($box, $meta_box_value)) { echo ‘ checked=”checked”‘; }} echo ‘/>’;
    echo $day;
    echo ‘</label>’;
    }
    //endforeach
    }

Viewing 7 replies - 1 through 7 (of 7 total)
  • The topic ‘Adding a checkbox to custom write panels’ is closed to new replies.