settings api - Assign value to custom checkbox using get_option()

I have a bootstrap 4 switch that is a custom checkbox input type introduced from the 4.3 version. I want to integrate it

I have a bootstrap 4 switch that is a custom checkbox input type introduced from the 4.3 version. I want to integrate it in a custom option page and assign to it a default value when my option page is rendered. How I can do this, is possible?

here is my actual code, I've omitted the add_action functions but there are present on the plugin code.

function registerSettings()
  {
    $menu_slug = 'my-plugin-options';
    register_setting( $menu_slug, 'my-option' );
    // I want to use these two functions but I'm not able to output the form so I've commented them 
    //add_settings_section( '', 'Slider settings', '', $menu_slug );
    //add_settings_field( '', 'Auto init', [$this, 'initSettings'], $menu_slug, '' );
  }


function renderOptionsMenu()
  {
    ?>
      <div class="container-fluid">
        <div class="row">
          <h1>Bootstrap Shortcodes - Swiper settings</h1>
          <form action="options.php" method="POST">
            <?php
              settings_fields('my-plugin-options');
              do_settings_sections('my-plugin-options');
            ?>
              <div class="custom-control custom-switch">
                <input type="checkbox" class="custom-control-input" name="my-option" id="customSwitch1" value="<?php echo esc_attr( get_option('my-option') ); ?>">
                <label class="custom-control-label" for="customSwitch1">Auto init</label>
              </div>
            <?php
              submit_button();
            ?>
          </form>
        </div>
      </div>
    <?php
  }

I have a bootstrap 4 switch that is a custom checkbox input type introduced from the 4.3 version. I want to integrate it in a custom option page and assign to it a default value when my option page is rendered. How I can do this, is possible?

here is my actual code, I've omitted the add_action functions but there are present on the plugin code.

function registerSettings()
  {
    $menu_slug = 'my-plugin-options';
    register_setting( $menu_slug, 'my-option' );
    // I want to use these two functions but I'm not able to output the form so I've commented them 
    //add_settings_section( '', 'Slider settings', '', $menu_slug );
    //add_settings_field( '', 'Auto init', [$this, 'initSettings'], $menu_slug, '' );
  }


function renderOptionsMenu()
  {
    ?>
      <div class="container-fluid">
        <div class="row">
          <h1>Bootstrap Shortcodes - Swiper settings</h1>
          <form action="options.php" method="POST">
            <?php
              settings_fields('my-plugin-options');
              do_settings_sections('my-plugin-options');
            ?>
              <div class="custom-control custom-switch">
                <input type="checkbox" class="custom-control-input" name="my-option" id="customSwitch1" value="<?php echo esc_attr( get_option('my-option') ); ?>">
                <label class="custom-control-label" for="customSwitch1">Auto init</label>
              </div>
            <?php
              submit_button();
            ?>
          </form>
        </div>
      </div>
    <?php
  }

Share Improve this question edited Oct 12, 2019 at 14:45 fuxia 107k39 gold badges255 silver badges459 bronze badges asked Oct 12, 2019 at 13:28 userone2userone2 441 silver badge5 bronze badges
Add a comment  | 

1 Answer 1

Reset to default 0

You can use the checked function for this.

https://codex.wordpress/Function_Reference/checked

The value attribute just indicates the value to be saved if the box is checked. eg.

<input type="checkbox" class="custom-control-input" name="my-option" id="customSwitch1" value="1" <?php checked( '1', get_option('my-option'), true ); ?>>

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

相关推荐

  • settings api - Assign value to custom checkbox using get_option()

    I have a bootstrap 4 switch that is a custom checkbox input type introduced from the 4.3 version. I want to integrate it

    17小时前
    10

发表回复

评论列表(0条)

  • 暂无评论

联系我们

400-800-8888

在线咨询: QQ交谈

邮件:admin@example.com

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

关注微信