Here’s a quick options page that will give you a dropdown select for choosing a page, using the get_pages
function. The Settings API takes care of saving the options for you. You can then use get_option
to load the options array in your template, and get_post
to load the post data associated with the ID saved in your option.
add_action( 'admin_init', 'russ_options_init' );
add_action( 'admin_menu', 'russ_options_page' );
function russ_options_init(){
register_setting(
'russ_options_group',
'russ_options',
'russ_options_validate'
);
}
function russ_options_page() {
add_options_page(
'Russ Options',
'Russ Options',
'manage_options',
'russ_options',
'russ_render_options'
);
}
function russ_render_options() {
?>
<div class="wrap">
<form method="post" action="options.php">
<?php
settings_fields( 'russ_options_group' );
$options = get_option( 'russ_options' );
?>
<table class="form-table">
<tr valign="top"><th scope="row">Choose a page</th>
<td>
<select name="russ_options[page_id]">
<?php
if( $pages = get_pages() ){
foreach( $pages as $page ){
echo '<option value="' . $page->ID . '" ' . selected( $page->ID, $options['page_id'] ) . '>' . $page->post_title . '</option>';
}
}
?>
</select>
</td>
</tr>
</table>
<p class="submit">
<input type="submit" class="button-primary" value="<?php _e('Save Changes') ?>" />
</p>
</form>
</div>
<?php
}
function russ_options_validate( $input ) {
// do some validation here if necessary
return $input;
}