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
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
|
<?php
//start//http://blog.wpjam.com/m/postviews-for-admin/
//统计浏览量,配合WP-PostViews插件
add_filter('manage_posts_columns', 'postviews_admin_add_column');
function postviews_admin_add_column($columns){
$columns['views'] = __('Views');
return $columns;
}
add_action('manage_posts_custom_column','postviews_admin_show',10,2);
function postviews_admin_show($column_name,$id){
if ($column_name != 'views')
return;
$post_views = get_post_meta($id, "views",true);
echo $post_views;
}
//end------------------------------------------------------
//更改作者链接//默认为http://youthlin.com/author/用户名
//https://codex.wordpress.org/Plugin_API/Filter_Reference/author_link
add_filter( 'author_link', 'modify_author_link', 10, 1 );
function modify_author_link( $link ) {
$link = 'http://youthlin.com/';
return $link;
}
//end-------------------------------------------------------
//网络字体替换。用add_filter
function my_font_url() {
$font_url = '';
if ( 'off' !== _x( 'on', 'Lato font: on or off', 'twentyfourteen' ) ) {
$font_url = add_query_arg( 'family',
urlencode( 'Lato:300,400,700,900,300italic,400italic,700italic' ), "//fonts.useso.com/css" );
//http://doufu.me/tofu/219.html http://www.amznz.com/fonts-googleapis-com-load-slow/
}
return $font_url;
}
add_filter('twentyfourteen_font_url','my_font_url');
//end-------------------------------------------------------
//评论回复电邮通知
function comment_mail_notify($comment_id) {
$admin_notify = '1'; // admin 要不要收回复通知 ( '1'=要 ; '0'=不要 )
$admin_email = get_bloginfo ('admin_email'); // $admin_email 可改为你指定的 e-mail.
$comment = get_comment($comment_id);
$comment_author_email = trim($comment->comment_author_email);
$parent_id = $comment->comment_parent ? $comment->comment_parent : '';
global $wpdb;
if ($wpdb->query("Describe {$wpdb->comments} comment_mail_notify") == '')
$wpdb->query("ALTER TABLE {$wpdb->comments} ADD COLUMN comment_mail_notify TINYINT NOT NULL DEFAULT 0;");
if ( ($comment_author_email != $admin_email && isset($_POST['comment_mail_notify']))
|| ($comment_author_email == $admin_email && $admin_notify == '1') )
$wpdb->query("UPDATE {$wpdb->comments} SET comment_mail_notify='1' WHERE comment_ID='$comment_id'");
$notify = $parent_id ? get_comment($parent_id)->comment_mail_notify : '0';
$spam_confirmed = $comment->comment_approved;
if ($parent_id != '' && $spam_confirmed != 'spam' && $notify == '1') {
$wp_email = 'no-reply@' . preg_replace('#^www\.#', '', strtolower($_SERVER['SERVER_NAME']));
// e-mail 发出点, no-reply 可改为可用的 e-mail.
$to = trim(get_comment($parent_id)->comment_author_email);
$subject = '您在 [' . get_option("blogname") . '] 的留言有了回复';
$message = '
<div style="background-color:#eef2fa; border:1px solid #d8e3e8; color:#111; padding:0 15px; -moz-border-radius:5px; -webkit-border-radius:5px; -khtml-border-radius:5px;">
<p><strong>' . trim(get_comment($parent_id)->comment_author) . '</strong>, 您好!</p>
<p>您曾在《' . get_the_title($comment->comment_post_ID) . '》的留言:<br /><div style="color:#060;background-color:#cc3;padding:20px 20px;box-shadow: 10px 10px 5px #123;border-radius:20px;" >
' . trim(get_comment($parent_id)->comment_content) .'</div></p>
<p><strong>' . trim($comment->comment_author) . ' </strong>.给您的回复:<br /><div style="border-radius:25px;color:#000;background-color:#090;box-shadow: 5px 10px 5px #321;padding:20px 20px;margin-top:20px;"> ' . trim($comment->comment_content) . ' </div><br /></p>
<p>您可以<a href="' . htmlspecialchars(get_comment_link($parent_id)) . '"><strong style="color:#22f;background-color:#cc3;border-radius:20px;">点击查看回复的完整內容</strong></a></p>
<p>欢迎再度光临 <a href="' . get_option('home') . '">' . get_option('blogname') . '</a></p>
<p>(此邮件由系统自动发送,请勿回复.)</p>
</div>';
//样式是自己写的 Youth.霖 http://youthlin.com/
$from = "From: \"" . get_option('blogname') . "\" <$wp_email>";
$headers = "$from\nContent-Type: text/html; charset=" . get_option('blog_charset') . "\n";
wp_mail( $to, $subject, $message, $headers );
//echo 'mail to ', $to, '<br/> ' , $subject, $message; // for testing
}
}
add_action('comment_post', 'comment_mail_notify');
// 自动加勾选栏
function add_checkbox() {
echo '<p><input type="checkbox" name="comment_mail_notify" id="comment_mail_notify" value="comment_mail_notify" checked="checked" style="margin:6px 4px;float:left;" /><label for="comment_mail_notify">有人回复时邮件通知我</label></p>';
}
add_action('comment_form', 'add_checkbox');
//回复END--------------------------------------------------------
//后台登陆数学验证码 http://www.tennfy.com/1794.html
function myplugin_add_login_fields() {
//获取两个随机数, 范围0~9
$num1=rand(0,9);
$num2=rand(0,9);
//最终网页中的具体内容
echo "<p><label for='math' class='small'>验证码<br />
$num1 + $num2 = ?
<input type='text' name='sum' class='input' value='' size='25'>"
."<input type='hidden' name='num1' value='$num1'>"
."<input type='hidden' name='num2' value='$num2'></label></p>";
}
add_action('login_form','myplugin_add_login_fields');
function login_val() {
if($_SERVER['REQUEST_METHOD'] == "POST"){
$sum=$_POST['sum'];
switch($sum){
case $_POST['num1']+$_POST['num2']:break;
case null:wp_die('错误: 请输入验证码.');break;
default:wp_die('错误: 验证码错误,请重试.');
}
}
}
add_action('login_form_login','login_val');
//end-----------------------------------------------------------
//wp-login.php添加参数
add_action('login_enqueue_scripts','login_protection');
function login_protection(){
if($_SERVER['REQUEST_METHOD']=='POST'){
if ( $_POST['sum'] == $_POST['num1'] + $_POST['num2'])
return;
}
if($_GET['abc'] == '123')//必须加?abc=123才能打开登录页面。//当然得换个不好猜测的
return;
header('Location: http://youthlin.com/');
}
//end-----------------------------------------------
//添加后台快捷键提交评论
add_action('admin_footer', 'Bing_admin_comment_ctrlenter');
function Bing_admin_comment_ctrlenter(){
echo
'<script type="text/javascript">
jQuery(document).ready(function($){
$("textarea").keypress(function(e){
if(e.ctrlKey&&e.which==13||e.which==10){
$("#replybtn").click();
}
});
});</script>';
};
//end----------------------------------------------------
//标签云 边栏http://www.biantime.com/bian/caise-biaoqian --彼岸时光网
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);
//end-----------------------------------------------------
//垃圾评论拦截 http://www.bgbk.org/wp-code-comment-spam.html
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><textarea name=\"comment\" cols=\"100%\" rows=\"4\" style=\"display:none\"></textarea>",$input);') );
}
}
function gate() {
if ( !empty($_POST['w']) && empty($_POST['comment']) ) {
$_POST['comment'] = $_POST['w'];
} else {
$request = $_SERVER['REQUEST_URI'];
$referer = isset($_SERVER['HTTP_REFERER']) ? $_SERVER['HTTP_REFERER'] : '隐瞒';
$IP = isset($_SERVER["HTTP_X_FORWARDED_FOR"]) ? $_SERVER["HTTP_X_FORWARDED_FOR"] . ' (透过代理)' : $_SERVER["REMOTE_ADDR"];
$way = isset($_POST['w']) ? '手动操作' : '未经评论表格';
$spamcom = isset($_POST['comment']) ? $_POST['comment'] : null;
$_POST['spam_confirmed'] = "请求: ". $request. "\n来路: ". $referer. "\nIP: ". $IP. "\n方式: ". $way. "\n內容: ". $spamcom. "\n -- 记录成功 --";
}
}
function sink( $comment ) {
if ( !empty($_POST['spam_confirmed']) ) {
if ( in_array( $comment['comment_type'], array('pingback', 'trackback') ) ) return $comment;
//方法一: 直接挡掉, 將 die(); 前面两斜线刪除即可.
die();
//方法二: 标记为 spam, 留在资料库检查是否误判.
//add_filter('pre_comment_approved', create_function('', 'return "spam";'));
//$comment['comment_content'] = "[ 小墙判断这是 Spam! ]\n". $_POST['spam_confirmed'];
}
return $comment;
}
}
$anti_spam = new anti_spam();
//end---------------------------------貌似并没有什么用(-。-;)--------------------------
//屏蔽纯英文留言
function scp_comment_post( $incoming_comment ) {
$pattern = '/[一-龥]/u';
if(!preg_match($pattern, $incoming_comment['comment_content'])) {
wp_die( "You should type some Chinese word (like 你好) in your comment to pass the spam-check, thanks for your patience! 请使用中文留言!" );
}
return( $incoming_comment );
}
add_filter('preprocess_comment', 'scp_comment_post');
//end---------------------------------------------------------
// 替换 WordPress 默认 Emoji 资源地址
function c7sky_change_wp_emoji_baseurl($url){
return set_url_scheme('//twemoji.maxcdn.com/72x72/');
}
add_filter('emoji_url', 'c7sky_change_wp_emoji_baseurl');
//end----------------http://c7sky.com/change-the-wordpress-emoji-cdn.html--------
//系统表情指向主题表情(修改表情代码_代码1/3)
add_filter('smilies_src','custom_smilies_src',1,20);
function custom_smilies_src ($img_src, $img, $siteurl){
return get_bloginfo('template_directory').'/../2014/images/smilies/'.$img;
}
//表情代码转换图片(修改表情代码_代码2/3)
if ( !isset( $wpsmiliestrans ) ) {
$wpsmiliestrans = array(
' ' => 'icon_question.gif',
' ' => 'icon_razz.gif',
' ' => 'icon_sad.gif',
'[/愤怒]' => 'icon_smile.gif',
' ' => 'icon_redface.gif',
' ' => 'icon_biggrin.gif',
' ' => 'icon_surprised.gif',
' ' => 'icon_eek.gif',
' ' => 'icon_confused.gif',
' ' => 'icon_cool.gif',
' ' => 'icon_lol.gif',
' ' => 'icon_mad.gif',
' ' => 'icon_rolleyes.gif',
' ' => 'icon_wink.gif',
' ' => 'icon_neutral.gif',
' ' => 'icon_cry.gif',
' ' => 'icon_arrow.gif',
' ' => 'icon_exclaim.gif',
' ' => 'icon_evil.gif',
' ' => 'icon_mrgreen.gif',
':?:' => 'icon_question.gif',
':razz:' => 'icon_razz.gif',
':sad:' => 'icon_sad.gif',
':evil:' => 'icon_smile.gif',
':!:' => 'icon_redface.gif',
':smile:' => 'icon_biggrin.gif',
':oops:' => 'icon_surprised.gif',
':grin:' => 'icon_eek.gif',
':eek:' => 'icon_confused.gif',
':shock:' => 'icon_cool.gif',
':???:' => 'icon_lol.gif',
':cool:' => 'icon_mad.gif',
':lol:' => 'icon_rolleyes.gif',
':mad:' => 'icon_wink.gif',
':twisted:' => 'icon_neutral.gif',
':roll:' => 'icon_cry.gif',
':wink:' => 'icon_arrow.gif',
':idea:' => 'icon_exclaim.gif',
':arrow:' => 'icon_evil.gif',
':neutral:' => 'icon_mrgreen.gif',
':cry:' => 'icon_eek.gif',
':mrgreen:' => 'icon_razz.gif',
);
}
//3/3在comment.php
//end--------------------------------------
//wordpress版权信息代码
function add_copyright($content) {
if(is_single() or is_feed()) {
$content.= "<hr />";
$content.= '<div class="copyright" style="border: 1px solid;font-size: smaller;background-color: beige;border-radius: 9px">
<div style="border-left-color: green;border-left-style: solid;border-left-width: 5px;margin: 1%;">
<h5 style="margin:0;"><small>声明</small></h5><ul style="margin-bottom:0">
<li class="copyright-li">本作品采用<a rel="license" href="http://creativecommons.org/licenses/by-nc-sa/4.0/" target="_blank">知识共享署名-非商业性使用-相同方式共享 4.0 国际许可协议</a>进行许可。除非注明,<a href="http://youthlin.com" target="_blank"><strong>霖博客</strong></a>文章均为原创。</li>
<li class="copyright-li">转载请保留本文(<a name="theTitle">《'.get_the_title().'》</a>)链接地址 : <u name="shortLink">'.wp_get_shortlink(get_the_ID()).'</u></li>
<li class="copyright-li">订阅本站:<a title="霖博客的RSS源" href="http://youthlin.com/feed/" rel="nofollow">http://youthlin.com/feed/</a></li></ul></div></div><!--.copyright-->';
}
return $content;
}
add_filter ('the_content', 'add_copyright');
//end----------------------------------------------------------
//fancybox 图片暗箱 header里需要引用CSS/JS
add_filter('the_content', 'fancybox');
function fancybox ($content){
global $post;
$pattern = "/<a(.*?)href=('|\")([^>]*).(bmp|gif|jpeg|jpg|png|swf)('|\")(.*?)>(.*?)<\/a>/i";
$replacement = '<a$1href=$2$3.$4$5 rel="box" class="fancybox"$6>$7</a>';
$content = preg_replace($pattern, $replacement, $content);
return $content;
}
//end-------------------------------------------
//近期评论 http://youthlin.com/?p=610
class My_Widget_Recent_Comments extends WP_Widget {
//新版Wordpress不能继承于WP_Widget_Recent_Comments了,后台不显示
public function __construct() {
$widget_ops = array('classname' => 'my_widget_recent_comments', 'description' => __('显示最新评论内容'));
parent::__construct('my-recent-comments', __('近期评论(带头像)'), $widget_ops);
}
function widget( $args, $instance ) {
global $comments, $comment;
$cache = wp_cache_get('widget_recent_comments', 'widget');
if ( ! is_array( $cache ) )
$cache = array();
if ( ! isset( $args['widget_id'] ) )
$args['widget_id'] = $this->id;
if ( isset( $cache[ $args['widget_id'] ] ) ) {
echo $cache[ $args['widget_id'] ];
return;
}
extract($args, EXTR_SKIP);
$output = '';
$title = ( ! empty( $instance['title'] ) ) ? $instance['title'] : __( 'Recent Comments' );
$title = apply_filters( 'widget_title', $title, $instance, $this->id_base );
$number = ( ! empty( $instance['number'] ) ) ? absint( $instance['number'] ) : 5;
if ( ! $number )
$number = 5;
//////////////////// 2 这里 /////////////////////////
$comments = get_comments( apply_filters( 'widget_comments_args', array( 'number' => $number, 'status' => 'approve', 'post_status' => 'publish' , 'user_id' => 0) ) );
$output .= $before_widget;
if ( $title )
$output .= $before_title . $title . $after_title;
$output .= '<style>.recmtfleft{float:left;margin-right:4px;}.recmtauthor{font-weight:900;}span.recntfright {display: block;min-height: 53px;}.recentcomments{clear:both;padding:2px 1px;border-bottom: 1px dotted gray;}.recmtfleft .avatar{border-radius: 8px; -webkit-border-radius: 8px; -moz-border-radius:8px;}</style><ul id="recentcomments">';
if ( $comments ) {
// Prime cache for associated posts. (Prime post term cache if we need it for permalinks.)
$post_ids = array_unique( wp_list_pluck( $comments, 'comment_post_ID' ) );
_prime_post_caches( $post_ids, strpos( get_option( 'permalink_structure' ), '%category%' ), false );
foreach ( (array) $comments as $comment) {
//////////////////// 1 这里 /////////////////////////
$avatar = '<span class="recmtfleft">'.get_avatar($comment,53,'','头像').'</span>';
$output .= '<li class="recentcomments">' . /* translators: comments widget: 1: comment author, 2: post link */
sprintf(_x('%1$s : %2$s', 'widgets'),
$avatar .'<span class="recntfright"><span class="recmtauthor fn">'. get_comment_author_link().'</span>',
'<a href="'. esc_url( get_comment_link($comment->comment_ID) )
. '" title="《'.get_the_title($comment->comment_post_ID).'》,于'.get_comment_date('Y-m-j')
. '">'
. mb_strimwidth(strip_tags($comment->comment_content),0,64, '[...]') . '</a>') . '</span></li>';
}
}
/*http://www.typemylife.com/wordpress-recent-comments-remove-author-name-display-content/
修改步骤一:
把(_x('%1$s on %2$s', 'widgets')里面的这个单词“on”改成冒号“:”。
修改步骤二:
把get_the_title($comment->comment_post_ID)改为mb_strimwidth(strip_tags($comment->comment_content),0,50, 。。。》)。 这里的数字“50”是用来限制评论显示的字数,可以自行修改,至于后边那个小尾巴"。。。》"则是用来在实际评论字数少于允许显示的字数时补充空白处的,也可以依自己喜欢的格式修改之。(_注:。。。》要用单引号括起来)
以上修改完成后,最新评论的格式就变为:“读者ID”+":"+“实际评论内容”。
2)让最新评论不显示作者自己的评论
修改对象依然是上面提到的default-widgets.php文件。
搜索到以下代码片段:
$comments = get_comments( apply_filters( 'widget_comments_args', array( 'number' => $number, 'status' => 'approve', 'post_status' => 'publish' ) ) );
修改为以下格式:
$comments = get_comments( apply_filters( 'widget_comments_args', array( 'number' => $number, 'status' => 'approve', 'post_status' => 'publish', 'type' => 'comment', 'user_id' => 0 ) ) );
解释一下:'user_id' => 0效果为不显示站长自己的回复,'type' => 'comment'效果为只显示评论类留言,即,不显示pingback和trackback类留言。
*/
$output .= '</ul>';
$output .= $after_widget;
echo $output;
$cache[$args['widget_id']] = $output;
wp_cache_set('widget_recent_comments', $cache, 'widget');
}
public function update( $new_instance, $old_instance ) {
$instance = $old_instance;
$instance['title'] = sanitize_text_field( $new_instance['title'] );
$instance['number'] = absint( $new_instance['number'] );
return $instance;
}
public function form( $instance ) {
$title = isset( $instance['title'] ) ? $instance['title'] : '';
$number = isset( $instance['number'] ) ? absint( $instance['number'] ) : 5;
?>
<p><label for="<?php echo $this->get_field_id( 'title' ); ?>"><?php _e( 'Title:' ); ?></label>
<input class="widefat" id="<?php echo $this->get_field_id( 'title' ); ?>" name="<?php echo $this->get_field_name( 'title' ); ?>" type="text" value="<?php echo esc_attr( $title ); ?>" /></p>
<p><label for="<?php echo $this->get_field_id( 'number' ); ?>"><?php _e( 'Number of comments to show:' ); ?></label>
<input class="tiny-text" id="<?php echo $this->get_field_id( 'number' ); ?>" name="<?php echo $this->get_field_name( 'number' ); ?>" type="number" step="1" min="1" value="<?php echo $number; ?>" size="3" /></p>
<?php
}
public function flush_widget_cache() {
_deprecated_function( __METHOD__, '4.4' );
}
}
register_widget('My_Widget_Recent_Comments');
//end--------------------------------------------------------------------------
//说说
/** http://nichen.info/add-mood-widget/
* WP添加侧边栏“心情随笔”
*
* http://www.nuodou.com/a/856.html
* WordPress自定义侧边栏小工具
* 2012年06月07日 Mr.诺豆
*
* 小工具接口
* http://codex.wordpress.org/zh-cn:%E5%B0%8F%E5%B7%A5%E5%85%B7%E6%8E%A5%E5%8F%A3/
*/
class Saying_Comments extends WP_Widget{
public function __construct(){
$widget_ops = array('classname'=>'widget_saying','description'=>_('一个类似QQ空间“说说”的小工具。'));
parent::__construct(false,_('说说'),$widget_ops);
}
function form($instance){
//title:标题 pageid:页面ID listnum:显示数量 charnum:截取字长
$instance = wp_parse_args((array)$instance,array('title'=>'说说','pageid'=>0,'listnum'=>8,'charnum'=>54));//默认值
$title = htmlspecialchars($instance['title']);
$pageid = htmlspecialchars($instance['pageid']);
$listnum = htmlspecialchars($instance['listnum']);
$charnum = htmlspecialchars($instance['charnum']);
echo '<p style="text-align:left;"><label for="'.$this->get_field_name('title')
.'">标题:<input style="width:200px;" id="'.$this->get_field_id('title').'" name="'.$this->get_field_name('title')
.'" type="text" value="'.$title.'" /></label></p>';
echo '<p style="text-align:left;"><label for="'.$this->get_field_name('pageid')
.'">页面ID:<input style="width:200px;" id="'.$this->get_field_id('pageid')
.'" name="'.$this->get_field_name('pageid').'" type="text" value="'.$pageid.'" /></label></p>';
echo '<p style="text-align:left;"><label for="'.$this->get_field_name('listnum')
.'">显示条数:<input style="width:200px;" id="'.$this->get_field_id('listnum').'" name="'
.$this->get_field_name('listnum').'" type="text" value="'.$listnum.'" /></label></p>';
echo '<p style="text-align:left;"><label for="'.$this->get_field_name('charnum')
.'">截取字长:<input style="width:200px" id="'.$this->get_field_id('charnum').'" name="'
.$this->get_field_name('charnum').'" type="text" value="'.$charnum.'" /></label></p>';
}
function update($new_instance, $old_instance) {
$instance = $old_instance;
$instance['title'] = strip_tags(stripslashes($new_instance['title']));
$instance['pageid'] = $new_instance['pageid'];
$instance['listnum'] = strip_tags(stripslashes($new_instance['listnum']));
$instance['charnum'] = strip_tags(stripslashes($new_instance['charnum']));
return $instance;
}
//$args是注册侧边栏的注册的几个变量
//$instance是小工具的设置数据
function widget($args,$instance){
// http://nichen.info/add-mood-widget/
// @see WP_Widget_Recent_Comments
global $comments, $comment;
extract($args); //将数组展开
$title = ( ! empty( $instance['title'] ) ) ? $instance['title'] : __( '说说' );
$title = apply_filters( 'widget_title', $title, $instance, $this->id_base );
//$title = apply_filters('widget_title', empty($instance['title']) ? __('说说') : $instance['title']);
$pageid = empty($instance['pagedid']) ? $instance['pageid'] : 0;
$listnum = empty($instance['listnum']) ? 5 : $instance['listnum'];
$charnum = empty($instance['charnum']) ? 140 : $instance['charnum'];
$avatar = get_avatar(1,60,'','头像');
// http://codex.wordpress.org/Function_Reference/get_avatar
$argument = array(
'number' => $listnum,
'post_id' => $pageid,
'user_id' => '1',
'parent' => '0'
);
$wbfollow='<wb:follow-button uid="3191703225" type="red_1" width="67" height="24" style="position:absolute;margin-top:16px;margin-left:10px;"></wb:follow-button>';
//微博关注按钮
$gplus='<!-- 将此代码放置在你希望显示徽章的位置。 -->
<a href="//plus.google.com/u/0/111325266765620895423?prsrc=3"
rel="publisher" target="_blank" style="text-decoration:none;position: absolute;margin-top: 14px;margin-left: 80px;">
<img src="//ssl.gstatic.com/images/icons/gplus-16.png" alt="Google+" style="border:0;width:20px;height:20px;"/>
</a>';
// http://codex.wordpress.org/zh-cn:函数参考/get_comments
$comments = get_comments($argument);
$output = '';
$output .= $before_widget;
if ( $title ){
$title = $avatar.'<a href="'.get_permalink($pageid) .'">' . $title . '</a>' . $wbfollow . $gplus;
$output .= $before_title . $title . $after_title;
}
//echo $pageid;//输出页面ID调试以查看pageid是否正确
$output .= '<ul id="saying">';
if ( $comments){
foreach ( (array) $comments as $comment){
$output .= '<li class="saying">'
. '<span><a href="'.esc_url( get_comment_link($comment->comment_ID) ).'">'
. convert_smilies(mb_strimwidth(strip_tags($comment->comment_content),0,$charnum,'[...]'))
// http://blog.wpjam.com/function_reference/convert_smilies/
. '</a></span><br />'
. '<span>'.get_comment_date('m月d日H:i ',$comment->comment_ID) . '</span></li>';
}
}
$output .= '</ul>';
$output .= $after_widget;
echo $output;
}
}
add_action('widgets_init', create_function('', 'return register_widget("Saying_Comments");'));
//end---------------------------------------------------
//随机文章
/**
* Random_Posts widget class
*
* Author: haoxian_zeng <http://cnzhx.net/>
* Date: 2013.05.14, cnzhx2011 1.0
*/
class WP_Widget_myRandom_Posts extends WP_Widget {
function __construct() {
$widget_ops = array('classname' => 'widget_my_random_posts', 'description' => __( '水景一页定制的随机文章微件。The cnzhx customized random posts widget.' ) );
parent::__construct('random-posts', __('随机文章 Random Posts'), $widget_ops);
$this->alt_option_name = 'widget_my_random_posts';
}
function widget( $args, $instance ) {
global $randomposts, $post;
extract($args, EXTR_SKIP);
$output = '';
// 设置 widget 标题
$title = apply_filters('widget_title', empty($instance['title']) ? __('随机文章 Random Posts') : $instance['title']);
// 设置要获取的文章数目
if ( ! $number = absint( $instance['number'] ) )
$number = 5;
// WP 数据库查询,使用 rand 参数来获取随机的排序,并取用前面的 $number 个文章
$randomposts = get_posts( array( 'number' => $number, 'orderby' => 'rand', 'post_status' => 'publish' ) );
// 下面开始准备输出数据
// 先输出一般的 widget 前缀
$output .= $before_widget;
// 输出标题
if ( $title )
$output .= $before_title . $title . $after_title;
// random posts 列表开始
$output .= '<ul id="randomposts">';
if ( $randomposts ) {
foreach ( (array) $randomposts as $post) {
$output .= '<li><a href="' . get_permalink() . '">' . $post->post_title . '</a></li>';
}
}
$output .= '</ul>';
// 输出一般的 widget 后缀
$output .= $after_widget;
// 输出到页面
echo $output;
}
function update( $new_instance, $old_instance ) {
$instance = $old_instance;
$instance['title'] = strip_tags($new_instance['title']);
$instance['number'] = absint( $new_instance['number'] );
$alloptions = wp_cache_get( 'alloptions', 'options' );
if ( isset($alloptions['widget_my_random_posts']) )
delete_option('widget_my_random_posts');
return $instance;
}
// 在 WP 后台的 widget 内部显示两个参数, 1. 标题;2. 显示文章数目
function form( $instance ) {
$title = isset($instance['title']) ? esc_attr($instance['title']) : '';
$number = isset($instance['number']) ? absint($instance['number']) : 5;
?>
<p><label for="<?php echo $this->get_field_id('title'); ?>"><?php _e('Title:'); ?></label>
<input class="cnzhx" id="<?php echo $this->get_field_id('title'); ?>" name="<?php echo $this->get_field_name('title'); ?>" type="text" value="<?php echo $title; ?>" /></p>
<p><label for="<?php echo $this->get_field_id('number'); ?>"><?php _e('Number of posts to show:'); ?></label>
<input id="<?php echo $this->get_field_id('number'); ?>" name="<?php echo $this->get_field_name('number'); ?>" type="text" value="<?php echo $number; ?>" size="3" /></p>
<?php
}
}
// register WP_Widget_myRandom_Posts widget
add_action( 'widgets_init', create_function( '', 'register_widget("WP_Widget_myRandom_Posts");' ) );
//end-------------------------------------------------
///http://www.freehao123.com/wordpress-gravatar/#toc-4
////缓存头像
function mytheme_get_avatar($avatar) {
//gravatar.duoshuo.com//并不能用反而cn.gravatar.com好使
$avatar = str_replace(array("www.gravatar.com","0.gravatar.com","1.gravatar.com","2.gravatar.com"),"cn.gravatar.com",$avatar);
return $avatar;
}
add_filter( 'get_avatar', 'mytheme_get_avatar', 10, 3 );
//end-------------------------------------------
//页面首部header添加自定义代码
function add_header(){
$keywords="";
$description="";
global $post;
//如果是首页
if (is_home()){
$keywords = "霖博客,wordpress,Youth.霖";
$description = "Youth.霖的博客。";
}
//如果是文章页
elseif (is_single()){
//默认使用文章页添加关键字
$keywords = get_post_meta($post->ID, "keywords", true);
//如果为空,使用标签作为关键字
if($keywords == ""){
$tags = wp_get_post_tags($post->ID);
foreach ($tags as $tag){
$keywords = $keywords.$tag->name.",";
}
//去掉最后一个,
$keywords = rtrim($keywords, ', ');
}
//默认使用文章页添加描述
$description = get_post_meta($post->ID, "description", true);
}
//如果是页面,使用页面添加的关键字和描述
elseif (is_page()){
$keywords = get_post_meta($post->ID, "keywords", true);
$description = get_post_meta($post->ID, "description", true);
}
//如果是分类页,使用分类名作为关键字,分类描述作为描述
elseif (is_category()){
$keywords = single_cat_title('', false);
$description = category_description();
}
//如果是标签页,使用标签名作为关键字,标签描述作为描述
elseif (is_tag()){
$keywords = single_tag_title('', false);
$description = tag_description();
}
if (!is_404()) {
//标签为空则用标题
if ($keywords == "") {
$keywords = get_the_title($post->ID);
}
//描述为空则用前100个字
if($description == ""){
if($post->post_excerpt){
$description = $post->post_excerpt;
}else{
$description = mb_strimwidth(strip_tags(apply_filters('the_content',$post->post_content)),0,200);
}
}
}
$keywords = trim(strip_tags($keywords));
$description = trim(strip_tags($description));
echo "<meta name=\"keywords\" content=\"$keywords\" />\n"
."<meta name=\"description\" content=\"$description\"/>\n";
if( is_single() || is_page() ) {
if( function_exists('get_query_var') ) {
$cpage = intval(get_query_var('cpage'));
$commentPage = intval(get_query_var('comment-page'));
}
if( !empty($cpage) || !empty($commentPage) ) {
echo '<meta name="robots" content="noindex, nofollow" />';
echo "\n";
}
}
echo '<script type="text/javascript" src="'.get_bloginfo('template_directory').'/../2014/fancybox/fancybox.js"></script>'
."\n<script type='text/javascript'>jQuery(document).ready(function() {jQuery('.fancybox').fancybox();});</script>"
."\n<link rel='stylesheet' type='text/css' href='". get_bloginfo('template_directory')."/../2014/fancybox/fancybox.css' />";
if ( is_singular() ){
echo "\n<script type=\"text/javascript\" src=\"".get_bloginfo('template_directory')."/../2014/comments-ajax.js\"></script>\n";
}
?>
<script type="text/javascript">
//评论分页ajax
jQuery(document).ready(function($) {
$body = (window.opera) ? (document.compatMode == "CSS1Compat" ? $('html') : $('body')) : $('html,body');
$(document).on('click', '.comments-navi a', function(e) {
e.preventDefault();
$.ajax({
type: "GET",
url: $(this).attr('href'),
beforeSend: function() {
$('.comments-navi').remove();
$('.comment-list').remove();
$('#loading-comments').slideDown();
$body.animate({scrollTop: $('#comments').offset().top - 65}, 800 );
//http://www.w3school.com.cn/jquery/css_offset.asp/////////http://kayosite.com/ajax-turn-page-for-wordpress-comments-list.html#comment-8925
},//beforeSend
dataType: "html",
success: function(out) {
result = $(out).find('.comment-list');
abovenav = $(out).find('#comment-nav-above');
belownav = $(out).find('#comment-nav-below');
$('#loading-comments').slideUp(550);
$('#loading-comments').after(result.fadeIn(800));
result.before(abovenav);
result.after(belownav);
$('.fn a').click (function() {$(this).attr('target','_blank');});
}//success
});//$.ajax
});//function(e)
//2014主题固定侧栏:
$(window).scroll(function(){
if ($('body').hasClass('masthead-fixed') && $('html').width() > 990) {
$('#secondary').css('position','fixed');
$('#secondary').css('margin-left',0);
$('#secondary').css('top','50px');
$('#secondary').css('padding','10px');
$('#secondary').css('width','160px');
if ($('html').width()>1260) {$('#masthead').css('width','99%');}
}else{
$('#secondary').removeAttr('style');$('#masthead').css('width','100%');
}
});
});//ready
</script>
<?php
}
add_action('wp_head','add_header');
//end header-------------------------------------------------
//复制文字添加链接 http://www.dreamfy.com/notes/wordpress/767.html
function add_footer() { ?>
<style type="text/css">#colophon>.site-info{display: none;border-top: none;}.gotoTop:hover{background-color: green;}
.my-site-footer>.site-info {border-top: 1px solid rgba(255, 255, 255, 0.2);}</style>
<div class="site-footer my-site-footer"><div class="site-info">
<div class="gotoTop"><a style="display:block;padding: 1%;margin: auto;text-align: center;font-size: larger;font-weight: bold;" href="javascript:scroll(0,0)">返回顶部</a></div>
©2011-<?php echo date('Y'); ?> <span class="copyAuthor" > <a href="http://youthlin.com/" target="_blank">霖博客</a></span>
自豪地采用 <a href="http://wordpress.org/" target="_blank">WordPress</a> ,
<a href="https://my.laoxuehost.net/aff.php?aff=2315" target="_blank" rel="nofallow">老薛主机</a>.
<a href="http://youthlin.com/sitemap.xml" target="_blank">站点地图</a>.</div></div>
<script src="http://tjs.sjs.sinajs.cn/open/api/js/wb.js" type="text/javascript" charset="utf-8"></script>
<script type="text/javascript">
jQuery(document).ready(function($){
//Ctrl+Enter回复
jQuery(document).keypress(function(e){
if(e.ctrlKey && e.which == 13 || e.which == 10) {
jQuery("#submit").click();
}
})
var msgctrl = '提示:按Ctrl+Enter快速提交';
jQuery(".form-submit").before(msgctrl);
$('.fn a').click (function() {$(this).attr('target','_blank');});
});
(function(i,s,o,g,r,a,m){i['GoogleAnalyticsObject']=r;i[r]=i[r]||function(){
(i[r].q=i[r].q||[]).push(arguments)},i[r].l=1*new Date();a=s.createElement(o),
m=s.getElementsByTagName(o)[0];a.async=1;a.src=g;m.parentNode.insertBefore(a,m)
})(window,document,'script','//www.google-analytics.com/analytics.js','ga');
ga('create', 'UA-46211856-2', 'auto');
ga('send', 'pageview');
</script>
<script type='text/javascript'>
function addLink() {
var body_element = document.getElementsByTagName('body')[0];
var selection;
selection = window.getSelection();
var pagelink = "<br /><br />//来源: <a href='"+document.location.href+"'>"+document.location.href+"</a>";
var copy_text = selection + pagelink;
var new_div = document.createElement('div');
new_div.style.left='-99999px';
new_div.style.position='absolute';
body_element.appendChild(new_div );
new_div.innerHTML = copy_text ;
selection.selectAllChildren(new_div );
window.setTimeout(function() {
body_element.removeChild(new_div );
},0);
}
document.oncopy = addLink;
</script>
<div id="shangxia" style="position:fixed;">
<div id="shang"></div>
<?php if (is_single()||is_page() ) { ?><div id="comt"></div><?php } ?>
<div id="xia"></div>
</div>
<script type="text/javascript">
jQuery(document).ready(function($){
$('#shang').click(function(){$('html,body').animate({scrollTop: '0px'}, 1000);$(this).mouseout();});
$('#comt').click(function(){$('html,body').animate({scrollTop:$('#comments').offset().top-45}, 1300);});
$('#xia').click(function(){$('html,body').animate({scrollTop:$('#colophon').offset().top},1000);
$(this).mouseout();});
/* 咸菜一点米追加的代码段开始 */
//来源: http://youthlin.com/20151025.html
var xcy_me = 0 ;
$('#shang').hover(
function(){if(!xcy_me){xcy_me = setInterval(function() {$(document).scrollTop($(document).scrollTop() - 4 );},44);}},
function(){if(xcy_me) {clearInterval(xcy_me);xcy_me = 0;}}
);/* 咸菜一点米追加的代码段结束 */
// http://gnqc.jx.cn
var flag = 0;
$('#xia').hover(
function(){if(!flag){flag = setInterval(function(){$(document).scrollTop($(document).scrollTop() + 4 );},44);}},
function(){clearInterval(flag);flag = 0;}
);// http://gnqc.jx.cn
});
</script>
<!-- 将此标记放置在你希望显示+1 按钮的位置。 -->
<!--div class="g-plusone" data-annotation="none"></div-->
<!-- 将此标记放置在最后一个 +1 按钮 标记之后。 -->
<script type="text/javascript">
window.___gcfg = {lang: 'zh-CN'};
(function() {
var po = document.createElement('script'); po.type = 'text/javascript'; po.async = true;
po.src = 'https://apis.google.com/js/platform.js';
var s = document.getElementsByTagName('script')[0]; s.parentNode.insertBefore(po, s);
})();
</script>
<div class="youthlin" style="display:none;">
<!--腾讯分析20140224-->
<script type="text/javascript" src="http://tajs.qq.com/stats?sId=30683215" charset="UTF-8"></script>
</div>
<!--span class="query"><?php echo get_num_queries(); ?> queries in <?php timer_stop(1); ?> seconds.</span-->
<?php
}//add_footer
add_action( 'wp_footer', 'add_footer');
//end add_footer ------------------------------------------------------
|
Reader Echoes
6 comments