0

i'm facing an issue with check if a checkbox is checked.

I have an array of custom post types. I want to list all of them with checkboxes. When the user check and save the page, the checkboxes should remain checked.

That's the code:

$options_pt = get_option( 'post_type_to_include' );

$post_types = get_post_types();

$remove_post_types = array('revision','nav_menu_item','custom_css','customize_changeset','acf-field-group','acf-field','wpcf7_contact_form','attachment','polylang_mo');

foreach ($remove_post_types as $remove_post_type) {

    if (($key = array_search($remove_post_type, $post_types)) !== false) {

        unset($post_types[$key]);

    }

}

?>

<?php if($options_pt){

    foreach ($post_types as $post_type) { ?>

        <input type='checkbox' name='post_type_to_include[<?php echo $post_type; ?>]' <?php checked( $options_pt[$post_type], 1 ); ?> value='1'><?php echo $post_type; ?></br> <?php

    }         

} else { 

foreach ($post_types as $post_type) { ?>

        <input type='checkbox' name='post_type_to_include[<?php echo $post_type; ?>]' value='1'><?php echo $post_type; ?></br> <?php

    } 

}

What's coming out of this code is:

-When I select NO post type, everything works fine.

-When I select a post type, the selected post type works fine the remainder say this:

Notice: Undefined index: page in /*****/option.php on line 71
value='1'>page

Line 71 is: the checkbox in the first foreach.

Anybody know where the issue could be?

Gianno
  • 148
  • 9
  • Also I dont actually see a reference to an array where you are using the ['page'] index in plain sight, So I assume it will be in a part of the code you are using something like `$array[$variable]` So it would be doubly good to identify the line where the error is occuring. – RiggsFolly May 12 '17 at 14:24
  • There are some very clever people that haunt this site, but non of them are _clarevoyant_ – RiggsFolly May 12 '17 at 14:25
  • @RiggsFolly maybe you can read better the next time.. Line 71 is: the checkbox in the first foreach. Page is generated by get_post_types()... If i tag a topic with wordpress I hope who answer know just a little bit of wordpress – Gianno May 12 '17 at 14:29
  • Ooopppssss silly me – RiggsFolly May 12 '17 at 14:31
  • Just a quick check. AS its a checkbox you do realise that checkboxes are only sent to the script if they are actually checked. – RiggsFolly May 12 '17 at 14:32
  • Yep! Finally the answer! I miss a isset control before the check of the checkbox! Thanks Riggs :) – Gianno May 12 '17 at 14:44
  • :) :) You are welcome. And me knowing nothing about WordPress as well :) ;) – RiggsFolly May 12 '17 at 14:46

0 Answers0