Archive for the ‘WordPress’ Category
2009December . 19th

显示每篇文章的日期

WordPress日期格式
WordPress的模板非常灵活,一个优秀的模板所实现的功能往往超出你的想象。这篇文章说说在WordPress里调用文章的发布日期。

2009December . 10th

我的博客优化历程

自从换了域名,换了主题以后,我一直努力令博客的浏览体验更加好,但因此也挂载了大量的JS文件,页面的载入速度一度变得非常缓慢。于是优化就迫在眉睫了。我的优化步骤是:

1.压缩CSS和JS文件,并修改一部分插件,优化页面的载入进程

一般需要另外加载JS或者CSS的插件都会存在add_action(“wp_head”,”xxxx”)或者add_action(“wp_footer”,”xxxx”)这两句代码,目的是把自己的脚本或者样式插入到主题的wp_head()wp_footer()处,使插件可以正常工作(那些反映说插件激活了但看不到效果的人注意了,我观察到相当一部分人所使用的主题不能正常加载插件的脚本,缺的就是这两个函数了)。
下面转回正题。我们需要优化载入进程,也就是流量整形,把CSS文件移到head里(这点100%的插件都能做到,不用担心),把JS文件放在页面最后。我们可以把add_action(xxxx)这句删掉,然后手工把所需的文件插入到主题模板里。

2009November . 30th

PHP随机显示目录下的图片

前言

在不久前,推特上,@doublechou(才女,大家follow之,博客)正在做主题(现在她暂时用iNove了)。她当时想做一个随机更换背景图片的功能,用JavaScript写的话,程序流程应该是:建立一个图片数组->随机选择数组里其中一个值->生成样式并写入body标签
可是用JS做的话,有以下缺点:
1.万一浏览器禁用了JS的话就失效了,而且写代码是需要考虑兼容性。
2.维护比较麻烦,图片的位置都存放在数组里。
于是我提议用PHP处理,可是我和她对PHP都是半桶水的,一时之间也想不出怎么做。今天时运高,看到一个PHP随机显示目录下图片的源码,学习一下,并分享之。

2009November . 28th

自动在正文内容后添加内容

很多时候,你都需要在文章内容后面添加一些信息,例如订阅,文章分享,收藏和Creative Commons协议声明等。一般情况下,你可以直接编辑主题的single.php文件添加代码来达到目的。但在制作主题的时候,每个用户的需求都不同,而且你也不可能在文章下方添加太多的内容。因此让用户能自定义自己需要的内容是最好的方案。

2009November . 25th

自定义FEED地址

自定义FEED地址FEED是WEB 2.0时代一种重要的网站阅读手段。通过FEED订阅,用户不需要登录网站就可以了解到该网站上最新的更新。而且随着鲜果、Google Reader、Puto等网络阅读器的兴起,订阅喜欢的网站的FEED已经成为了网民中流行的一种阅读方式。
WordPress默认也提供FEED订阅接口,但其默认订阅地址却比较难记、难输入。WordPress的原始FEED地址有:

http://example.com/?feed=rss

http://example.com/?feed=rss2

http://example.com/?feed=rdf

http://example.com/?feed=atom

这几种,其中的区别是针对不同的协议进行不同的优化,以适应不同的订阅设备。下面用电脑上最常用的rss协议讲讲自定义WordPress FEED地址。

2009November . 9th

成为一个优秀WordPress开发者的10条秘诀

成为一个优秀的Wordpress开发者是很多WPer的梦想,这次大家来看看国外开发者Mike Smith的经验。

1.为自己的主题创建方便定制的选项面板

如果你有留意收费主题的发展趋势,你会发现带有一个方便用户个性化的选项面板已经是收费主题的标配。其实为主题做一个选项面板,无论是对主题的用户还是自己使用都是有很大的帮助的。通过一个选项面板,用户可以自定义广告,FEED订阅器等,而不必接触繁琐的代码,因此作为一个开发者,你应该知道如何去创建一个主题选项面板。
如果你还不懂得这个技术,可以阅读MG12的教程:《主题技巧: 为主题添加管理选项》

2009November . 7th

无Gzip模块主机实现Gzip压缩js和css

众所周知,开启Gzip能极大地压缩文本数据的体积,可是有的主机上并没有安装Gzip模块(例如我现在使用的主机),因此不能通过写.htaccess文件或者httpd参数等方法开启Gzip压缩功能。今天给大家介绍两个兄弟版的WordPress插件:WP CSSWP JS,作者都是Halmat Ferello

WP CSS

WP CSSWP CSS能自动清除使用@import参数载入的CSS文件里的空格,并生成缓存文件,在浏览器发出请求时以Gzip的方式输出。这样就不需要每次加载CSS的时候都进行动态压缩,降低了服务器负担。

点评

经过WP CSS压缩后的CSS文件经本人测试在IE系列兼容性较差的浏览器上都能正常解析。但在进行W3C CSS在线验证时会产生未知错误(当然,CSS还是符合W3C标准的),原因未明。

2009November . 6th

为主题添加AJAX提交评论功能

本文参考了Xiaorsz的使用 jQuery 实现 wordpress 的 Ajax 留言
前几天,为了提高评论体验,为了减轻服务器负担,我为主题添加了AJAX提交评论功能,现在分享一下方法。
首先需要在主题的function.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
< ?php
function fail($s) {
    header('HTTP/1.0 500 Internal Server Error');
    echo $s;
    exit;
}
 
