0

I am creating one custom setting panel, my codes are

function custom_text_field_html(){
 
    $text = get_option( 'homepage_text' );
 
    printf(
        '<input type="text" id="homepage_text" name="homepage_text" value="%s" />',
        esc_attr( $text )
    );
 
}

function custom_checkbox_field_html(){
 
    $checkbox = get_option( 'disabletitle_text' );
 
    printf(
        '<input type="checkbox" id="disabletitle_text" name="disabletitle_text" value="1" />',
        esc_attr( $checkbox )
    );
 
}

my checkbox data is not saving, how to save checkbox data like 'text field', so that I can call them in functions, if someone checked the field 'enable that function', if someone 'not checked the field', disable the function.

I referred this http://qnimate.com/add-checkbox-using-wordpress-settings-api/, but it's showing error in my case

1 Answer 1

1

Your checkbox data is saved as 1 or '' if someone checked or unchecked it.

you can also verify this using var_dump($checkbox) inside custom_checkbox_field_html function

This should work.

function custom_checkbox_field_html(){
 
    $checkbox = get_option( 'disabletitle_text' );
    $is_checked = (  $checkbox != '' && $checkbox == 1 ) ? 'checked': '';

    printf(
        '<input type="checkbox" id="disabletitle_text" name="disabletitle_text" value="1" %s/>',
        esc_attr( $is_checked )
    );
 
}
2
  • thanks, it worked. Can you answer the second question- How can I call them in functions, if someone checked the field 'enable that function', if someone 'not checked the field', disable the function. for example after check, disable updates. Thanks :)
    – Ricky
    Commented May 31, 2021 at 10:39
  • 1
    just use this get_option( 'disabletitle_text' ) value to determine you needs. if it return 1 then you can enable you function else you can disable your function. Commented May 31, 2021 at 10:43

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

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