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