So, I need to add a custom options page, and based on as many tutorials I could find, I came up with the following code to create an officers & chairs options page for my group, mainly, so I can write more advanced stuff for my site. The code is here:
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>
<?
}
However, when I go to save that, I get the following error:
ERROR: options page not found.
I feel completely dumb for not being able to figure this out, but the official documentation hasn’t really helped me at all.
The problem part is this line:
This sends your form to wp-admin/options.php (and I guess you’re not sending the form from this URL). Leave the action value empty like this:
And your form will send itself to the same page from which it was sent. Then you can save the settings accessing $_POST variable before outputing
Hope this helps…
PS: you should also consider using wordpress nonces. Eg. https://codex.wordpress.org/Function_Reference/wp_nonce_field