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?

Read more here: Error during check if a checkbox is checked in option page wordpress [duplicate]


Solution:

If you know the solution of this issue, please leave us a reply in Comment section, to update the question.


Wordpress related questions and answers: