[resolved] How to add (and save) checkbox to custom meta box (2 posts)

  1. cabrailsford
    Posted 2 years ago #

    I have input the following code to create a custom meta box for a site I'm building. However, I can't seem to get the checkbox to save after clicking "Publish" or "Update". I've tried several different variations of code from different sources on how to tackle it, but they seem to not match up with what I'm doing, and therefore have no effect. If anyone can assist in telling me what to add/change to get it to work, I'd greatly appreciate it!

    // The Contact Metabox
    	function cg_contact() {
        	global $post;
        // Noncename needed to verify where the data originated
        echo '<input type="hidden" name="contactmeta_noncename" id="contactmeta_noncename" value="' .
        	wp_create_nonce( plugin_basename(__FILE__) ) . '" />';
    	// Get the contact data if its already been entered
        $phone1 = get_post_meta($post->ID, '_phone1', true);
    	$phone2 = get_post_meta($post->ID, '_phone2', true);
    	$fax = get_post_meta($post->ID, '_fax', true);
    	$emailaddr = get_post_meta($post->ID, '_emailaddr', true);
    	$websiteaddr = get_post_meta($post->ID, '_websiteaddr', true);
    	$directlink = get_post_meta($post->ID, '_directlink', true);
        // Echo out the field ?>
        <table width="100%">
            	<td style="padding-right:10px;">
    			<?php echo '<p>Phone 1:</p>';
    			echo '<input type="text" name="_phone1" value="' . $phone1  . '" class="widefat" />';?>
                <td style="padding-right:10px;">
    			<?php echo '<p>Phone 2:</p>';
    			echo '<input type="text" name="_phone2" value="' . $phone2  . '" class="widefat" />';?>
    			<?php echo '<p>Fax:</p>';
    			echo '<input type="text" name="_fax" value="' . $fax  . '" class="widefat" />';?>
            	<td style="padding-right:10px;">
                <?php echo '<p>Email Address:</p>';
    			echo '<input type="text" name="_emailaddr" value="' . $emailaddr  . '" class="widefat" />';?>
                <td style="padding-right:10px;">
                <?php echo '<p>Website:</p>';
    			echo '<input type="text" name="_websiteaddr" value="' . $websiteaddr  . '" class="widefat" />';?>
                <?php echo '<p>Direct Link?</p>';
    			echo '<input type="checkbox" name="_directlink" class="widefat"'. checked( $check, 'on' ). '/>';?>
    	// Save the Contact Metabox Data
    	function cg_save_contact_meta($post_id, $post) {
        // verify this came from the our screen and with proper authorization,
        // because save_post can be triggered at other times
        if ( !wp_verify_nonce( $_POST['contactmeta_noncename'], plugin_basename(__FILE__) )) {
        return $post->ID;
        // Is the user allowed to edit the post or page?
        if ( !current_user_can( 'edit_post', $post->ID ))
            return $post->ID;
        // OK, we're authenticated: we need to find and save the data
        // We'll put it into an array to make it easier to loop though.
        $contact_meta['_phone1'] = $_POST['_phone1'];
    	$contact_meta['_phone2'] = $_POST['_phone2'];
    	$contact_meta['_fax'] = $_POST['_fax'];
    	$contact_meta['_emailaddr'] = $_POST['_emailaddr'];
    	$contact_meta['_websiteaddr'] = $_POST['_websiteaddr'];
    	$contact_meta['_directlink'] = $_POST['_directlink'];
        // Add values of $contact_meta as custom fields
        foreach ($contact_meta as $key => $value) { // Cycle through the $events_meta array!
            if( $post->post_type == 'revision' ) return; // Don't store custom data twice
            $value = implode(',', (array)$value); // If $value is an array, make it a CSV (unlikely)
            if(get_post_meta($post->ID, $key, FALSE)) { // If the custom field already has a value
                update_post_meta($post->ID, $key, $value);
            } else { // If the custom field doesn't have a value
                add_post_meta($post->ID, $key, $value);
            if(!$value) delete_post_meta($post->ID, $key); // Delete if blank
    add_action('save_post', 'cg_save_contact_meta', 1, 2); // save the custom fields
  2. cabrailsford
    Posted 2 years ago #

    Nevermind guys, I got it resolved. Had to add in an if statement regarding the checkbox being checked, as seen below.

    <?php if( $_directlink == true ) { ?>checked="checked"<?php } ?>

Topic Closed

This topic has been closed to new replies.

About this Topic