因此,我需要添加一个自定义选项页面,根据我能找到的尽可能多的教程,我想出了以下代码来创建一个官员&;主要是我的团队的椅子选项页面,这样我可以为我的网站写更多高级的东西。代码如下:
add_action(\'admin_menu\', \'composit_settings_page\');
function composit_settings_page()
{
add_options_page(\'Officers & Chairs\', \'Officers & Chairs\', \'manage_options\', \'officers_chairs\', \'composit_settings\');
}
function composit_settings()
{
$actives = get_users(\'role=contributor\');
$actives = array_merge($actives, get_users(\'role=editor\'));
$actives = array_merge($actives, get_users(\'role=administrator\'));
$positions = array(
"prytanis",
"epiprytanis",
"hypophetes",
"hegemon",
"grammateus",
"histor",
"pylortes",
"crysopholos"
);
?>
<div class="wrap">
<div id="icon-options-general" class="icon32"><br></div>
<h2>Officers & Chairs</h2>
<form method="post" action="options.php">
<?php settings_fields(\'officers-chairs\'); ?>
<?php foreach($positions as $position):
register_setting(\'officers-chairs\', $position, \'intval\');
endforeach; ?>
<table class="form-table">
<tbody>
<?php foreach($positions as $position): ?>
<tr valign="top">
<th scope="row">
<label for="<?= $position ?>"><?= ucwords($position) ?></label>
</th>
<td>
<select name="<?= $position ?>" id="<?= $position ?>" class="regular-text">
<?php foreach($actives as $active): ?>
<option value="<?= $active->ID ?>" <?php if(get_option($position) == $active->ID) { ?> selected <? } ?>><?=$active->first_name . " " . $active->last_name ?></option>
<?php endforeach; ?>
</select>
</td>
</tr>
<?php endforeach; ?>
</tbody>
</table>
<p class="submit"><input type="submit" name="submit" id="submit" class="button button-primary" value="Save Changes"></p>
</form>
</div>
<?
}
然而,当我去保存它时,我得到以下错误:
ERROR: options page not found.
我觉得自己完全没办法弄明白这一点,但官方文件对我一点帮助都没有。
最合适的回答,由SO网友:david.binda 整理而成
问题在于这一行:
<form method="post" action="options.php">
这会将您的表单发送到
wp-admin/options.php (我猜您不是从这个URL发送表单)。将操作值保留为空,如下所示:
<form method="post" action="">
您的表单将自动发送到发送表单的页面。然后,您可以在输出前保存访问$\\u POST变量的设置
<form method="post" action="options.php">...
希望这有帮助。。。
附言:您还应该考虑使用wordpress nonces。例如:。https://codex.wordpress.org/Function_Reference/wp_nonce_field