Add theme options to wordpress
Below is the code to add theme options to wordpress, in this below code you can add fields like text field, file and wp editor field.
///////////////////////////////////////////////////////////
///////////////Wordpress Add Theme Options/////////////////
///////////////////////////////////////////////////////////
function theme_settings_page()
{
?>
<div class="wrap">
<h1>Theme Panel</h1>
<form method="post" action="options.php" enctype="multipart/form-data">
<?php
settings_fields("section");
do_settings_sections("theme-options");
submit_button();
?>
</form>
</div>
<?php
}
function add_theme_menu_item()
{
add_menu_page("Theme Setting", "Theme Setting", "manage_options", "theme-panel", "theme_settings_page", null, 99);
}
add_action("admin_menu", "add_theme_menu_item");
function display_text()
{
?>
<input type="text" name="display_text_field" id="display_text_field" value="<?php echo get_option('display_text_field'); ?>" />
<?php
}
function display_texteditor()
{
$content = get_option('display_texteditor_field');
$editor_id = 'display_texteditor_field';
$settings = array( 'textarea_name' => 'display_texteditor_field','editor_height' => 200, // In pixels, takes precedence and has no default value
'textarea_rows' => 6,);
echo wp_editor( $content, $editor_id, $settings );
}
function display_image()
{
?>
<input type="file" name="display_image_field" />
<input type="hidden" name="display_image_field_hidden" value="<?php echo get_option('display_image_field'); ?>" readonly />
<img src="<?php echo get_option('display_image_field'); ?>" width="150" />
<?php
}
function handle_logo_upload()
{
if(!empty($_FILES["display_image_field"]["tmp_name"]))
{
$urls = wp_handle_upload($_FILES["display_image_field"], array('test_form' => FALSE));
$temp = $urls["url"];
return $temp;
}else{
return $_POST['display_image_field_hidden'];
}
return $option;
}
function display_theme_panel_fields()
{
add_settings_section("section", "All Settings", null, "theme-options");
add_settings_field("display_text_field", "Popup Heading", "display_text", "theme-options", "section");
add_settings_field("display_image_field", "Image", "display_image", "theme-options", "section");
add_settings_field("display_texteditor_field", "Popup Content", "display_texteditor", "theme-options", "section");
register_setting("section", "display_text_field");
register_setting("section", "display_image_field", "handle_logo_upload");
register_setting("section", "display_texteditor_field");
}
add_action("admin_init", "display_theme_panel_fields");
///////////////////////////////////////////////////////////
///////////////Wordpress Add Theme Options/////////////////
///////////////////////////////////////////////////////////