2

I have a form on the frontend of Wordpress which is saving all fields except a checkbox array. It displays and saves the correct values if I tick the boxes in the backend post editor, and the ticks show correctly on the front. However ticking a box on the frontend doesnt save! All other fields save correctly from font and back.I have spent a long time trying to figure it out however it doesn't seem to work, any help would be greatly appreciated. Here's the code:

$query = new WP_Query( array( 'post_type' => 'property', 'posts_per_page' => '-1' ) ); 

if ( $query->have_posts() ) : while ( $query->have_posts() ) : $query->the_post(); 

if(isset( $_GET['post'] ) ) {
    if ( $_GET['post'] == $post->ID ) {
        $current_post = $post->ID;
        $options = $custom_meta_fields[7]['options'];
        $checkboxes = get_post_meta($current_post, 'custom_cgtest', true);
}
}
endwhile; endif; 
wp_reset_query();
global $current_post;

if(isset($_POST['submitted']) && isset($_POST['post_nonce_field']) && wp_verify_nonce($_POST['post_nonce_field'], 'post_nonce')) {
$post_information = array(
'ID' => $current_post,
'post_title' => ($_POST['post_title']),
'post_content' => esc_attr(strip_tags($_POST['postContent'])),
'post-type' => 'property',
'post_status' => 'publish',
);    

$post_id = wp_update_post($post_information);
if($post_id) { 
update_post_meta($post_id, 'custom_cgtest', ($_POST['cbn']));
}
}

In the page:

foreach ($options as $option) {  
    echo '<input type="checkbox" name="cbn[]" id="'.$option['value'].'"',$checkboxes && in_array($option['value'], $checkboxes) ? ' checked="checked"' : '',' /> ';
    echo $option['value'];
    }

In functions.php:

array (  
'label' => 'Checkbox Group',  
'desc'  => 'A description for the field.',  
'id'    => $prefix.'cgtest',  
'type'  => 'cgtest',  
'options' => array (  
    'one' => array (  
        'label' => 'Option One',  
        'value' => 'one'  
    ),  
    'two' => array (  
        'label' => 'Option Two',  
        'value' => 'two'  
    ),  
    'three' => array (  
        'label' => 'Option Three',  
        'value' => 'three'  
    )  
)  
)

If you require any more code please let me know.

4
  • Posting disjointed code like this makes it hard to test. You would have better luck posting complete, working (as much as you have) code blocks.
    – s_ha_dum
    May 5 '13 at 15:46
  • I can try and make it a bit more coherent, there's tons of code and I just wanted to post the relevant bits but I will update...
    – user32437
    May 5 '13 at 15:48
  • Yes, you only need post the relevant bits but as it is, to test this I'd have to make up a bunch of code. That does not encourage assistance, and it means that solutions are based on assumptions about your code.
    – s_ha_dum
    May 5 '13 at 15:51
  • Did you set $post_id to anything. You do not show code for it. May 5 '13 at 15:52
1

Did you check the database for the data?

My guess is that the problem is here:

echo '<input type="checkbox" name="cbn[]" id="'.$option['value'].'"',$checkboxes && in_array($option['value'], $checkboxes) ? ' checked="checked"' : '',' /> ';

should be:

echo '<input type="checkbox" name="cbn[]" id="'.$option['value'].'"'.$checkboxes && in_array($option['value'], $checkboxes) ? ' checked="checked"' : ''.' /> ';

Edit: You're confusing poor PHP. Try enclosing the tertiary function in parenthesis.

echo '<input type="checkbox" name="cbn[]" id="' . $option['value'] .    '"' . ( $checkboxes && in_array( $option['value'], (array) $checkboxes ) ? ' checked="checked"' : '' ) . ' /> ';
8
  • Thanks for your help, it seems that the first comma is needed, however the very last one seems to function the same with a stop rather than a comma... so the problem remains! I will check the database though.
    – user32437
    May 5 '13 at 15:51
  • What do you mean first comma is needed? For joining strings in PHP you use ".", comma is used in listing function arguments. May 5 '13 at 15:54
  • Sorry, its not needed as such, it just breaks when I change them to stops, rendering as: /> rather than showing the checkboxes..
    – user32437
    May 5 '13 at 16:07
  • Check the wp_postmeta table, go to the end and look for your post ID. May 5 '13 at 16:24
  • It doesn't show anything, but when I tick the box from the back-end editor it shows up in the db and on the front-end! so it shows the value correctly, it just doesn't post it..
    – user32437
    May 5 '13 at 16:30

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.