Ask your WordPress questions! Pay money and get answers fast! Comodo Trusted Site Seal
Official PayPal Seal

Checkbox, with Custom Meta WordPress

  • SOLVED

I would like a checkbox to be linked to a custom field;

The checkbox displays but i believe it does not save... i would like it to save.

Thanks code is below.

<?php
}

add_action('admin_menu', 'work_app_stores_cf');

function work_app_stores_cf() {
add_meta_box('app_store', 'Available on Which Stores?', 'work_app_stores_cff', 'work', 'normal', 'high');
}

//Adds the actual option box
function work_app_stores_cff() {
global $post;
?>

<style type="text/css">
#appstore div
{ display: block; margin: 0 0 0; padding: 15px 0; }

#appstore div label
{ font-size: 14px; margin: 0 0 4px; display: block; font-weight: bold; }

#appstore div small
{ display: block; margin: 0 0 10px; }

#appstore div select
{ width: 400px; height: 30px; }



</style>


<fieldset id="appstore">
<div>
<label for="app_apple" >
<input type="checkbox" id="app_apple" name="app_apple" value="checked" <?php get_post_meta($post->ID, 'app_apple', true) ?>/>Do you want to display this page on the home page?

</label>

</div>

</fieldset>
<?php
}


Save Code:


add_action('save_post', 'custom_add_save');

function custom_add_save($postID){
// called after a post or page is saved
if($parent_id = wp_is_post_revision($postID))
{
$postID = $parent_id;
}
if ($_POST['app_apple']) {
update_custom_meta($postID, $_POST['app_apple'], 'app_apple');
}
}

function update_custom_meta($postID, $newvalue, $field_name) {
// To create new meta
if(!get_post_meta($postID, $field_name)){
add_post_meta($postID, $field_name, $newvalue);
}else{
// or to update existing meta
update_post_meta($postID, $field_name, $newvalue);
}
}


Answers (1)

2011-10-20

Luis Cordova answers:

have you tested it? I think it should save

if not there should be an error right check the logs


xavier comments:

Yes you are right it is saving but not updating the HTML element to say it is checked, please advise.


Luis Cordova comments:

problem is here


<input ... id="app_apple" ... value="checked" <?php get_post_meta($post->ID, 'app_apple', true) ?>/>Do you want to display this page on the home page? ...


you are passing value="checked" and perhaps it gets confused with the other get_post_meta

maybe first output the get_post_meta alone to check that part is working and then make sure the input tag is rendered valid otherwise it can get confused with the value you are passing