当前位置: 首页 » 笔记 » WordPress主题选项重写优化部分代码保留短连接

WordPress主题选项重写优化部分代码保留短连接

零分笔记1,9722022-09-13 18:49

重写优化了一下WordPress主题设置选项,优化焦点轮播图的设置选项(还是没有把上传写进去,感觉没必要),删除了广告选项,保留了短连接选项!

设置广告,在代码中直接添加一个array数组就可以了,目前代码

<?php
/**
 *零分博客
 *后台设置选项
 */
$shortname = "eson_";    //主题简写,必须是英文、数字、下划线组合 
$options = array (
	'set'=>array(
		'title'=>'综合设置',
		'data'=>array(
			'keyword'=>array(
				'name'=>'keyword',
				'title'=>'网站关键词(Meta Keywords),中间用半角逗号隔开。',
				'num'=>1
			),
			'description'=>array(
				'name'=>'description',
				'title'=>'网站描述(Meta Description),针对搜索引擎设置的网页描述。',
				'num'=>3
			),
			'statement'=>array(
				'name'=>'statement',
				'title'=>'网站声明及联系方式',
				'num'=>3
			),
			'record'=>array(
				'name'=>'record',
				'title'=>'网站ICP备案号',
				'num'=>1
			),
			'statistics'=>array(
				'name'=>'statistics',
				'title'=>'统计代码',
				'num'=>3
			)
		)
	),
	'slider'=>array(
		'title'=>'焦点图',
		'data'=>array(
			'slider'=>array(
				'title'=>'焦点图标题',
				'pic'=>'焦点图URL',
				'url'=>'焦点图链接'
			),
		),
	),
	'short'=>array(
		'title'=>'短连接管理',
		'data'=>array(
			'short'=>array(
				'title'=>'短连接标题',
				'url'=>'长链接URL',
				'shorturl'=>'短连接URL'
			),
		),
	),
	
);
function eson_page (){
	global 	$shortname,$options;
	if ( count($_POST) > 0 && isset($_POST[$shortname.'settings']) ){
		foreach ( $options as $opts ){
			foreach($opts["data"] as $ky=>$opt){
				delete_option ( $shortname.$ky, json_encode($_POST[$ky]) );
				add_option ( $shortname.$ky, json_encode($_POST[$ky]) );	
				}
			}
	}
	add_theme_page(__('主题设置'), __('主题设置'), 'edit_themes', basename(__FILE__), $shortname.'settings');
}
function eson_settings(){global	$shortname,$options;?>
<style type="text/css">
ruby {
	font-size:60px;
	color:red;
	font-family:微软雅黑;
}
.tab {
	border:solid 1px;
	border-color:#aaa;
	width:960px;
	margin:10px auto auto 10px;
	text-align:left;
}
.hd {
	height:25px;
	width:100%;
	border-bottom:solid 1px;
	border-color:#aaa;
}
.hd ul {
	padding:0;
	margin:0;
	height:26px;
	overflow: hidden;
}
.hd .nomal {
	font-size:14px;
	height:26px;
	line-height:25px;
	display:inline-block;
	width:32%;
	text-align:center;
	border-right:solid 1px;
	border-color:#aaa;
	cursor:pointer;
	list-style:none;
	}
.hd .activeTab {
	color:#2481C6;
	font-weight: bold;
	}
#content {
	padding:20px 10px;
	} 
fieldset{
	width:100%;
	border:1px solid #aaa;
	padding-bottom:10px;
	margin-top:5px;
	-webkit-box-shadow:rgba(0,0,0,.2) 0px 0px 5px;
	-moz-box-shadow:rgba(0,0,0,.2) 0px 0px 5px;
	box-shadow:rgba(0,0,0,.2) 0px 0px 5px;
	}
legend{
	margin-left:5px;
	padding:0 5px;
	color:#2481C6;
	cursor:pointer;
	}
textarea{
	width:100%;
	font-size:11px;
	border:1px solid #aaa;
	-webkit-box-shadow:rgba(0,0,0,.2) 1px 1px 2px inset;
	-moz-box-shadow:rgba(0,0,0,.2) 1px 1px 2px inset;
	box-shadow:rgba(0,0,0,.2) 1px 1px 2px inset;
	-webkit-transition:all .4s ease-out;
	-moz-transition:all .4s ease-out;
	}
