1

I have created a page for plugin. For that page i have added a checkbox meta field. The field is working fine. By default, the checkbox field is unchecked. I mean at first to active the plugin, the checkbox field is unchecked. But I need to make it check by default. I have tried a condition but not working at all. My code is :

function ins_street_address_shortcode_callback(){
    $val = get_option('ins_street_address_shortcode');

    $checkval = "";

    if ($val == 'on'){
        $checkval = "checked";
    }

    printf("<input type='checkbox' name='ins_street_address_shortcode' %s />", $checkval);
}

2 Answers 2

2

Actually, your code is working, as long as $val == 'on'.

Did you try debugging by doing echo $val ?

If it's still not working, try hard refreshing (Ctrl + F5) your webpage, checkboxes tend to keep their state on normal refresh (F5).

Also, please put your code directly on this forum, so the code is always accessible for future viewers ;)

1

If you want a HTML checkbox to be checked by default just add "checked" attribute:

printf("<input type='checkbox' name='ins_street_address_shortcode' %s /> checked", $checkval);

1
  • I have also tried it. It works but it keeps checked always. When I unchecked it, it becomes checked automatically. How can I fix it? Commented Jun 27, 2019 at 2:25

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.