How to verify this checkbox is checked?

Actually I want to include a file if the checkbox is checked

<?php
$checked = get_option('automatic') ? "checked='checked'" : "";
echo "<input type='checkbox' name='automatic' $checked />";
?>

if(get_option('automatic') == 'checked')) ( require_once 'myfile.php'; )

This form is in a plugin option page

Related posts

Leave a Reply

4 comments

  1. <?php
    echo "<input type='checkbox' name='automatic' value='1' ".checked(1, get_option('automatic'))." />";
    
    if (get_option('automatic') === '1') { require_once 'myfile.php'; }
    ?>
    
  2. You may want to check out this tutorial.

    An example:

    $checked = get_option( 'automatic' ) ? 'checked="checked"' : '';
    echo '<input type="checkbox" name="automatic" value="automatic" ' . $checked . '/>';
    
    // verify form submission (use nonces: wp_verify_nonce)
    
    if ( isset( $_POST['automatic'] ) AND $_POST['automatic'] === 'automatic' )
    {
        require_once( 'myfile.php' );
    }
    
  3. I will not take more if your time. I just need to know why my function inside the IF statement is not working. If I place it somewher without the IF staememt it work wonderfully

    <?php
    
    echo "<input type='checkbox' name='automatic' value='1' ".checked(1, get_option('automatic'))." />";
    
    if (get_option('automatic') === '1') {
        function wp_automatically_shorcode($content) {
            ob_start();
            wp_automatically_functions();
            $output_string=ob_get_contents();
            ob_end_clean();
            return $output_string;
        }
        add_shortcode('automatic', 'wp_automatically_shorcode');
    
        function wp_automatically_functions() {
            echo 'ACTIVATED';
        }
    }
    ?>