input:focus,textarea:focus{
	 -webkit-box-shadow:rgba(0,0,0,.2) 0px 0px 8px;
	 -moz-box-shadow:rgba(0,0,0,.2) 0px 0px 8px;
	 box-shadow:rgba(0,0,0,.2) 0px 0px 8px;
	 outline:none;
	 }
.short_input_li,.slider_input_li{
	position:relative;
	height:30px;
	line-height:30px;
	margin:10px 0;
	}
.short_input,.slider_input{
	padding:10px;
	border:1px solid #aaa;
	margin:5px 0;
	position:relative;
	}
.short_input_li label,.slider_input_li label{
	width:100px;
	background:#f1f1f1;
	position:absolute;
	top:0;left:0;
	height:30px;
	line-height:30px;
	} 
.short_input_li input,.slider_input_li input{
	height:30px;
	line-height:30px;
	margin:0 0 0 100px;
	width:750px;
	}
.short_input .btn,.slider_input .btn{
	position:absolute;
	bottom:20px;
	right:10px;
	font-size:30px;
	line-height:30px;
	padding:5px;
	font-weight:800;
	color:#0085ba;

	cursor:pointer;
	}
.short_input .btn i,.slider_input .btn i{
	margin:0 5px;
	}
.form-table td{
	margin:0;
	padding:5px 10px;
	}
.form-table td span{
	padding:5px 0;
	display: block;
	}
</style>
<script src="<?php bloginfo('template_url');?>/images/js/jquery-3.3.1.min.js"></script>
<form method="post">
<input type="hidden" name="eson_settings" value="save" style="display:none;" />
<input type="hidden" name="activeTab" id="activeTab" value="<?php echo isset($_POST['activeTab'])?$_POST["activeTab"]:"set";?>">
<div style="height:40px;line-height:40px;font-size:26px;font-weight: bold;margin: 10px auto auto 10px;">主题设置</div>
	<div class="tab">
		<div class="hd">
			<ul>
			<?php $i=0; foreach($options as $k=>$ops){$i++;?>
			<li class="nomal <?php echo (isset($_POST["activeTab"]) && $_POST["activeTab"]==$k) || (!isset($_POST["activeTab"]) && $i==1) ? 'activeTab':$i;?>" data-tab="<?php echo $k;?>"><?php echo $ops["title"]?></li>
			<?php }?>
			</ul>
		</div>
		
		<div id="content">
			<?php $i=0; foreach($options as $k=>$ops){$i++;?>
				<div id="<?php echo $k;?>" style="<?php echo  (isset($_POST["activeTab"]) && $_POST["activeTab"]==$k) || (!isset($_POST["activeTab"]) && $i==1)?'display:block':'display:none';?>">
				<fieldset><legend><strong><?php echo $ops["title"];?></strong></legend>
				<table class="form-table">
					<?php if($k=="slider" || $k=="short"){?>
					<div class="<?php echo $k;?>" id="<?php echo $k;?>">
					<tr><td>
						<div class="<?php echo $k;?>" id="<?php echo $k;?>">
							
							<?php if(!empty(json_decode(get_option($shortname.$k))) && is_array(json_decode(get_option($shortname.$k)))){?>
								<?php $n=0; foreach(json_decode(get_option($shortname.$k)) as $data){?>
								<div class="<?php echo $k;?>_input">
									<?php  $m=0;  foreach($ops["data"][$k] as $key=>$v){?>
										<div class="<?php echo $k;?>_input_li"><label><?php echo $ops["data"][$k][$key];?>:</label><input type="text" name='<?php echo $k;?>[<?php echo $n;?>][<?php echo $key;?>]' value="<?php echo $key=='shorturl'?bloginfo('url').'/go/5_'.$n.'_0_'.time().'.html':$data->$key;?>"></div>
									<?php  $m++; }?>
									<span class="btn"><?php if($n==0 ){?><i class="addpic" title="添加链接">+</i><?php }else{?><i class="delepic" title="删除链接">-</i><?php } ?></span>
								</div>
								<?php $n++;} ?>
								
								<?php }else{ ?>
								<div class="<?php echo $k;?>_input">
									<?php $m=0; foreach($ops["data"][$k] as $key=>$v){?>
										<div class="<?php echo $k;?>_input_li"><label><?php echo $ops["data"][$k][$key];?>:</label><input type="text" name='<?php echo $k;?>[0][<?php echo $key;?>]' value=""></div>
									<?php  $m++; }?>
									<span class="btn"><?php if($n==0 ){?><i class="addpic" title="添加链接">+</i><?php }else{?><i class="delepic" title="删除链接">-</i><?php } ?></span>
								</div>
								<?php }	?>
							
						</div>
					</td></tr>
					</div>
					<?php }else{?>
					
					<tr><td>
					<?php foreach($ops['data'] as $keys=>$val){?>
						<span><?php echo $val["title"];?></span>
						<textarea name="<?php echo $val["name"];?>" id="<?php echo $val["name"];?>" rows="<?php echo $val["num"];?>" cols="70"><?php echo stripslashes(json_decode(get_option($shortname.$val["name"]))); ?></textarea><br /><br />
						
					
					<?php } ?>
					</td></tr>
					<?php } ?>
		
			</table>
			</fieldset>
			</div>
			<?php } ?>
		</div>

	</div>
	<p class="submit"><input type="submit" name="submit" class="button-primary" value="保存设置" /></p>
