Support » Themes and Templates » Saving checkbox values

  • Hi,

    I have created a checkbox for pages and my custom post types called project.

    I want the default to be checked and then the client needs to untick the box and click update for the state to change.

    Here is how my meta box is created:-

    function add_feature_meta($post, $metabox)
    	global $post;
    	$show		= 'on';
      	$custom = get_post_custom($post->ID);
    	if (array_key_exists('show', $custom))
    		if ($custom["show"][0] == 'on')
    			$show	= ' checked';
    	include(MY_THEME_FOLDER . '/custom/featuremeta.php');

    The featuremeta.php file contains this code:-

    <input type="checkbox" name="show" <?php if( $show == true ) { ?>checked<?php } ?> >

    To save the value I have tried this:-

    add_action('save_post', 'save_details');
    function save_details($post_ID = 0) {
        $post_ID = (int) $post_ID;
        $post_type = get_post_type( $post_ID );
        $post_status = get_post_status( $post_ID );
        if ($post_type) {
        	update_post_meta($post_ID, "show", $_POST["show"]);
       return $post_ID;

    However until I go through all the pages and projects and click update the default ticked option is not active. Any ideas if this is the correct way to save the checkbox data?

    Thank you,

  • The topic ‘Saving checkbox values’ is closed to new replies.