plugins - Make the checkbox meta field checked by default

I have created a page for plugin. For that page i have added a checkbox meta field. The field is working fine. By defau

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);
}

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);
}
Share Improve this question edited Jun 23, 2019 at 10:42 Camille V. 3391 gold badge2 silver badges12 bronze badges asked Jun 23, 2019 at 3:14 BinsaifullahBinsaifullah 111 bronze badge
Add a comment  | 

2 Answers 2

Reset to default 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 ;)

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);

发布者:admin,转转请注明出处:http://www.yc00.com/questions/1745376714a4625033.html

相关推荐

  • plugins - Make the checkbox meta field checked by default

    I have created a page for plugin. For that page i have added a checkbox meta field. The field is working fine. By defau

    3小时前
    20

发表回复

评论列表(0条)

  • 暂无评论

联系我们

400-800-8888

在线咨询: QQ交谈

邮件:admin@example.com

工作时间:周一至周五,9:30-18:30,节假日休息

关注微信