0
我需要创建一个插件来将自定义值保存到options.php
。Wordpress动态选项页面从数组中实际保存?
我想创建数组中的字段,并工作。
当我迷路是这样的:
- 形式实际上不会保存在数据库WP的设置。
- 如何创建其他部分?
我知道它与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
}