本文的目标读者是WordPress 主题开发者,需要懂一些基本的PHP知识。另外,下文提到的所有代码都必须添加到functions. php文件里面。
WordPress主题一般有一系列的php文件和一个style. css文件,而其中功能最为强大的文件则是functions. php。WordPress 有非常多的常用函数,你可以通过添加和删除一些函数来增加WordPress主题的功能,而不需要修改任何的主题文件。
1,添加Google Analytics 统计
只需要把下面的代码添加到functions. php文件里面——注意把里面的中文部分替换成你的Google 统计代码,然后你就不用担心了。
1. <?php
2. add_action('wp_footer', 'add_googleanalytics');
3. function add_googleanalytics() { ?>
4. // 把Google 统计代码复制到这里
5. <?php } ?>
2,给WordPress 博客添加一个 Favicon 图标。
每一个博客都应该有一个独一无二的标志,你可以通过添加代码到header.php来实现。当然,你也可以通过添加代码到functions.php来实现。添加完下面的代码后,只需要把Favicon.ico文件上传到网站根目录即可。
1. // add a favicon to your
2. function blog_favicon() {
3. echo '<link rel="Shortcut Icon" type="image/x-icon" href="'.get_bloginfo('wpurl').'/favicon.ico" />';
4. }
5. add_action('wp_head', 'blog_favicon');
3,移除WordPress版本号。
WordPress有新版本出来后,总会在后台提示管理员进行升级。但假如你是给客户制作网站,而他们又不想升级的话,最好的办法就是从WordPress 头部、RSS里面以及其他任何地方移除版本的信息。
1. function wpbeginner_remove_version() {
2. return '';
3. }
4. add_filter('the_generator', 'wpbeginner_remove_version');
4,给WordPress控制面板添加自定义logo
用WordPress给客户制作网站,如果给WordPress的控制面板后台添加一个自定义logo,则会让网站显的专业很多。要做到这一点,你只需要把代码添加到functions.php即可。
1. //hook the administrative header output
2. add_action('admin_head', 'my_custom_logo');
3.
4. function my_custom_logo() {
5. echo '
6. <style type="text/css">
7. #header-logo { background-image: url('.get_bloginfo('template_directory').'/images/custom-logo.gif) !important; }
8. </style>
9. ';
10. }
5,改变WordPress后台控制面板底部信息
如上所述,如果不想让客户知道网站是由WordPress制作,则可以修改WordPress控制面板底部的信息,只需要把下面的代码添加到 functions.php文件即可。
1. function remove_footer_admin () {
2. echo 'Fueled by <a href="http://www.wordpress.org"target="_blank">WordPress</a> | Designed by <a href="http://www.uzzz.net"target="_blank">Uzzz Productions</a> | WordPress Tutorials: <a href="http://www.wpbeginner.com"target="_blank">WPBeginner</a></p>';
3. }
4.
5. add_filter('admin_footer_text', 'remove_footer_admin');
注:代码里面的html部分可以修改。
6,自定义WordPress控制面板模块
一些WordPress插件会在控制面板那里添加一些模块来显示相应的信息,作为一个WordPress模板设计者,你也可以通过修改functions.php文件来实现这个功能。注意替换里面的相应信息。
1. add_action('wp_dashboard_setup', 'my_custom_dashboard_widgets');
2.
3. function my_custom_dashboard_widgets() {
4. global $wp_meta_boxes;
5.
6. wp_add_dashboard_widget('custom_help_widget', 'Theme Support', 'custom_dashboard_help');
7. }
8.
9. function custom_dashboard_help() {
10. echo'<p>Welcome to Custom Blog Theme! Need help? Contact the developer <a href="mailto:yourusername@gmail.com">here</a>. For WordPress Tutorials visit: <a href="http://www.wpbeginner.com"target="_blank">WPBeginner</a></p>';
11. }
7,改变默认的 Gravatar 头像
WordPress程序默认的 Gravatar 头像很不咋地,而且到处都是千篇一律的默认头像一点都无法体现独特性。你可以把以下代码添加到functions.php文件里面,然后记得把自定义的 Gravatar 头像上传到WordPress模板的images文件夹。
1. add_filter( 'avatar_defaults', 'newgravatar' );
2.
3. function newgravatar ($avatar_defaults) {
4. $myavatar = get_bloginfo('template_directory') . '/images/gravatar.gif';
5. $avatar_defaults[$myavatar] = "WPBeginner";
6. return $avatar_defaults;
7. }
8,让WordPress底部的版权时间显示的更生动
很多网站的版权时间都显示的是建站时的年份,有些则是显示当下的年份。事实上,这两种方式都不是太好。
最好的方式是显示从网站建设之初的年份到目前的年份位置,类似© 2006 – 2010这种显示方式。
这种效果通过以下的代码可以实现。添加完下面的代码后,系统会自动抓取发布第一篇文章的年份以及最新一篇文章的年份,并把它显示出来。
1. function comicpress_copyright() {
2. global $wpdb;
3. $copyright_dates = $wpdb->get_results("
4. SELECT
5. YEAR(min(post_date_gmt)) AS firstdate,
6. YEAR(max(post_date_gmt)) AS lastdate
7. FROM
8. $wpdb->posts
9. WHERE
10. post_status = 'publish'
11. ");
12. $output = '';
13. if($copyright_dates) {
14. $copyright = "© " . $copyright_dates[0]->firstdate;
15. if($copyright_dates[0]->firstdate != $copyright_dates[0]->lastdate) {
16. $copyright .= '-' . $copyright_dates[0]->lastdate;
17. }
18. $output = $copyright;
19. }
20. return $output;
21. }
把上面的代码添加到了functions.php文件里面后,还需要在 footer.php 任何你想显示版权时间的地方加上如下代码:
1. <?php echo comicpress_copyright(); ?>
9,给读者投稿文章添加姓名/来源
如果你的博客接受读者的投稿,想在该篇文章出现投稿者的姓名,同时又不想通过添加作者的这种繁琐而麻烦的方式来操作,则可以使用下面的代码。使用下面的代码后,只需要在撰写文章的时候在自定义区域填上投稿者的姓名即可。系统会自动将发布者的名称换成投稿者的名称。
这个代码对接受读者投稿较多的网站,或者是资讯型的网站非常有用(利用它来显示来源)。
1. add_filter( 'the_author', 'guest_author_name' );
2. add_filter( 'get_the_author_display_name', 'guest_author_name' );
3.
4. function guest_author_name( $name ) {
5. global $post;
6.
7. $author = get_post_meta( $post->ID, 'guest-author', true );
8.
9. if ( $author )
10. $name = $author;
11.
12. return $name;
13. }
10,启用文章缩略图功能
从WordPress2.9版本开始,可以给模板添加文章缩略图功能。操作方法很简单,只需要把下面的代码添加到functions.php里面。
1. add_theme_support( 'post-thumbnails' );
然后在要显示缩略图的地方放置下面的代码即可。
1. <?php the_post_thumbnail(); ?>
11,自定义WordPress 3.0 版本导航栏
WordPress 3.0 增加了一个功能,可以让WordPress模板开发者自定义导航菜单。如果你想给用户一个导航栏的选择权,只需要把下面的代码加入到 functions.php 文件里面。
1. add_theme_support( 'nav-menus' );
之后把下面的代码复制到你想出新的地方:
1. <?php wp_nav_menu( array( 'sort_column' => 'menu_order', 'container_class' => 'menu-header' ) ); ?>
12,移除WordPress默认的个人资料选项
如果你客户的想让用户可以自行添加个人资料,那么需要让这个选项更简单。其中一个方法就是移除部分选项,AIM, Yahoo IM 和 Jabber 之类的东东。
1. add_filter('user_contactmethods','hide_profile_fields',10,1);
2.
3. function hide_profile_fields( $contactmethods ) {
4. unset($contactmethods['aim']);
5. unset($contactmethods['jabber']);
6. unset($contactmethods['yim']);
7. return $contactmethods;
8. }
13,添加作者个人资料选项
如果你想更充分的展示作者的个人资料,那么你可以添加一些更个性化的资料选项,例如添加twitter 和 facebook账号等。下面的代码就是添加twitter 和 facebook账号用的。当然,你可以把里面的内容替换成其他任何你想展示的资料。这个对多博客作者尤其有用。
1. function my_new_contactmethods( $contactmethods ) {
2. // Add Twitter
3. $contactmethods['twitter'] = 'Twitter';
4. //add Facebook
5. $contactmethods['facebook'] = 'Facebook';
6.
7. return $contactmethods;
8. }
9. add_filter('user_contactmethods','my_new_contactmethods',10,1);
添加完是上面的代码后,你需要在author.php文件里面添加如下的代码:
1. <?php echo $curauth->twitter; ?>
注意:改代码仅在WordPress2.9以上的版本起作用。
14,添加侧边栏小模块。
这是目前用的最多的技巧之一,很多WordPress模板开发者都已经知道,并且在用了。
1. if ( function_exists('register_sidebar') )
2. register_sidebar(array('name'=>'MiddleSidebar',
3. 'before_widget' => '<li class="widget">',
4. 'after_widget' => '</li>',
5. 'before_title' => '<h2 class="widgettitle">',
6. 'after_title' => '</h3>',
7. ));
8. register_sidebar(array('name'=>'FooterSidebar',
9. 'before_widget' => '<li class="widget">',
10. 'after_widget' => '</li>',
11. 'before_title' => '<h2 class="widgettitle">',
12. 'after_title' => '</h3>',
13. ));
上面的代码可以增加两个侧边栏的小模块。以此类推,你可以添加无限多侧边栏的小模块。添加完上面的代码后,你需要把下面的代码添加到你要出现这边小模块的地方。
1. <?php if ( !function_exists('dynamic_sidebar') || !dynamic_sidebar('MiddleSidebar') ) : ?>
2. <!–Default sidebar info goes here–>
3. <?php endif; ?>
注意:侧边栏并不一定需要出现在sidebar.php文件里面。
15,优化Wordpress 博客的RSS
如何在RSS里面加入版权链接?如何在RSS加入广告?针对国内互联网的现状,在RSS里面加入版权尤为重要,广告倒是次要的。
除了插件(Better Feed)以外,可以采用以下的方法来实现。
1. function wpbeginner_postrss($content) {
2. if(is_feed()){
3. $content = 'This post was written by Syed Balkhi '.$content.'Check out WPBeginner';
4. }
5. return $content;
6. }
7. add_filter('the_excerpt_rss', 'wpbeginner_postrss');
8. add_filter('the_content', 'wpbeginner_postrss');
16,给RSS添加缩略图
缩略图一般是在正常的博客页面上用来起到美化界面的作用。当然,如果需要的话,也可以给RSS内容增加一个缩略图。要做到这一点,只需要在functions.php 里面加入如下代码:
1. function rss_post_thumbnail($content) {
2. global $post;
3. if(has_post_thumbnail($post->ID)) {
4. $content = '<p>' . get_the_post_thumbnail($post->ID) .
5. '</p>' . get_the_content();
6. }
7. return $content;
8. }
9. add_filter('the_excerpt_rss', 'rss_post_thumbnail');
10. add_filter('the_content_feed', 'rss_post_thumbnail');
17,开启WordPress评论嵌套功能。
评论嵌套功能是WordPress自身带有的最好功能之一,只可惜很多WordPress模板都不支持。很多文章都有提到过修改的方法,但一般都涉 及到修改comments文件和header文件。事实上,通过修改functions.php文件来修改是最简便的,而且一劳永逸。
1. // enable threaded comments
2. function enable_threaded_comments(){
3. if (!is_admin()) {
4. if (is_singular() AND comments_open() AND (get_option('thread_comments') == 1))
5. wp_enqueue_script('comment-reply');
6. }
7. }
8. add_action('get_header', 'enable_threaded_comments');
18,移除WordPress登陆面板的错误提示
当你输入的密码或者用户名错误的时候,WordPress登陆界面会给出相应的提示。但如果碰到黑客的话,这些提示反而给了他们更好的提示,让他们更容易破解用户名和密码。因此,处于安全性考虑,移除WordPress登陆面板的错误提示是非常必要的。
1. add_filter('login_errors',create_function('$a', "return null;"));
19,关闭WordPress的搜索功能
当把WordPress当做CMS系统来使用的时候,WordPress自带的搜索功能实用性就不是太强了。一来增加数据库查询次数,二来Google 自定义搜索会是更好的替代。因此,你只需要通过以下的代码就可以关闭WordPress的搜索功能。
1. function fb_filter_query( $query, $error = true ) {
2.
3. if ( is_search() ) {
4. $query->is_search = false;
5. $query->query_vars[s] = false;
6. $query->query[s] = false;
7.
8. // to error
9. if ( $error == true )
10. $query->is_404 = true;
11. }
12. }
13.
14. add_action( 'parse_query', 'fb_filter_query' );
15. add_filter( 'get_search_form', create_function( '$a', "return null;" ) );
20,启用WordPress简码功能
Google AdSense 算是博客的标配之一了,很多CMS经常会在模板选项里面预置Google AdSense的广告位。假如你的模板不支持,你可以通过以下的方法来解决:
1. function showads() {
2. return '<div id="adsense"><script type="text/javascript"><!–
3. google_ad_client = "pub-XXXXXXXXXXXXXX";
4. google_ad_slot = "4668915978";
5. google_ad_width = 468;
6. google_ad_height = 60;
7. //–>
8. </script>
9.
10. <script type="text/javascript"
11. src="http://pagead2.googlesyndication.com/pagead/show_ads.js">
12. </script></div>';
13. }
14.
15. add_shortcode('adsense', 'showads');
21,不通过.htaccess将rss地址唯一化
WordPress本身提供好几个不同版本的rss地址,加入你又使用了FeedBurner或者feedsky的话,RSS地址就会更多。太多的RSS容易分流订阅客户,而且也不利于品牌推广。
一般的修改方法是通过更改.htaccess来进行,此外,还可以通过以下的代码来实现。
1. function custom_feed_link($output, $feed) {
2.
3. $feed_url = 'http://feeds.feedburner.com/wpbeginner';
4.
5. $feed_array = array('rss' => $feed_url, 'rss2' => $feed_url, 'atom' => $feed_url, 'rdf' =>$feed_url, 'comments_rss2' => '');
6. $feed_array[$feed] = $feed_url;
7. $output = $feed_array[$feed];
8.
9. return $output;
10. }
11.
12. function other_feed_links($link) {
13.
14. $link = 'http://feeds.feedburner.com/wpbeginner';
15. return $link;
16.
17. }
18. //Add our functions to the specific filters
19. add_filter('feed_link','custom_feed_link', 1, 2);
20. add_filter('category_feed_link', 'other_feed_links');
21. add_filter('author_feed_link', 'other_feed_links');
22. add_filter('tag_feed_link','other_feed_links');
23. add_filter('search_feed_link','other_feed_links');
22,启用paypal 捐赠简码
当你写完一篇以后,可以在文章里面插入paypal 捐赠按钮,方便读者捐赠。以下的代码可以让你非常轻松的做到这一点。
1. function donate_shortcode( $atts ) {
2. extract(shortcode_atts(array(
3. 'text' => 'Make a donation',
4. 'account' => 'REPLACE ME',
5. 'for' => '',
6. ), $atts));
7.
8. global $post;
9.
10. if (!$for) $for = str_replace(" "," ",$post->post_title);
11.
12. return '<a class="donateLink" href="https://www.paypal.com/cgi-bin/webscr?cmd=_xclick&business='.$account.'&item_name=Donation for '.$for.'">'.$text.'</a>';
13.
14. }
15. add_shortcode('donate', 'donate_shortcode');
23,设定文章从发布到出现在RSS中的时间长短
通过RSS订阅来阅读博文的朋友可能都会有这个体验:经常发现RSS中的文字或者细节有错误,而返回到页面的时候却发现错误已经没有了。这种情况最有可能是因为
RSS最大的好处是快捷、直接,但这个最大的好处有时候对作者来说却会引发某些尴尬。所以,有时候有必要让文章发布后到读者从RSS中按到有一个小小的时间差,方便作者排查某些问题。以下的代码可以做到以下几点:
1. function publish_later_on_feed($where) {
2. global $wpdb;
3.
4. if ( is_feed() ) {
5. // timestamp in WP-format
6. $now = gmdate(‘Y-m-d H:i:s’);
7.
8. // value for wait; + device
9. $wait = ‘10′; // integer
10.
11. // http://dev.mysql.com/doc/refman/5.0/en/date-and-time-functions.html#function_timestampdiff
12. $device = ‘MINUTE’; //MINUTE, HOUR, DAY, WEEK, MONTH, YEAR
13.
14. // add SQL-sytax to default $where
15. $where .= ” AND TIMESTAMPDIFF($device, $wpdb->posts.post_date_gmt, ‘$now’) > $wait “;
16. }
17. return $where;
18. }
19.
20. add_filter(‘posts_where’, ‘publish_later_on_feed’);
这段代码设置的时间是10分钟,你可以把10改成任何你想要的时间。
24,自定义摘要输出时的符号
一般设定自动摘要输出,你会经常在WordPress博客的首页看到“[...]”这样的符号。为了界面的美观,或者是个性化的需要,你可以把这个默认的符号改变为其他的符号。而以下的代码就是为了实现这个而写:
1. // custom excerpt ellipses for 2.9
2. function custom_excerpt_more($more) {
3. return '…';
4. }
5. add_filter('excerpt_more', 'custom_excerpt_more');
6.
7. /* custom excerpt ellipses for 2.8-
8. function custom_excerpt_more($excerpt) {
9. return str_replace('[...]', '…', $excerpt);
10. }
11. add_filter('wp_trim_excerpt', 'custom_excerpt_more');
12. */
25,自定义摘要输出的文字长度
假如你比较懒,不想在撰写文章的时候每篇文章都输入摘要,就可以让系统自动截取一定长度的文字来作为摘要输出。下面的代码默认是100个字节,也就是50个汉字。你可以把数值修改成符合你需要的数字。
1. function new_excerpt_length($length) {
2. return 100;
3. }
4. add_filter('excerpt_length', 'new_excerpt_length');
26,显示精确评论数
WordPress默认是把trackbacks 和 pings 都算作评论的,因此当你设置不显示trackbacks 和 ping的时候,评论数看起来总是不对头。以下的代码则以让WordPress只计算评论的数量,而不把trackbacks 和 pings也计算进去。
1. add_filter('get_comments_number', 'comment_count', 0);
2. function comment_count( $count ) {
3. if ( ! is_admin() ) {
4. global $id;
5. $comments_by_type = &separate_comments(get_comments('status=approve&post_id=' . $id));
6. return count($comments_by_type['comment']);
7. } else {
8. return $count;
9. }
10. }
27,取消RSS输出
对于某些博客而言,或者因为被太多人采集了,或者因为不想让别人通过RSS订阅,想取消RSS输出。WordPress默认是没有这个功能的,但你可以通过以下的代码来取消RSS输出。
1. function fb_disable_feed() {
2. wp_die( __('No feed available,please visit our <a href="'. get_bloginfo('url') .'">homepage</a>!') );
3. }
4.
5. add_action('do_feed', 'fb_disable_feed', 1);
6. add_action('do_feed_rdf', 'fb_disable_feed', 1);
7. add_action('do_feed_rss', 'fb_disable_feed', 1);
8. add_action('do_feed_rss2', 'fb_disable_feed', 1);
9. add_action('do_feed_atom', 'fb_disable_feed', 1);
28,显示Twitter 的订阅数以及其他资料
Twitter系统以及很多第三方的客户端都可以让你在WordPress博客的侧边栏暂时Twitter的订阅数以及一些其他的资料。这种做法往往很多时候都没办法跟博客已有的界面结合的很好。而以下的代码则可以让你自定义Twitter 在博客上的显示外观。
1. function rarst_twitter_user( $username, $field, $display = false ) {
2. $interval = 3600;
3. $cache = get_option('rarst_twitter_user');
4. $url = 'http://api.twitter.com/1/users/show.json?screen_name='.urlencode($username);
5.
6. if ( false == $cache )
7. $cache = array();
8.
9. // if first time request add placeholder and force update
10. if ( !isset( $cache[$username][$field] ) ) {
11. $cache[$username][$field] = NULL;
12. $cache[$username]['lastcheck'] = 0;
13. }
14.
15. // if outdated
16. if( $cache[$username]['lastcheck'] < (time()-$interval) ) {
17.
18. // holds decoded JSON data in memory
19. static $memorycache;
20.
21. if ( isset($memorycache[$username]) ) {
22. $data = $memorycache[$username];
23. }
24. else {
25. $result = wp_remote_retrieve_body(wp_remote_request($url));
26. $data = json_decode( $result );
27. if ( is_object($data) )
28. $memorycache[$username] = $data;
29. }
30.
31. if ( is_object($data) ) {
32. // update all fields, known to be requested
33. foreach ($cache[$username] as $key => $value)
34. if( isset($data->$key) )
35. $cache[$username][$key] = $data->$key;
36.
37. $cache[$username]['lastcheck'] = time();
38. }
39. else {
40. $cache[$username]['lastcheck'] = time()+60;
41. }
42.
43. update_option( 'rarst_twitter_user', $cache );
44. }
45.
46. if ( false != $display )
47. echo $cache[$username][$field];
48. return $cache[$username][$field];
49. }
把上面的代码复制到 functions.php后,再把下面代码复制到你想出现的地方即可。
Then place the following code where you want to display the count in your theme file:
1. echo rarst_twitter_user('wpbeginner', 'name').' has '.
2. rarst_twitter_user('wpbeginner', 'followers_count').' followers after '.
3. rarst_twitter_user('wpbeginner', 'statuses_count').' updates.';
29,彩色标签云
把代码复制到 functions.php后,原本单色的标签云,会变成多彩的.
1. //彩色标签云
2. function colorCloud($text) {
3. $text = preg_replace_callback('|<a (.+?)>|i', 'colorCloudCallback', $text);
4. return $text;
5. }
6. function colorCloudCallback($matches) {
7. $text = $matches[1];
8. $color = dechex(rand(0,16777215));
9. $pattern = '/style=(\'|\")(.*)(\'|\")/i';
10. $text = preg_replace($pattern, "style=\"color:#{$color};$2;\"", $text);
11. return "<a $text>";
12. }
13. add_filter('wp_tag_cloud', 'colorCloud', 1);
30,评论回应邮件通知
一般该功能都通过插件实现,把代码复制到 functions.php后,会在评论部分自动插入可选择的评论回应邮件通知功能.
1. // 评论回应邮件通知
2. function comment_mail_notify($comment_id) {
3. $admin_notify = '1'; // admin 要不要收回复通知 ( '1'=要 ; '0'=不要 )
4. $admin_email = get_bloginfo ('admin_email'); // $admin_email 可改为你指定的 e-mail.
5. $comment = get_comment($comment_id);
6. $comment_author_email = trim($comment->comment_author_email);
7. $parent_id = $comment->comment_parent ? $comment->comment_parent : '';
8. global $wpdb;
9. if ($wpdb->query("Describe {$wpdb->comments} comment_mail_notify") == '')
10. $wpdb->query("ALTER TABLE {$wpdb->comments} ADD COLUMN comment_mail_notify TINYINT NOT NULL DEFAULT 0;");
11. if (($comment_author_email != $admin_email&& isset($_POST['comment_mail_notify'])) || ($comment_author_email == $admin_email &&$admin_notify == '1'))
12. $wpdb->query("UPDATE {$wpdb->comments} SET comment_mail_notify='1' WHERE comment_ID='$comment_id'");
13. $notify = $parent_id ? get_comment($parent_id)->comment_mail_notify : '0';
14. $spam_confirmed = $comment->comment_approved;
15. if ($parent_id != '' && $spam_confirmed != 'spam' && $notify == '1') {
16. $wp_email = 'no-reply@' . preg_replace('#^www\.#', '', strtolower($_SERVER['SERVER_NAME'])); // e-mail 发出点, no-reply 可改为可用的 e-mail.
17. $to = trim(get_comment($parent_id)->comment_author_email);
18. $subject = '您在 [' . get_option("blogname") . '] 的留言有了回应';
19. $message = '
20. <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;">
21. <p>' . trim(get_comment($parent_id)->comment_author) . ', 您好!</p>
22. <p>您曾在《' . get_the_title($comment->comment_post_ID) . '》的留言:<br />'
23. . trim(get_comment($parent_id)->comment_content) . '</p>
24. <p>' . trim($comment->comment_author) . ' 给您的回应:<br />'
25. . trim($comment->comment_content) . '<br /></p>
26. <p>您可以点击 <a href="' . htmlspecialchars(get_comment_link($parent_id)) . '">查看回应完整內容</a></p>
27. <p>欢迎您再度光临 <a href="' . get_option('home') . '">' . get_option('blogname') . '</a></p>
28. <p>(此邮件由系统自动发出,请勿回复.)</p>
29. </div>';
30. $from = "From: \"" . get_option('blogname') . "\" <$wp_email>";
31. $headers = "$from\nContent-Type: text/html; charset=" . get_option('blog_charset') . "\n";
32. wp_mail( $to, $subject, $message, $headers );
33. //echo 'mail to ', $to, '<br/> ' , $subject, $message; // for testing
34. }
35. }
36. add_action('comment_post', 'comment_mail_notify');
37.
38. // 自动勾选
39. function add_checkbox() {
40. echo '<input type="checkbox" name="comment_mail_notify" id="comment_mail_notify"value="comment_mail_notify" checked="checked" style="margin-left:0px;" /><labelfor="comment_mail_notify">有人回复时邮件通知我</label>';
41. }
42. add_action('comment_form', 'add_checkbox');