function ajax_comment(){
 
if($_POST['action'] == 'ajax_comment') {
 
    global $wpdb, $db_check;
        // Check DB
        if(!$wpdb->dbh) {
            echo('Our database has issues. Try again later.');
			die();
        } 
nocache_headers();
$comment_post_ID = (int) $_POST['comment_post_ID'];
 
$status = $wpdb->get_row("SELECT post_status, comment_status FROM $wpdb->posts WHERE ID = '$comment_post_ID'");
if ( empty($status->comment_status) ) {
    do_action('comment_id_not_found', $comment_post_ID);
    fail('The post you are trying to comment on does not currently exist in the database.');
} elseif ( 'closed' ==  $status->comment_status ) {
    do_action('comment_closed', $comment_post_ID);
    fail('Sorry, comments are closed for this item.');
} elseif ( in_array($status->post_status, array('draft', 'pending') ) ) {
    do_action('comment_on_draft', $comment_post_ID);
    fail('The post you are trying to comment on has not been published.');
}
$comment_author       = trim(strip_tags($_POST['author']));
$comment_author_email = trim($_POST['email']);
$comment_author_url   = trim($_POST['url']);
$comment_content      = trim($_POST['comment']);
// If the user is logged in
$user = wp_get_current_user();
if ( $user->ID ) {
    $comment_author       = $wpdb->escape($user->display_name);
    $comment_author_email = $wpdb->escape($user->user_email);
    $comment_author_url   = $wpdb->escape($user->user_url);
    if ( current_user_can('unfiltered_html') ) {
        if ( wp_create_nonce('unfiltered-html-comment_' . $comment_post_ID) != $_POST['_wp_unfiltered_html_comment'] ) {
            kses_remove_filters(); // start with a clean slate
            kses_init_filters(); // set up the filters
        }
    }
} else {
    if ( get_option('comment_registration') )
        fail('Sorry, you must be logged in to post a comment.');
}
$comment_type = '';
if ( get_option('require_name_email') && !$user->ID ) {
    if ( 6> strlen($comment_author_email) || '' == $comment_author )
        fail('Sorry: please fill the required fields (name, email).');
    elseif ( !is_email($comment_author_email))
        fail('Sorry: please enter a valid email address.');
}
if ( '' == $comment_content )
    fail('Sorry: please type a comment.');
// Simple duplicate check
$dupe = "SELECT comment_ID FROM $wpdb->comments WHERE comment_post_ID = '$comment_post_ID' AND ( comment_author = '$comment_author' ";
if ( $comment_author_email ) $dupe .= "OR comment_author_email = '$comment_author_email' ";
$dupe .= ") AND comment_content = '$comment_content' LIMIT 1";
if ( $wpdb->get_var($dupe) ) {
    fail('Duplicate comment detected; it looks as though you\'ve already said that!');
}
$commentdata = compact('comment_post_ID', 'comment_author', 'comment_author_email', 'comment_author_url', 'comment_content', 'comment_type', 'user_ID');
if( !$user->ID ){
	$result_set = $wpdb->get_results("SELECT display_name, user_email FROM $wpdb->users WHERE display_name = '" . $comment_author . "' OR user_email = '" . $comment_author_email . "'");
	if ($result_set) {
	if ($result_set[0]->display_name == $comment_author){
	fail( __('Error: you are not allowed to use the nickname that you entered.if you are the administrator you hava to login to comment.','philna2') );
	} else {
	fail( __('Error: you are not allowed to use the email that you entered.if you are the administrator you hava to login to comment.','philna2') );
	}
	}
}
$comment_id = wp_new_comment( $commentdata );
$comment = get_comment($comment_id);
 
if( !$user->ID ){
	setcookie('comment_author_' . COOKIEHASH, $comment->comment_author, time() + 30000000, COOKIEPATH, COOKIE_DOMAIN);
	setcookie('comment_author_email_' . COOKIEHASH, $comment->comment_author_email, time() + 30000000, COOKIEPATH, COOKIE_DOMAIN);
	setcookie('comment_author_url_' . COOKIEHASH, clean_url($comment->comment_author_url), time() + 30000000, COOKIEPATH, COOKIE_DOMAIN);
}
@header('Content-type: ' . get_option('html_type') . '; charset=' . get_option('blog_charset'));
?>
	//这里需要粘贴你的评论框架代码,不过相关的调用代码有所变化:
	//评论ID:$comment->comment_ID
	//评论者名字:$comment->comment_author
	//判断评论者是否填写了网站地址:$comment->get_comment_author_url
	//评论者URL:$comment->comment_author_url
	//评论时间:mysql2date(__('F jS, Y'),$comment->comment_date)
	//评论者e-mail:$comment->comment_author_email
	//评论内容$comment->comment_content
< ?php
	die();
}
}
add_action('init', 'ajax_comment');
//添加AJAX评论钩子
?>
2009October . 31st

WordPress相册插件:Overlay4WP

WordPress相册插件:Overlay4WP

Overlay4WP是Bolo发布的第一个WordPress插件。在Highslide4WP的基础上修改,加入的后台选项,使用了效果强大的jQuery库,可以自定义多种特效。与其它同类型的图片特效插件相比,Overlay4WP具有以下几个优点。

2009October . 25th

jQuery 多级动感展开菜单

上次介绍了JavaScript打造多级下拉菜单,这次利用jQuery怎么做一个多级下拉菜单。
jQuery 多级动感展开菜单

Subscriber selector

Close