我正常用的主题设置的拓展页面,看效果图:

代码:https://gist.github.com/yao3060/5028434

mythemeoption

<?php
/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
add_action('admin_menu', 'my_theme_options_menu');

function my_theme_options_menu() {
    add_action('admin_print_scripts', 'my_options_admin_scripts');
    add_action('admin_print_styles', 'my_options__admin_styles');
    add_theme_page('My Plugin Theme', 'My Theme Options', 'administrator', 'my-theme-options', 'my_theme_options_function');
    add_action('admin_init', 'register_mysettings');
    add_action('admin_init', 'my_theme_icl_register_string');
}

function register_mysettings() {
    //register our settings
    register_setting('my_theme_options', 'myoption');
}

function my_options_admin_scripts() {
    wp_enqueue_script('media-upload');
    wp_enqueue_script('thickbox');
    wp_enqueue_script('my-upload');
}

function my_options__admin_styles() {
    wp_enqueue_style('thickbox');
}

function my_theme_options_function() {
    if (!current_user_can('manage_options')) {
        wp_die(__('You do not have sufficient permissions to access this page.'));
    }
    $my_theme_options = get_option('myoption'); 
    ?>
  <link rel="stylesheet" href="http://code.jquery.com/ui/1.10.0/themes/base/jquery-ui.css" /> 
  <script src="http://code.jquery.com/ui/1.10.0/jquery-ui.js"></script>  
    <div class="wrap"> 
        <?php screen_icon(); ?>
        <h2>My Theme Options</h2>
        <form method="post" id="my_theme_options" action="options.php"> 
            <?php
            settings_fields('my_theme_options');
            ?>

            <table class="form-table" id="my_theme_options">
                <tr>
                    <th scope="row">Logo</th>
                    <td><input type="text" id="my_logo" class="image_upload"  name="myoption[logo][url]" value="<?php echo $my_theme_options[logo][url]; ?>" ></td>
                    <td>
					<?php 
					if($my_theme_options[logo][id]){
						echo wp_get_attachment_image( $my_theme_options[logo][id], array(100,100));
					}else{
						echo '<img src="'.admin_url().'/images/wordpress-logo.png"><br>';
					}
					?>
					<input type="text" name="myoption[logo][id]" value="<?php echo $my_theme_options[logo][id]; ?>"></td>
                </tr>
                <tr>
                    <th scope="row">Favicon</th>
                    <td><input type="text" id="my_favicon"  class="image_upload"  name="myoption[favicon][url]" value="<?php echo $my_theme_options[favicon][url]; ?>" ></td>
                    <td>
					<?php 
					if($my_theme_options[favicon][id]){
						echo wp_get_attachment_image( $my_theme_options[favicon][id], array(100,100));
					}else{
						echo '<img src="'.admin_url().'/images/wordpress-logo.png"><br>';
					}
					?>
					<input type="text" name="myoption[favicon][id]" value="<?php echo $my_theme_options[favicon][id]; ?>">
					</td>
                </tr>
                <tr>
                    <th scope="row">Home Background</th>
                    <td>
						<input type="text" id="my_homebg"  class="image_upload"  name="myoption[homebg][url]" value="<?php echo $my_theme_options[homebg][url]; ?>" ><br>
						<input type="text" id="my_homebg_pos" name="myoption[homebg][pos]" value="<?php echo $my_theme_options[homebg][pos]; ?>" placeholder="Background Position( 0px 0px)" ><br>
						<input type="text" id="my_homebg_repeat" name="myoption[homebg][repeat]" value="<?php echo $my_theme_options[homebg][repeat]; ?>" placeholder="Background Repeat" ><br>
						<input type="text" id="my_homebg_height" name="myoption[homebg][h]" value="<?php echo $my_theme_options[homebg][h]; ?>" placeholder="Height" >
					</td>
                    <td>
					<?php 
					if($my_theme_options[homebg][id]){
						echo wp_get_attachment_image( $my_theme_options[homebg][id], array(100,100));
					}else{
						echo '<img src="'.admin_url().'/images/wordpress-logo.png"><br>';
					}
					?>
					<input type="text" name="myoption[homebg][id]" value="<?php echo $my_theme_options[homebg][id]; ?>">
					</td>
                </tr>
                <tr>
                    <th scope="row">Contact page</th>
                    <td>
<select name="myoption[contact_id]"> 
 <option value=""><?php echo esc_attr( __( 'Select page' ) ); ?></option> 
 <?php 
  $pages = get_pages(); 
  foreach ( $pages as $page ) {
	if($my_theme_options[contact_id] == $page->ID ){ $select = 'selected="selected"'; }else{ $select = ""; }
  	echo '<option value="'. $page->ID .'" '. $select .'>'.$page->post_title.'</option>';
  }
 ?>
</select>			</td>
                    <td></td>
                </tr>
                <tr>
                    <th scope="row">
					<h2>Categroies</h2>
					<p>Press Ctrl, Mutil select the categories</p>
					</th>
                    <td>
<ol id="selectable">
<?php
$categoryIDs = explode('&', $my_theme_options[categroies][id]);

$categories=get_categories();
foreach($categories as $category) { 
	if(in_array($category->term_id, $categoryIDs)) {
		$term_class = "ui-state-default ui-selected";
	}else{
		$term_class = "ui-state-default";
	}
	echo '<li  class="'.$term_class.'" rel="'.( $category->term_id ).'">'.$category->name.'</li>';
}
?> 
</ol>
					</td>
                    <td><input type="text" id="select_result" name="myoption[categroies][id]" value="<?php echo $my_theme_options[categroies][id]; ?>"></td>
                </tr>
				<tr>
                    <th scope="row">
					 Custom Post Type 

					</th>
                    <td>
<select name="myoption[post_type]"> 	 
<?php 
$post_types=get_post_types(array(  'public'   => true,  '_builtin' => false)); 
foreach ($post_types as $post_type ) {
	if('attachment' != $post_type){
		if($my_theme_options[post_type] == $post_type ){ $thisselect = 'selected="selected"'; }else{ $thisselect = ""; }
		$post_type_object = get_post_type_object($post_type);
		echo '<option value="'. $post_type .'" '. $thisselect .'>'.$post_type_object->labels->singular_name.'</option>';
	}
}
?>
</select>

					</td>
				</tr>

				<tr>
                    <th scope="row">
					 Custom Post Type 2

					</th>
                    <td>
<select name="myoption[post_type2]"> 	 
<?php 

foreach ($post_types as $post_type2 ) {
	if('attachment' != $post_type2){
		if($my_theme_options[post_type2] == $post_type2 ){ $thisselect2 = 'selected="selected"'; }else{ $thisselect2 = ""; }
		$post_type_object = get_post_type_object($post_type2);
		echo '<option value="'. $post_type .'" '. $thisselect2 .'>'.$post_type_object->labels->singular_name.'</option>';
	}
}
?>
</select>

					</td>
				</tr>

                <tr>
                    <th scope="row">Share Lable</th>
                    <td><input type="text" id="my_share_lable"  class="my_text_field"  name="myoption[_share][str]" value="<?php echo $my_theme_options[_share][str]; ?>" ></td>
                    <td><input type="checkbox" name="myoption[_share][t]" <?php if ($my_theme_options[_share][t] == 1) echo "checked"; ?> value="1"> register to WPML? </td>
                </tr>

                <tr>
                    <th scope="row">Share Code</th>
                    <td><textarea rows="3" cols="20" class="my_share_code"  name="myoption[share_code]"><?php echo $my_theme_options[share_code]; ?></textarea></td>
                    <td></td>
                </tr>

                <tr>
                    <th scope="row">Analysis Code</th>
                    <td><textarea rows="3" cols="20" class="my_analysis_code"  name="myoption[analysis]"><?php echo $my_theme_options[analysis]; ?></textarea></td>
                    <td></td>
                </tr>

                <tr>
                    <th scope="row" colspan="3"> <?php submit_button(); ?> </th>
                </tr> 
                <tr>
                    <th scope="row" colspan="3"><pre> <?php print_r($my_theme_options); ?> </pre></th>
                </tr> 

            </table>
        </form>
    </div>
  <style> 
 #selectable .ui-selecting { background: #FECA40; }
  #selectable .ui-selected { background: #F39814; color: white; }
  #selectable { list-style-type: none; margin: 0; padding: 0; width: 60%; }
  #selectable li { margin: 3px; padding: 0.4em;  height: 18px; }
 textarea { width: 100%; height: 200px;}
.form-table td, .form-table th{vertical-align: top;border: 1px solid #e2e2e2; } #my_theme_options input.image_upload { width: 100% } #my_theme_options img { max-width: 100px;}</style>	  
    <script>
        var $ =jQuery.noConflict();

        jQuery(document).ready(function($) {
			$( "#selectable" ).selectable({
			  stop: function() {
				var list_tag_selected = [];
				var result = $( "#select-result" ).empty();
				$( ".ui-selected", this ).each(function() { 
				  list_tag_selected.push($(this).attr("rel"));
				  $('#select_result').val(list_tag_selected.join("&"));
				});
			  }
			});

            $('#my_theme_options input.image_upload').live("click",function(){
                formfield = $(this).attr('name');
                field_id = $(this).attr('id');
                preview_img = $(this).parent().next().find('img');
				preview_id  = $(this).parent().next().find('input');
                //alert(field_id);
                tb_show('', 'media-upload.php?type=image&amp;TB_iframe=true');
                return false;
            });
            window.send_to_editor = function(html) {

				imgelement = $('img',html);
				//alert(imgelement);
				$classes = imgelement.attr('class');
				imgurl = imgelement.attr('src');
				$image_id = $classes.replace(/(.*?)wp-image-/, '');
                $("#"+field_id).val(imgurl);
                preview_img.attr("src",imgurl); 
              	preview_id.val($image_id);
                tb_remove();
            }
        });
    </script>
    <?php
}

function my_theme_icl_register_string() {
    if (function_exists('icl_register_string')) {
        $my_theme_options = get_option('myoption');
        foreach ($my_theme_options as $key => $mt_option) {
            if (is_array($mt_option) && $mt_option[t] == 1) {
                icl_register_string('My theme options', $key, $mt_option[str]);
            }
        }
    }
}
?>

581 thoughts on “My WordPress Theme Options

  1. Raka Channel

    Hiya, I am really glad I have found this info. Today bloggers publish just about gossip and internet stuff and this is really irritating. A good site with exciting content, that’s what I need. Thank you for making this website, and I will be visiting again. Do you do newsletters by email?

  2. HP Oppo

    Hiya, I am really glad I’ve found this info. Nowadays bloggers publish just about gossip and web stuff and this is actually annoying. A good site with exciting content, that’s what I need. Thank you for making this website, and I’ll be visiting again. Do you do newsletters by email?

  3. Bisnis Online

    Hello there. I found your site by the use of Google whilst looking for a comparable matter, your website got here up. It seems to be great. I’ve bookmarked it in my google bookmarks to visit then.

  4. bandar ceme terpercaya

    Hiya, I’m really glad I’ve found this information. Nowadays bloggers publish only about gossip and internet stuff and this is actually irritating. A good website with interesting content, this is what I need. Thank you for making this site, and I’ll be visiting again. Do you do newsletters by email?

  5. obat keputihan gatal

    Hey there. I discovered your web site by the use of Google while looking for a similar matter, your website got here up. It appears great. I’ve bookmarked it in my google bookmarks to come back then.

  6. Convertible

    Excellent weblog right here! Also your web site lots up fast! What host are you using? Can I get your associate hyperlink for your host? I desire my web site loaded up as quickly as yours lol

  7. What is Home Service

    A lot of thanks for your whole hard work on this site. My niece takes pleasure in managing investigation and it is simple to grasp why. Almost all know all about the powerful medium you give practical tricks through this web blog and as well as encourage contribution from some other people on that issue so our princess is studying so much. Take pleasure in the remaining portion of the year. You have been conducting a useful job.

  8. homosexual

    Hello, i feel that i saw you visited my blog thus i got here to go back the prefer?.I am attempting to find things to enhance my web site!I assume its good enough to make use of a few of your concepts!!

  9. homosexual

    Hello there, I discovered your site by means of Google while searching for a related subject, your website got here up, it appears to be like great. I have bookmarked it in my google bookmarks.

  10. homosexual

    Hi there! I simply wish to offer you a huge thumbs up for your great information you’ve got here on this post. I am returning to your blog for more soon.

  11. Harga Rumput Sintetis

    Hiya, I’m really glad I have found this information. Nowadays bloggers publish just about gossip and internet stuff and this is really frustrating. A good blog with interesting content, this is what I need. Thanks for making this web site, and I’ll be visiting again. Do you do newsletters by email?

  12. youtube apk

    Hi there. I discovered your blog by way of Google while searching for a comparable subject, your web site got here up. It looks good. I’ve bookmarked it in my google bookmarks to come back then.

  13. Tukang Las Jakarta

    Hiya, I’m really glad I have found this information. Today bloggers publish just about gossip and web stuff and this is actually irritating. A good site with exciting content, this is what I need. Thanks for making this website, and I will be visiting again. Do you do newsletters by email?

  14. Teak garden

    Hi there. I found your website by the use of Google even as looking for a similar subject, your web site came up. It looks great. I have bookmarked it in my google bookmarks to visit then.

  15. situs judi bola resmi

    Hey there. I found your site by means of Google even as looking for a related topic, your web site came up. It seems great. I have bookmarked it in my google bookmarks to visit then.

  16. Plasa Batam

    Hiya, I’m really glad I have found this information. Nowadays bloggers publish only about gossip and internet stuff and this is actually annoying. A good site with interesting content, this is what I need. Thanks for making this website, and I will be visiting again. Do you do newsletters by email?

  17. yaho togel

    Hiya, I am really glad I have found this info. Today bloggers publish just about gossip and net stuff and this is actually frustrating. A good web site with interesting content, that’s what I need. Thanks for making this web-site, and I’ll be visiting again. Do you do newsletters by email?

  18. situs judi bola

    Hi there. I found your web site by the use of Google at the same time as searching for a similar topic, your web site came up. It appears to be great. I have bookmarked it in my google bookmarks to visit then.

  19. fiforlif

    Awesome post. I’m a regular visitor of your site and appreciate you taking the time to maintain the nice site. I will be a regular visitor for a long time.

  20. sabun laundry

    Hiya, I’m really glad I’ve found this info. Today bloggers publish only about gossip and web stuff and this is actually frustrating. A good blog with exciting content, this is what I need. Thank you for making this web site, and I will be visiting again. Do you do newsletters by email?

  21. streaming film

    Hiya, I’m really glad I have found this information. Today bloggers publish only about gossip and net stuff and this is really frustrating. A good website with exciting content, that is what I need. Thank you for making this web-site, and I will be visiting again. Do you do newsletters by email?

  22. saku qq

    Awesome post. I’m a regular visitor of your web site and appreciate you taking the time to maintain the nice site. I’ll be a frequent visitor for a really long time.

  23. gestunco

    Hello there. I found your blog via Google while looking for a similar topic, your website got here up. It seems good. I’ve bookmarked it in my google bookmarks to visit then.

  24. agen togel terbesar di asia

    Hiya, I am really glad I’ve found this information. Nowadays bloggers publish just about gossip and net stuff and this is really frustrating. A good website with exciting content, that’s what I need. Thanks for making this website, and I will be visiting again. Do you do newsletters by email?

  25. Bandar Togel

    Awesome post. I am a normal visitor of your site and appreciate you taking the time to maintain the nice site. I’ll be a frequent visitor for a long time.

发表评论