</form>

<div id="ok" style="display:none;position:fixed;width:100px;height:20px;line-height:20px;background:#33A600;color:#fff;left:40%;top:30%;padding:10px;text-align:center;border-radius:5px;-moz-border-radius:5px;-webkit-border-radius:5px;">保存成功</div>

<div id="spare-box" style="display:none;">
	<div class="short-box">
		<div class="short_input">
			<div class="short_input_li"><label>短连接标题:</label><input type="text" name="spare-title" value=""></div>
			<div class="short_input_li"><label>长链接URL:</label><input type="text" name="spare-url" value=""></div>
			<div class="short_input_li"><label>短连接URL:</label><input type="text" name="spare-shorturl" value=""></div>
			<span class="btn"><i class="delepic" title="删除链接">-</i></span>
		</div>
	</div>

	<div class="slider-box">
		<div class="slider_input">
		<div class="slider_input_li"><label>焦点图标题:</label><input type="text" name="spare-title" value=""></div>
		<div class="slider_input_li"><label>焦点图URL:</label><input type="text" name="spare-pic" value=""></div>
		<div class="slider_input_li"><label>焦点图链接:</label><input type="text" name="spare-url" value=""></div>
		<span class="btn"><i class="delepic" title="删除链接">-</i></span>
	</div>
	
</div>

</div>

<script type="text/javascript">
$('.tab .hd ul li').on("click",function(){
	if($(this).hasClass('activeTab')) 
		return;
	$('.hd ul li').removeClass('activeTab');
	$(this).addClass('activeTab');
	var tabId = $(this).attr('data-tab');
	$('#content > div').hide();
	$('#' + tabId).show();
	$("#activeTab").val($(this).attr('data-tab'));
	});
$('.submit').on("click",function(){
	$('#ok').fadeIn().delay(1000).fadeOut(1000);
})

$(".addpic").click(function(){
	var div=$(this).parent().parent().parent();
	var id=div.attr("id");
	var i=$("#"+id+">."+id+"_input").length;
	var html=$("."+id+"-box").html();
	html=html.replace('spare-title',id+'['+i+'][title]');
	html=html.replace('spare-url',id+'['+i+'][url]');
	html=html.replace('spare-pic',id+'['+i+'][pic]');
	html=html.replace('spare-shorturl',id+'['+i+'][shorturl]');
	$("#"+id).append(html);
	$(".delepic").on("click",function(){
		$(this).parent().parent().remove();
		});
	
});
$(".delepic").on("click",function(){
	$(this).parent().parent().remove();
	});
</script>
<?php }add_action('admin_menu', 'eson_page');?>

只写适合自己在写主题的选项,记录共参考!

END
零分站龄5个月资深站长
一个喜欢折腾,却又折腾不出像样东西的,不会PHP的PHP程序员!
323
文章
8
分类
125
标签
2
友链
onlinelovesky317355746vipsever@vip.qq.com