最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

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

programmeradmin2浏览0评论

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 ); ?>>
发布评论

评论列表(0)

  1. 暂无评论