Sample code for theme options page. It can be added to the functions.php file of the theme, or extend it and put in on different file and then include the file in functions.php
Note that the fields are not sanitized, refer to WordPress site for more info.
<div id="theme-options-wrap">
<div class="icon32" id="icon-tools"></div>
<h2>Theme Settings</h2>
Update various settings throughout your website.
<form action="options.php" enctype="multipart/form-data" method="post"><!--?php settings_fields('theme_options'); ?--> <!--?php do_settings_sections(__FILE__); ?-->
<p class="submit"><input class="button-primary" type="submit" name="Submit" value="<?php esc_attr_e('Save Changes'); ?>" /></p>
</form></div>
<!--?php <br ?-->
} add_action('admin_init', 'register_and_build_fields');
function register_and_build_fields() {
register_setting('theme_options', 'theme_options', 'validate_setting');
add_settings_section('homepage_settings', 'Homepage Settings', 'section_homepage', __FILE__);
add_settings_section('footer_settings', 'Footer Settings', 'section_footer', __FILE__);
function section_homepage() {
}
function section_footer() {
}
add_settings_field('button1text', 'Button 1 Text', 'button1text_setting', __FILE__, 'homepage_settings');
add_settings_field('button1link', 'Button 1 URL', 'button1link_setting', __FILE__, 'homepage_settings');
add_settings_field('button2text', 'Button 2 Text', 'button2text_setting', __FILE__, 'homepage_settings');
add_settings_field('button2link', 'Button 2 URL', 'button2link_setting', __FILE__, 'homepage_settings');
add_settings_field('button3text', 'Button 3 Text', 'button3text_setting', __FILE__, 'homepage_settings');
add_settings_field('button3link', 'Button 3 URL', 'button3link_setting', __FILE__, 'homepage_settings');
add_settings_field('phonenumber', 'Phone Number', 'phonenumber', __FILE__, 'footer_settings');
add_settings_field('facebookurl', 'Facebook URL', 'facebookurl', __FILE__, 'footer_settings');
add_settings_field('googleurl', 'Google+ URL', 'googleurl', __FILE__, 'footer_settings');
add_settings_field('twitterurl', 'Twitter URL', 'twitterurl', __FILE__, 'footer_settings');
}
function validate_setting($theme_options) {
return $theme_options;
}
function button1text_setting() {
$options = get_option('theme_options');
echo "<input type="text" name="theme_options[button1text_setting]" value="{$options[" />";
}
function button1link_setting() {
$options = get_option('theme_options');
echo "<input type="text" name="theme_options[button1link_setting]" value="{$options[" />";
}
function button2text_setting() {
$options = get_option('theme_options');
echo "<input type="text" name="theme_options[button2text_setting]" value="{$options[" />";
}
function button2link_setting() {
$options = get_option('theme_options');
echo "<input type="text" name="theme_options[button2link_setting]" value="{$options[" />";
}
function button3text_setting() {
$options = get_option('theme_options');
echo "<input type="text" name="theme_options[button3text_setting]" value="{$options[" />";
}
function button3link_setting() {
$options = get_option('theme_options');
echo "<input type="text" name="theme_options[button3link_setting]" value="{$options[" />";
}
function phonenumber() {
$options = get_option('theme_options');
echo "<input type="text" name="theme_options[phonenumber]" value="{$options[" />";
}
function facebookurl() {
$options = get_option('theme_options');
echo "<input type="text" name="theme_options[facebookurl]" value="{$options[" />";
}
function googleurl() {
$options = get_option('theme_options');
echo "<input type="text" name="theme_options[googleurl]" value="{$options[" />";
}
function twitterurl() {
$options = get_option('theme_options');
echo "<input type="text" name="theme_options[twitterurl]" value="{$options[" />";
}
add_action('admin_menu', 'theme_options_page');
function theme_options_page() {
add_menu_page('Theme Settings', 'Theme Settings', 'administrator', __FILE__, 'build_options_page', '', '62.2');
}
Then we create a variable with all our settings so we can use it in the template.
<?php $options = get_option('theme_options'); ?>
Then we can use some of the options like this:
<a href="<?php echo $options['button1link_setting']; ?>"><?php echo $options['button1text_setting']; ?></a>