首页 » 后端 » Wordpress » 正文

WordPress 在页面上提示搜索引擎和关健字

发布者:站点默认
2011/04/25 浏览数(2,996) 分类:Wordpress 评论(2)

提示:
直接看代码吧(为防止冲突函数名前加了“upall_”使用时可以替换掉,^_^)。
效果:

来自搜索引擎的关键字高亮提示

来自搜索引擎的关键字高亮提示

主题下的function.php:

<?php // upall:判断来自哪个搜索引擎
function upall_get_delim($ref) {
	$search_engines = array (
		'google.com' => 'q',
		'google.com.hk' => 'q',
		'baidu.com' => 'wd',
		'search.yahoo.com' => 'p',
		'cn.search.yahoo.com' => 'p',
		'cn.bing.com' => 'q',
		'search.lycos.com' => 'query',
		'youdao.com' => 'q',
		'search.aol.com' => 'query',
		'ask.com' => 'q',
		'hotbot.com' => 'query',
		'overture.com' => 'Keywords',
		'search.netscape.com' => 'query',
		'search.looksmart.com' => 'qt',
		'search.earthlink.net' => 'q',
		'search.viewpoint.com' => 'k',
		'mamma.com' => 'query'
	);
	$delim = FALSE;
	if (isset($search_engines[$ref])) {
		$delim = $search_engines[$ref];
	}
	return $delim;
}
function upall_get_terms($d) {
	$terms = null;
	$query_array = array();
	$query_terms = null;
	$query = explode($d.'=', $_SERVER['HTTP_REFERER']);
	$query = explode('&', $query[1]);
	$query = urldecode($query[0]);
	$query = str_replace("'", '', $query);
	$query = str_replace('"', '', $query);
	$query_array = preg_split('/[\s,\+\.]+/',$query);
	$query_terms = implode(' ', $query_array);
	$terms = htmlspecialchars(urldecode($query_terms));
	if (!seems_utf8($terms)){
		$terms=iconv("GBK", "UTF-8", $terms);
	}
	return $terms;
}
function upall_get_refer() {
	if (!isset($_SERVER['HTTP_REFERER']) || ($_SERVER['HTTP_REFERER'] == '')) return FALSE;
	$referer_info = parse_url($_SERVER['HTTP_REFERER']);
	$referer = $referer_info['host'];
	if (substr($referer, 0, 4) == 'www.')
		$referer = substr($referer, 4);
	return $referer;
}
function upall_getinfo($what) {
	$referer = upall_get_refer();
	if (!$referer) return FALSE;
	$delimiter = upall_get_delim($referer);

	if ($delimiter) {
		$terms = upall_get_terms($delimiter);
		if ($what == 'isref') {
			return ($terms != ''?true:false);
		}
		if ($what == 'referrer') {
			$parsed = parse_url($_SERVER['HTTP_REFERER']);
			//echo '<a href="http://'.$parsed['host'].'">'.$parsed['host'].'</a>';
			echo $parsed['host'];
		}
		if ($what == 'terms') {
			echo $terms;
		}
	}
}
function  upall_terms_filter($content){
	$referer = upall_get_refer();
	$output = '';
	$delimiter = upall_get_delim($referer);
	if($delimiter) {
		$terms = upall_get_terms($delimiter);
		$terms= array_unique(preg_split("/[\s,]+/", $terms));
		$contentarr = preg_split("/(<.*>)/U", $content, -1, PREG_SPLIT_DELIM_CAPTURE);
		$stop = count($contentarr);
		for ($i = 0; $i < $stop; $i++) {
			$content = $contentarr[$i];
			if ((strlen($content) > 0) && ('<' != $content{0})) {
				foreach ($terms as $term) {
				if (strlen($term)>1)
					$content=eregi_replace("($term)","<b style='background-color:#ffff00;'>\\1</b>", $content);
				}
			}
			$output .= $content;
		}
		return $output;
	} else{
		return $content;
	}
}
function upall_get_se_keywords(){
	$referer = upall_get_refer();
	$delimiter = upall_get_delim($referer);
	$terms = upall_get_terms($delimiter);
	$terms= array_unique(preg_split("/[\s,]+/", $terms));
	$keywords = '';
	foreach ($terms as $term) {
		$keywords .= $term . ' ';
	}
	echo $keywords;
}
add_filter('the_content', 'upall_terms_filter');
add_filter('comment_text', 'upall_terms_filter');
function upall_set_header() {
	if (upall_getinfo('isref')) header('Vary: Referer', FALSE);
}
add_action('init', 'upall_set_header');
// upall:end:判断来自哪个搜索引擎
?>

主题下的footer.php:

<?php if (upall_getinfo('isref')) : ?>
<div id='seTip'> 提示:您通过 <?php upall_getinfo('referrer'); ?> 搜索的关健字
	<strong><?php upall_get_se_keywords();?></strong>
	已在文章正文中高亮显示。
	<span class="closebox"><a onclick="javascript:document.getElementById('seTip').style.display='none';" title="关闭">×</a></span>
</div>
<?php endif; ?>

主题下的style.css:

#seTip {
	background:#000000;
	border:1px solid #B3B3B3;
	color:#FFFFFF;
	font-size:14px;
	opacity:0.7;
	filter:alpha(opacity=70);
	padding:10px;
	position:fixed;
	width:280px;
	margin-left:-140px;
	left:50%;
	top:5px;
	border-radius:8px;
	box-shadow: 2px 2px 5px #333333;
}
#seTip a {
	cursor:pointer;
	float:right;
	margin:5px;
	display:block;
	height:15px;
	width:15px;
	text-align:center;
	line-height:15px;
	overflow:hidden;
}
#seTip strong{
	background-color:#ffff00;
	color:#000;
}

<完。摘自:Lift Studio

点击返回顶部
  1. 留言
  2. 联系方式