Wordpress动态选项页面从数组实际保存?

时间:2017-08-07 11:02:43

标签: php arrays wordpress options

我需要创建一个插件来将自定义值保存到options.php

我想在数组中创建字段并且有效。

我迷路的地方是:

  1. 表单实际上不会保存WP数据库中的设置。
  2. 如何创建其他部分?
  3. 我知道它与add_settings_section()等等有关。但我无法找到如何使用数组输入执行此操作。请指教。 ; - )

    到目前为止我的代码:

    <?php
    // Create WordPress admin menu
    function stonehenge_menu_page(){
        $page_title = 'Stonehenge Options'; 
        $menu_title = 'Stonehenge Options';
        $capability = 'manage_options';
        $menu_slug  = 'stonehenge_slug';
        $function   = 'stonehenge_render_page';
        $icon_url   = 'dashicons-admin-plugins';
        $position   = 99;
    
    add_menu_page( $page_title, $menu_title, $capability, $menu_slug, $function, $icon_url, $position );
    
    // Call update_stonehenge function to update database
    add_action( 'admin_init', 'update_stonehenge' );
    }
    
    // Create function to register plugin settings in the database
    function update_stonehenge() {
       register_setting( 'stonehenge-settings', 'stonehenge' );
    }
    
    function stonehenge_render_page() {
    
    $section_1 = array(
        'Field 1' => array(             // $name    Name of the field
            'field' => 'field_1',       // $field   
            'label' => 'Field 1',       // $label   Label of the field
            'type' => 'text',           // $type    input type (text, textarea, option, etc.)       
        ),  
        'Field 2' => array(             // $field   Name of the field
            'field' => 'field_2',       // $field
            'label' => 'Field 2',       // $label   Label of the field
            'type' => 'text',           // $type    input type (text, textarea, option, etc.)       
        ),  
        'Field 3' => array(             // $field   Name of the field
            'field' => 'field_3',           // $field
            'label' => 'Field 3',       // $label   Label of the field
            'type' => 'text',           // $type    input type (text, textarea, option, etc.)       
        ),  
    );
    
    ## START OF MAIN PAGE
    echo '<h1>Stonehenge Options Fields</h1>';
    ?>  
    <form method="post" action="options.php">
    <?php settings_fields( 'stonehenge-settings' ); ?>
    <?php do_settings_sections( 'stonehenge-settings' ); ?>
    
    <!-- >## SECTION 1 should start here </!-->
    <br><h2>Section 1</h2>
    <table>
    <?php
    foreach ($section_1 as $var)    {
    $prefix         = 'stonehenge_';            
    $option_label   = $var['label'];
    $option_name    = $var['field'];
    $option_field   = 'stonehenge_'.$var['field'];
    $option_value   = get_option( $option_field);
    $option_input   = '<input type="' .$var['type']. '"name="'.$option_field.'" id="'.$option_field.'" value="'.$option_value.'"> ';
    $label_for      = '<label for='.$field.'>' . $var['label'] . '</label>';
    ?>
        <tr><th scope="row" align="left"><?php echo $option_label; ?></th>
            <td><?php echo $option_input; ?></td>
            <td>Database Field = <?php echo $option_field; ?></td>
        </tr>
    <?php } ?>
    </table>
    
    <!-- >## SECTION 2 should start here </!-->
    <br><h2>Section 2</h2>
    <table> 
    </table>
    
    <?php submit_button(); ?>
    </form> 
    <?php
    }
    

1 个答案:

答案 0 :(得分:0)

<强>解决!

如果做得正确,事实证明这很简单。 :-)现在我可以添加一个数组,所有其余的是动态创建的。呐喊!呐喊!

输入存储在options.php中作为stonehenge_section_fieldname,并且可以使用get_option轻松调用cal(&#39; stonehenge_section_fieldname&#39;);

如果有人感兴趣的话,到目前为止这里是代码。

<script src="https://ajax.googleapis.com/ajax/libs/angularjs/1.2.23/angular.min.js"></script>
<div ng-app="myApp" ng-controller="AppCtrl as vm">
  <div class="btn-group">
    <button type="button" class="btn btn-default dropdown-toggle" data-toggle="dropdown">
        {{vm.myValue}} 
        <span class="caret"></span>
    </button>
    <ul class="dropdown-menu">
      <li ng-repeat="value in vm.valueList">
        <button class="btn btn-link" ng-click="vm.myValue = value.key">
                {{value.key}}
            </button>
      </li>
    </ul>
  </div>
</div>