Take the 2-minute tour ×
WordPress Development Stack Exchange is a question and answer site for WordPress developers and administrators. It's 100% free, no registration required.

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

share|improve this question
add comment

4 Answers

<?php
echo "<input type='checkbox' name='automatic' value='1' ".checked(1, get_option('automatic'))." />";

if (get_option('automatic') === '1') { require_once 'myfile.php'; }
?>
share|improve this answer
    
checked='checked' appear on the form and requre_once is not executed –  puanthanh Mar 9 '11 at 22:02
    
Is the if statement getting cached? Replace require_once with echo 'It works.'; to see. –  imHavoc Mar 9 '11 at 22:36
    
bah it show It works –  puanthanh Mar 9 '11 at 22:57
    
Thank. checked='checked' appear in the left side of the box after checked –  puanthanh Mar 9 '11 at 22:59
    
@uanthanh - how about passing a third parameter false Ex. - checked(1, get_option('automatic'), false ) –  amit Aug 17 '12 at 16:04
add comment

Is this what you mean?

if( get_option('automatic') ) {

    // do something here

}

Surely not?

share|improve this answer
    
<?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'; } ?> –  puanthanh Mar 9 '11 at 23:20
    
I want to write another function in my plugin. This is to be use only if the checkbox is checked. –  puanthanh Mar 9 '11 at 23:23
    
Something like this if( get_option('automatic') //checked box is checked // write new function } –  puanthanh Mar 9 '11 at 23:29
add comment

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' );
}
share|improve this answer
    
checkbox is fine but this is not executed require_once( 'myfile.php' ); } –  puanthanh Mar 9 '11 at 22:23
    
My function are not executed if in is inside the conditon if ( isset( $_POST['automatic'] ) AND $_POST['automatic'] === 'automatic' ) { require_once( 'myfile.php' ); } –  puanthanh Mar 9 '11 at 23:11
    
This is inside myfile.php function wp_automaticlly_shorcode($content) { ob_start(); wp_automaticlly_functions(); $output_string=ob_get_contents(); ob_end_clean(); return $output_string; } add_shortcode('automatic', 'wp_automaticlly_shorcode'); function wp_automaticlly_functions() { echo 'ACTIVATED'; } –  puanthanh Mar 9 '11 at 23:13
    
Thanks all that is a different probs now –  puanthanh Mar 9 '11 at 23:17
add comment

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';
    }
}
?>
share|improve this answer
    
Its's funny. It works now. I had to call in with another add action function –  puanthanh Mar 10 '11 at 0:08
add comment

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

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