我对PHP不太在行。我刚刚创建了WordPress管理选项页面,它工作正常:
// Admin Menu
add_action(\'admin_menu\', \'my_cool_plugin_create_menu\');
function my_cool_plugin_create_menu() {
$parent_slug = \'test-slug\';
$capability = \'administrator\';
// sub menus
add_submenu_page( $parent_slug, \'Test\', \'Test\', $capability, \'test\', \'hp_settings_page\');
//call register settings function
add_action( \'admin_init\', \'cm_register_settings\' );
}
function cm_register_settings() {
// title
register_setting(
\'mytheme_group\', // Option group
\'mytheme_title\', // Option name
\'admin_options_sanitize_text_1\' //sanitize callback
);
// IDs
register_setting(
\'mytheme_group\',
\'mytheme_ids\',
\'admin_options_sanitize_text_2\'
);
}
// Admin Page
function hp_settings_page() { ?>
<div class="wrap">
<h2>Test</h2>
<form method="post" action="options.php">
<?php settings_fields( \'mytheme_group\' ); ?>
<?php do_settings_sections( \'mytheme_group\' ); ?>
<table class="form-table">
<tr>
<th scope="row">Title</th>
<td>
<textarea name="mytheme_title" rows="10" cols="100"><?php echo get_option(\'mytheme_title\'); ?></textarea>
</td>
</tr>
<tr>
<th scope="row">IDs</th>
<td>
<textarea name="mytheme_ids" rows="10" cols="100"><?php echo get_option(\'mytheme_ids\'); ?></textarea>
</td>
</tr>
</table>
<?php submit_button(); ?>
</form>
</div><?php
}
// sanitize 1
function admin_options_sanitize_text_1($input) {
$new_input = sanitize_text_field( $input );
return $new_input;
}
// sanitize 2
function admin_options_sanitize_text_2($input) {
$new_input = preg_replace("/[^0-9\\,]/", "", $input );
return $new_input;
}
这将在数据库中创建两行,我可以显示如下值:
echo get_option(\'mytheme_ids\');
echo get_option(\'mytheme_title\');
我需要对此代码进行哪些更改,以便将其保存到数据库中的一行作为数组?然后我可以显示如下值:
$options = get_option(\'mytheme_options\');
echo = $options[\'ids\'];
echo = $options[\'title\'];
感谢您的帮助。