forked from weipxiu/Art_Blog
-
Notifications
You must be signed in to change notification settings - Fork 0
/
functions.php
495 lines (455 loc) · 19.4 KB
/
functions.php
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
<?php
//注册菜单
register_nav_menus(array(
'MainNav'=>'主导航',
));
?>
<?php
if ( function_exists('register_sidebar') )
register_sidebar(array(
'before_widget' => '<div class="sidebox"> ',
'after_widget' => '</div>',
'before_title' => '<h2>',
'after_title' => '</h2>',
));
?>
<?php
//注册特色图像
add_theme_support('post-thumbnails');
set_post_thumbnail_size(220, 150, true); // 图片宽度与高度
?>
<?php //控制分页页面,每个页面所显示的文章数量
/*function custom_posts_per_page($query){
if(is_home()){
$query->set('posts_per_page',15);//首页每页显示12篇文章
}
if(is_search()){
$query->set('posts_per_page',10);//搜索页显示所有匹配的文章,不分页
}
if(is_archive()){
$query->set('posts_per_page',15);//其它页面每页显示10篇文章
}//endif
}//function
add_action('pre_get_posts','custom_posts_per_page');*/
?>
<?php //文章分类统计
function wt_get_category_count($input = '') {
global $wpdb;
if($input == '') {
$category = get_the_category();
return $category[0]->category_count;
}
elseif(is_numeric($input)) {
$SQL = "SELECT $wpdb->term_taxonomy.count FROM $wpdb->terms, $wpdb->term_taxonomy WHERE $wpdb->terms.term_id=$wpdb->term_taxonomy.term_id AND $wpdb->term_taxonomy.term_id=$input";
return $wpdb->get_var($SQL);
}
else {
$SQL = "SELECT $wpdb->term_taxonomy.count FROM $wpdb->terms, $wpdb->term_taxonomy WHERE $wpdb->terms.term_id=$wpdb->term_taxonomy.term_id AND $wpdb->terms.slug='$input'";
return $wpdb->get_var($SQL);
}
}
?>
<?php
//获取浏览数-参数文章ID
function getPostViews($postID){
//字段名称
$count_key = 'post_views_count';
//获取字段值即浏览次数
$count = get_post_meta($postID, $count_key, true);
//如果为空设置为0
if($count==''){
delete_post_meta($postID, $count_key);
add_post_meta($postID, $count_key, '0');
return "0";
}
return $count;
}
//设置浏览数-参数文章ID
function setPostViews($postID) {
//字段名称
$count_key = 'post_views_count';
//先获取获取字段值即浏览次数
$count = get_post_meta($postID, $count_key, true);
//如果为空就设为0
if($count==''){
$count = 0;
delete_post_meta($postID, $count_key);
add_post_meta($postID, $count_key, '0');
}else{
//如果不为空,加1,更新数据
$count++;
update_post_meta($postID, $count_key, $count);
}
}
add_filter('show_admin_bar', '__return_false');//去掉默认顶端导航条
//时间显示方式‘xx以前’
function time_ago( $type = 'commennt', $day = 7 ) {
$d = $type == 'post' ? 'get_post_time' : 'get_comment_time';
if (time() - $d('U') > 60*60*24*$day) return;
echo ' (', human_time_diff($d('U'), strtotime(current_time('mysql', 0))), '前)';
}
function timeago( $ptime ) {
$ptime = strtotime($ptime);
$etime = time() - $ptime;
if($etime < 1) return '刚刚';
$interval = array (
12 * 30 * 24 * 60 * 60 => '年前 ('.date('Y-m-d', $ptime).')',
30 * 24 * 60 * 60 => '个月前 ('.date('m-d', $ptime).')',
7 * 24 * 60 * 60 => '周前 ('.date('m-d', $ptime).')',
24 * 60 * 60 => '天前',
60 * 60 => '小时前',
60 => '分钟前',
1 => '秒前'
);
foreach ($interval as $secs => $str) {
$d = $etime / $secs;
if ($d >= 1) {
$r = round($d);
return $r . $str;
}
};
}
// 更改后台字体
function Bing_admin_lettering(){
echo'<style type="text/css">
* { font-family: "Microsoft YaHei" !important; }
i, .ab-icon, .mce-close, i.mce-i-aligncenter, i.mce-i-alignjustify, i.mce-i-alignleft, i.mce-i-alignright, i.mce-i-blockquote, i.mce-i-bold, i.mce-i-bullist, i.mce-i-charmap, i.mce-i-forecolor, i.mce-i-fullscreen, i.mce-i-help, i.mce-i-hr, i.mce-i-indent, i.mce-i-italic, i.mce-i-link, i.mce-i-ltr, i.mce-i-numlist, i.mce-i-outdent, i.mce-i-pastetext, i.mce-i-pasteword, i.mce-i-redo, i.mce-i-removeformat, i.mce-i-spellchecker, i.mce-i-strikethrough, i.mce-i-underline, i.mce-i-undo, i.mce-i-unlink, i.mce-i-wp-media-library, i.mce-i-wp_adv, i.mce-i-wp_fullscreen, i.mce-i-wp_help, i.mce-i-wp_more, i.mce-i-wp_page, .qt-fullscreen, .star-rating .star { font-family: dashicons !important; }
.mce-ico { font-family: tinymce, Arial !important; }
.fa { font-family: FontAwesome !important; }
.genericon { font-family: "Genericons" !important; }
.appearance_page_scte-theme-editor #wpbody *, .ace_editor * { font-family: Monaco, Menlo, "Ubuntu Mono", Consolas, source-code-pro, monospace !important; }
</style>';
}
add_action('admin_head', 'Bing_admin_lettering');
//点赞
add_action('wp_ajax_nopriv_bigfa_like', 'bigfa_like');
add_action('wp_ajax_bigfa_like', 'bigfa_like');
function bigfa_like(){
global $wpdb,$post;
$id = $_POST["um_id"];
$action = $_POST["um_action"];
if ( $action == 'ding'){
$bigfa_raters = get_post_meta($id,'bigfa_ding',true);
$expire = time() + 99999999;
$domain = ($_SERVER['HTTP_HOST'] != 'localhost') ? $_SERVER['HTTP_HOST'] : false; // make cookies work with localhost
setcookie('bigfa_ding_'.$id,$id,$expire,'/',$domain,false);
if (!$bigfa_raters || !is_numeric($bigfa_raters)) {
update_post_meta($id, 'bigfa_ding', 1);
}
else {
update_post_meta($id, 'bigfa_ding', ($bigfa_raters + 1));
}
echo get_post_meta($id,'bigfa_ding',true);
}
die;
}
//最热排行
function hot_posts_list($days=7, $nums=10) {
global $wpdb;
$today = date("Y-m-d H:i:s");
$daysago = date( "Y-m-d H:i:s", strtotime($today) - ($days * 24 * 60 * 60) );
$result = $wpdb->get_results("SELECT comment_count, ID, post_title, post_date FROM $wpdb->posts WHERE post_date BETWEEN '$daysago' AND '$today' ORDER BY comment_count DESC LIMIT 0 , $nums");
$output = '';
if(empty($result)) {
$output = '<li>None data.</li>';
} else {
$i = 1;
foreach ($result as $topten) {
$postid = $topten->ID;
$title = $topten->post_title;
$commentcount = $topten->comment_count;
if ($commentcount != 0) {
$output .= '<li><p><span class="post-comments">评论 ('.$commentcount.')</span><span class="muted"><a href="javascript:;" data-action="ding" data-id="'.$postid.'" id="Addlike" class="action';
if(isset($_COOKIE['bigfa_ding_'.$postid])) $output .=' actived';
$output .='"><i class="fa fa-heart-o"></i><span class="count">';
if( get_post_meta($postid,'bigfa_ding',true) ){
$output .=get_post_meta($postid,'bigfa_ding',true);
} else {$output .='0';}
$output .='</span>喜欢</a></span></p><span class="label label-'.$i.'">'.$i.'</span><a href="'.get_permalink($postid).'" title="'.$title.'">'.$title.'</a></li>';
$i++;
}
}
}
echo $output;
}
//在 WordPress 编辑器添加“下一页”按钮
add_filter('mce_buttons','add_next_page_button');
function add_next_page_button($mce_buttons) {
$pos = array_search('wp_more',$mce_buttons,true);
if ($pos !== false) {
$tmp_buttons = array_slice($mce_buttons, 0, $pos+1);
$tmp_buttons[] = 'wp_page';
$mce_buttons = array_merge($tmp_buttons, array_slice($mce_buttons, $pos+1));
}
return $mce_buttons;
}
function e_secret($atts, $content=null){//输入密码查看
extract(shortcode_atts(array('key'=>null), $atts));
if(isset($_POST['e_secret_key']) && $_POST['e_secret_key']==$key){
return '<div class="e-secret">'.$content.'</div>';
}
else{
return '<form class="e-secret" action="'.get_permalink().'" method="post" name="e-secret"><label>请输入密码:</label><input type="password" name="e_secret_key" class="euc-y-i" maxlength="50"><input type="submit" class="euc-y-s" value="确定"><div class="euc-clear"></div></form>';
}
}
add_shortcode('secret','e_secret');
remove_action( 'wp_head', 'wp_enqueue_scripts', 1 ); //Javascript的调用
remove_action( 'wp_head', 'feed_links', 2 ); //移除feed
remove_action( 'wp_head', 'feed_links_extra', 3 ); //移除feed
remove_action( 'wp_head', 'rsd_link' ); //移除离线编辑器开放接口
remove_action( 'wp_head', 'wlwmanifest_link' ); //移除离线编辑器开放接口
remove_action( 'wp_head', 'index_rel_link' );//去除本页唯一链接信息
remove_action( 'wp_head', 'adjacent_posts_rel_link_wp_head', 10, 0 );
remove_action( 'wp_head', 'locale_stylesheet' );
remove_action('publish_future_post','check_and_publish_future_post',10, 1 );
remove_action( 'wp_head', 'noindex', 1 );
remove_action( 'wp_head', 'wp_print_styles', 8 );//载入css
remove_action( 'wp_head', 'wp_print_head_scripts', 9 );
remove_action( 'wp_head', 'wp_generator' ); //移除WordPress版本
remove_action( 'wp_head', 'rel_canonical' );
remove_action( 'wp_footer', 'wp_print_footer_scripts' );
remove_action( 'wp_head', 'wp_shortlink_wp_head', 10, 0 );
remove_action( 'template_redirect', 'wp_shortlink_header', 11, 0 );
add_action('widgets_init', 'my_remove_recent_comments_style');
function my_remove_recent_comments_style() {
global $wp_widget_factory;
remove_action('wp_head', array($wp_widget_factory->widgets['WP_Widget_Recent_Comments'] ,'recent_comments_style'));
}
//禁止加载WP自带的jquery.js
if ( !is_admin() ) { // 后台不禁止
function my_init_method() {
wp_deregister_script( 'jquery' ); // 取消原有的 jquery 定义
}
add_action('init', 'my_init_method');
}
wp_deregister_script( 'l10n' );
add_action('after_wp_tiny_mce', 'add_button_mce');
function add_button_mce($mce_settings) {//扩充发表文章编辑器的导航标签
?>
<script type="text/javascript">
QTags.addButton( '注意', '注意', "<span class='beCareful'>", "</span>" );
QTags.addButton( 'a', 'a', "<a href=''>", "</a>" );
QTags.addButton( 'p', 'p', "\n<p>", "</p>" );
QTags.addButton( 'span', 'span', "\n<span>", "</span>" );
QTags.addButton( 'h1', 'h1', "\n<h1>", "</h1>" );
QTags.addButton( 'h4', 'h4', "\n<h4>", "</h4>" );
QTags.addButton( 'h5', 'h5', "\n<h5>", "</h5>" );
QTags.addButton( 'cc', 'cc', "\n[cc lang='php']", "[/cc]" );
QTags.addButton( 'secret', 'secret', "\n[secret key='123']", "[/secret]" );
QTags.addButton( 'embed', 'embed', "\n[embed]\n", "\n[/embed]" );
</script>
<?php
}
// 彩色静态标签云 Color Tag Cloud
// function colorCloud($text) {
// $text = preg_replace_callback('|<a (.+?)>|i', 'colorCloudCallback', $text);
// return $text;
// }
// function colorCloudCallback($matches) {
// $text = $matches[1];
// $color = dechex(rand(0,16777215));
// $pattern = '/style=(\'|\")(.*)(\'|\")/i';
// $text = preg_replace($pattern, "style=\"color:#{$color};$2;\"", $text);
// return "<a $text>";
// }
// add_filter('wp_tag_cloud', 'colorCloud', 1);
// 搜索结果关键词高亮显示
// function lee_set_query() {
// $query = attribute_escape(get_search_query());
// if(strlen($query) > 0){
// echo '
// <script type="text/javascript">
// var lee_query = "'.$query.'";
// </script>
// ';
// }
// }
// function lee_init_jquery() {
// wp_enqueue_script('jquery');
// }
// add_action('init', 'lee_init_jquery');
// add_action('wp_print_scripts', 'lee_set_query');
// 文章外部链接加上nofollow
// add_filter( 'the_content', 'cn_nf_url_parse');
// function cn_nf_url_parse( $content ) {
// $regexp = "<a\s[^>]*href=(\"??)([^\" >]*?)\\1[^>]*>";
// if(preg_match_all("/$regexp/siU", $content, $matches, PREG_SET_ORDER)) {
// if( !empty($matches) ) {
// $srcUrl = get_option('siteurl');
// for ($i=0; $i < count($matches); $i++)
// {
// $tag = $matches[$i][0];
// $tag2 = $matches[$i][0];
// $url = $matches[$i][0];
// $noFollow = '';
// $pattern = '/target\s*=\s*"\s*_blank\s*"/';
// preg_match($pattern, $tag2, $match, PREG_OFFSET_CAPTURE);
// if( count($match) < 1 )
// $noFollow .= ' target="_blank" ';
// $pattern = '/rel\s*=\s*"\s*[n|d]ofollow\s*"/';
// preg_match($pattern, $tag2, $match, PREG_OFFSET_CAPTURE);
// if( count($match) < 1 )
// $noFollow .= ' rel="nofollow" ';
// $pos = strpos($url,$srcUrl);
// if ($pos === false) {
// $tag = rtrim ($tag,'>');
// $tag .= $noFollow.'>';
// $content = str_replace($tag2,$tag,$content);
// }
// }
// }
// }
// $content = str_replace(']]>', ']]>', $content);
// return $content;
// }
// 自定义登录界面
function custom_login(){
echo '<link rel="stylesheet" type="text/css" href="https://www.weipxiu.com/style-login.4205e3e2.css" />';
}
add_action('login_head', 'custom_login');
function login_headerurl($url) {
return get_bloginfo('url');
}
add_filter('login_headerurl', 'login_headerurl');
function login_headertitle($title){
return __('唯品秀');
}
add_filter('login_headertitle', 'login_headertitle');
// 解决找回密码链接无效问题
function reset_password_message( $message, $key ) {
if ( strpos($_POST['user_login'], '@') ) {
$user_data = get_user_by('email', trim($_POST['user_login']));
} else {
$login = trim($_POST['user_login']);
$user_data = get_user_by('login', $login);
}
$user_login = $user_data->user_login;
$msg = __('当前有请求重设如下帐号的密码:'). "\r\n\r\n";
$msg .= network_site_url() . "\r\n\r\n";
$msg .= sprintf(__('用户名:%s'), $user_login) . "\r\n\r\n";
$msg .= __('若这不是您本人要求的,请忽略本邮件,一切如常。') . "\r\n\r\n";
$msg .= __('要重置您的密码,请打开下面的链接:'). "\r\n\r\n";
$msg .= network_site_url("wp-login.php?action=rp&key=$key&login=" . rawurlencode($user_login), 'login') ;
return $msg;
}
add_filter('retrieve_password_message', reset_password_message, null, 2);
// 面包屑导航注册代码
function wheatv_breadcrumbs() {
$delimiter = '<i>></i>';
$name = '首页'; //text for the 'Home' link
$currentBefore = '';
$currentAfter = '';
if ( !is_home() && !is_front_page() || is_paged() ) {
echo '';
global $post;
// $home = get_bloginfo('url');
$home = get_option('home');
echo '<a href="'.$home.'" >'. $name . ' </a>' . $delimiter . ' ';
if ( is_category() ) {
global $wp_query;
$cat_obj = $wp_query->get_queried_object();
$thisCat = $cat_obj->term_id;
$thisCat = get_category($thisCat);
$parentCat = get_category($thisCat->parent);
if ($thisCat->parent != 0) echo(get_category_parents($parentCat, TRUE, ' ' . $delimiter . ' '));
echo $currentBefore . '';
single_cat_title();
echo '' . $currentAfter;
} elseif ( is_day() ) {
echo '' . get_the_time('Y') . ' ' . $delimiter . ' ';
echo '' . get_the_time('F') . ' ' . $delimiter . ' ';
echo $currentBefore . get_the_time('d') . $currentAfter;
} elseif ( is_month() ) {
echo '' . get_the_time('Y') . ' ' . $delimiter . ' ';
echo $currentBefore . get_the_time('F') . $currentAfter;
} elseif ( is_year() ) {
echo $currentBefore . get_the_time('Y') . $currentAfter;
} elseif ( is_single() ) {
$cat = get_the_category(); $cat = $cat[0];
echo get_category_parents($cat, TRUE, ' ' . $delimiter . ' ');
echo $currentBefore;
the_title();
echo $currentAfter;
} elseif ( is_page() && !$post->post_parent ) {
echo $currentBefore;
the_title();
echo $currentAfter;
} elseif ( is_page() && $post->post_parent ) {
$parent_id = $post->post_parent;
$breadcrumbs = array();
while ($parent_id) {
$page = get_page($parent_id);
$breadcrumbs[] = '' . get_the_title($page->ID) . '';
$parent_id = $page->post_parent;
}
$breadcrumbs = array_reverse($breadcrumbs);
foreach ($breadcrumbs as $crumb) echo $crumb . ' ' . $delimiter . ' ';
echo $currentBefore;
the_title();
echo $currentAfter;
} elseif ( is_search() ) {
echo $currentBefore . '搜索结果' . get_search_query() . '' . $currentAfter;
} elseif ( is_tag() ) {
echo $currentBefore . '搜索标签: ';
single_tag_title();
echo '' . $currentAfter;
} elseif ( is_author() ) {
global $author;
$userdata = get_userdata($author);
echo $currentBefore . 'Articles posted by ' . $userdata->display_name . $currentAfter;
} elseif ( is_404() ) {
echo $currentBefore . 'Error 404' . $currentAfter;
}
if ( get_query_var('paged') ) {
if ( is_category() || is_day() || is_month() || is_year() || is_search() || is_tag() || is_author() ) echo ' (';
echo __('第') . '' . get_query_var('paged') . '页';
if ( is_category() || is_day() || is_month() || is_year() || is_search() || is_tag() || is_author() ) echo ')';
}
echo '';
}
}
//取消内容转义
remove_filter('the_content', 'wptexturize');
//取消摘要转义
remove_filter('the_excerpt', 'wptexturize');
//取消评论转义
remove_filter('comment_text', 'wptexturize');
//更改编辑器默认视图为HTML/文本
add_filter('wp_default_editor', create_function('', 'return "html";'));
//关闭wordpress各种更新,避免插件不兼容
add_filter('pre_site_transient_update_core',create_function('$a', "return null;")); // 关闭核心提示
add_filter('pre_site_transient_update_plugins',create_function('$a', "return null;")); // 关闭插件提示
add_filter('pre_site_transient_update_themes',create_function('$a', "return null;")); // 关闭主题提示
remove_action('admin_init', '_maybe_update_core'); // 禁止 WordPress 检查更新
remove_action('admin_init', '_maybe_update_plugins'); // 禁止 WordPress 更新插件
remove_action('admin_init', '_maybe_update_plugins'); // 禁止 WordPress 更新插件
remove_action('admin_init', '_maybe_update_themes'); // 禁止 WordPress 更新主题
//防止CC攻击
session_start(); //开启session
$timestamp = time();
$ll_nowtime = $timestamp ;
//判断session是否存在 如果存在从session取值,如果不存在进行初始化赋值
if ($_SESSION){
$ll_lasttime = $_SESSION['ll_lasttime'];
$ll_times = $_SESSION['ll_times'] + 1;
$_SESSION['ll_times'] = $ll_times;
}else{
$ll_lasttime = $ll_nowtime;
$ll_times = 1;
$_SESSION['ll_times'] = $ll_times;
$_SESSION['ll_lasttime'] = $ll_lasttime;
}
//现在时间-开始登录时间 来进行判断 如果登录频繁 跳转 否则对session进行赋值
if(($ll_nowtime - $ll_lasttime) < 3){
if ($ll_times>=5){
header("location:https://127.0.0.1");//可以换成其他链接,比如站内的404错误显示页面(千万不要用动态页面)
exit;
}
}else{
$ll_times = 0;
$_SESSION['ll_lasttime'] = $ll_nowtime;
$_SESSION['ll_times'] = $ll_times;
}
//恢复wordpress删除的友情链接功能
add_filter('pre_option_link_manager_enabled','__return_true');
?>