來(lái)源于:
http://blog.pureisle.net/archives/378.html
http://zmingcx.com/wordpress-theme-very-useful-development-techniques.html
本文的目標(biāo)讀者是WordPress 主題開(kāi)發(fā)者,需要懂一些基本的PHP知識(shí)。另外,下文提到的所有代碼都必須添加到functions. php文件里面。
WordPress主題一般有一系列的php文件和一個(gè)style. css文件,而其中功能最為強(qiáng)大的文件則是functions. php。WordPress 有非常多的常用函數(shù),你可以通過(guò)添加和刪除一些函數(shù)來(lái)增加WordPress主題的功能,而不需要修改任何的主題文件。
1,添加Google Analytics 統(tǒng)計(jì)
只需要把下面的代碼添加到functions. php文件里面——注意把里面的中文部分替換成你的Google 統(tǒng)計(jì)代碼,然后你就不用擔(dān)心了。
<?php
add_action('wp_footer',?'add_googleanalytics');
function add_googleanalytics()?{??>
//?把Google?統(tǒng)計(jì)代碼復(fù)制到這里
<?php?}??>
2,給WordPress 博客添加一個(gè) Favicon 圖標(biāo)。
每一個(gè)博客都應(yīng)該有一個(gè)獨(dú)一無(wú)二的標(biāo)志,你可以通過(guò)添加代碼到header.php來(lái)實(shí)現(xiàn)。當(dāng)然,你也可以通過(guò)添加代碼到functions.php來(lái)實(shí)現(xiàn)。添加完下面的代碼后,只需要把Favicon.ico文件上傳到網(wǎng)站根目錄即可。
//?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版本號(hào)。
WordPress有新版本出來(lái)后,總會(huì)在后臺(tái)提示管理員進(jìn)行升級(jí)。但假如你是給客戶(hù)制作網(wǎng)站,而他們又不想升級(jí)的話,最好的辦法就是從WordPress 頭部、RSS里面以及其他任何地方移除版本的信息。
function wpbeginner_remove_version()?{
return '';
}
add_filter('the_generator',?'wpbeginner_remove_version');
4,給WordPress控制面板添加自定義logo
用WordPress給客戶(hù)制作網(wǎng)站,如果給WordPress的控制面板后臺(tái)添加一個(gè)自定義logo,則會(huì)讓網(wǎng)站顯的專(zhuān)業(yè)很多。要做到這一點(diǎn),你只需要把代碼添加到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后臺(tái)控制面板底部信息
如上所述,如果不想讓客戶(hù)知道網(wǎng)站是由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插件會(huì)在控制面板那里添加一些模塊來(lái)顯示相應(yīng)的信息,作為一個(gè)WordPress模板設(shè)計(jì)者,你也可以通過(guò)修改functions.php文件來(lái)實(shí)現(xiàn)這個(gè)功能。注意替換里面的相應(yīng)信息。
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? target="_blank">WPBeginner</a></p>';
}
7,改變默認(rèn)的 Gravatar 頭像
WordPress程序默認(rèn)的 Gravatar 頭像很不咋地,而且到處都是千篇一律的默認(rèn)頭像一點(diǎn)都無(wú)法體現(xiàn)獨(dú)特性。你可以把以下代碼添加到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,讓W(xué)ordPress底部的版權(quán)時(shí)間顯示的更生動(dòng)
很多網(wǎng)站的版權(quán)時(shí)間都顯示的是建站時(shí)的年份,有些則是顯示當(dāng)下的年份。事實(shí)上,這兩種方式都不是太好。
最好的方式是顯示從網(wǎng)站建設(shè)之初的年份到目前的年份位置,類(lèi)似? 2006 – 2010這種顯示方式。
這種效果通過(guò)以下的代碼可以實(shí)現(xiàn)。添加完下面的代碼后,系統(tǒng)會(huì)自動(dòng)抓取發(fā)布第一篇文章的年份以及最新一篇文章的年份,并把它顯示出來(lái)。
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 任何你想顯示版權(quán)時(shí)間的地方加上如下代碼:
<?php echo comicpress_copyright();??>
9,給讀者投稿文章添加姓名/來(lái)源
如果你的博客接受讀者的投稿,想在該篇文章出現(xiàn)投稿者的姓名,同時(shí)又不想通過(guò)添加作者的這種繁瑣而麻煩的方式來(lái)操作,則可以使用下面的代碼。使用下面的代碼后,只需要在撰寫(xiě)文章的時(shí)候在自定義區(qū)域填上投稿者的姓名即可。系統(tǒng)會(huì)自動(dòng)將發(fā)布者的名稱(chēng)換成投稿者的名稱(chēng)。
這個(gè)代碼對(duì)接受讀者投稿較多的網(wǎng)站,或者是資訊型的網(wǎng)站非常有用(利用它來(lái)顯示來(lái)源)。
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版本開(kāi)始,可以給模板添加文章縮略圖功能。操作方法很簡(jiǎn)單,只需要把下面的代碼添加到functions.php里面。
add_theme_support(?'post-thumbnails'?);
然后在要顯示縮略圖的地方放置下面的代碼即可。
<?php?the_post_thumbnail();??>
11,自定義WordPress 3.0 版本導(dǎo)航欄
WordPress 3.0 增加了一個(gè)功能,可以讓W(xué)ordPress模板開(kāi)發(fā)者自定義導(dǎo)航菜單。如果你想給用戶(hù)一個(gè)導(dǎo)航欄的選擇權(quán),只需要把下面的代碼加入到 functions.php 文件里面。
add_theme_support(?'nav-menus'?);
之后把下面的代碼復(fù)制到你想出新的地方:
<?php?wp_nav_menu( array(?'sort_column'?=>?'menu_order',?'container_class'?=>?'menu-header'?)?);??>
12,移除WordPress默認(rèn)的個(gè)人資料選項(xiàng)
如果你客戶(hù)的想讓用戶(hù)可以自行添加個(gè)人資料,那么需要讓這個(gè)選項(xiàng)更簡(jiǎn)單。其中一個(gè)方法就是移除部分選項(xiàng),AIM, Yahoo IM 和 Jabber 之類(lèi)的東東。
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,添加作者個(gè)人資料選項(xiàng)
如果你想更充分的展示作者的個(gè)人資料,那么你可以添加一些更個(gè)性化的資料選項(xiàng),例如添加twitter 和 facebook賬號(hào)等。下面的代碼就是添加twitter 和 facebook賬號(hào)用的。當(dāng)然,你可以把里面的內(nèi)容替換成其他任何你想展示的資料。這個(gè)對(duì)多博客作者尤其有用。
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,添加側(cè)邊欄小模塊。
這是目前用的最多的技巧之一,很多WordPress模板開(kāi)發(fā)者都已經(jīng)知道,并且在用了。
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>',
));
上面的代碼可以增加兩個(gè)側(cè)邊欄的小模塊。以此類(lèi)推,你可以添加無(wú)限多側(cè)邊欄的小模塊。添加完上面的代碼后,你需要把下面的代碼添加到你要出現(xiàn)這邊小模塊的地方。
<?php if (?!function_exists('dynamic_sidebar')?||?!dynamic_sidebar('MiddleSidebar')?)?:??>
<!–Default?sidebar?info?goes?here–>
<?php endif;??>
注意:側(cè)邊欄并不一定需要出現(xiàn)在sidebar.php文件里面。
15,優(yōu)化Wordpress 博客的RSS
如何在RSS里面加入版權(quán)鏈接?如何在RSS加入廣告?針對(duì)國(guó)內(nèi)互聯(lián)網(wǎng)的現(xiàn)狀,在RSS里面加入版權(quán)尤為重要,廣告倒是次要的。
除了插件(Better Feed)以外,可以采用以下的方法來(lái)實(shí)現(xiàn)。
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添加縮略圖
縮略圖一般是在正常的博客頁(yè)面上用來(lái)起到美化界面的作用。當(dāng)然,如果需要的話,也可以給RSS內(nèi)容增加一個(gè)縮略圖。要做到這一點(diǎn),只需要在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,開(kāi)啟WordPress評(píng)論嵌套功能。
評(píng)論嵌套功能是WordPress自身帶有的最好功能之一,只可惜很多WordPress模板都不支持。很多文章都有提到過(guò)修改的方法,但一般都涉 及到修改comments文件和header文件。事實(shí)上,通過(guò)修改functions.php文件來(lái)修改是最簡(jiǎn)便的,而且一勞永逸。
//?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登陸面板的錯(cuò)誤提示
當(dāng)你輸入的密碼或者用戶(hù)名錯(cuò)誤的時(shí)候,WordPress登陸界面會(huì)給出相應(yīng)的提示。但如果碰到黑客的話,這些提示反而給了他們更好的提示,讓他們更容易破解用戶(hù)名和密碼。因此,處于安全性考慮,移除WordPress登陸面板的錯(cuò)誤提示是非常必要的。
- add_filter('login_errors',create_function('$a', "return?null;"));
19,關(guān)閉WordPress的搜索功能
當(dāng)把WordPress當(dāng)做CMS系統(tǒng)來(lái)使用的時(shí)候,WordPress自帶的搜索功能實(shí)用性就不是太強(qiáng)了。一來(lái)增加數(shù)據(jù)庫(kù)查詢(xún)次數(shù),二來(lái)Google 自定義搜索會(huì)是更好的替代。因此,你只需要通過(guò)以下的代碼就可以關(guān)閉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簡(jiǎn)碼功能
Google AdSense 算是博客的標(biāo)配之一了,很多CMS經(jīng)常會(huì)在模板選項(xiàng)里面預(yù)置Google AdSense的廣告位。假如你的模板不支持,你可以通過(guò)以下的方法來(lái)解決:
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,不通過(guò).htaccess將rss地址唯一化
WordPress本身提供好幾個(gè)不同版本的rss地址,加入你又使用了FeedBurner或者feedsky的話,RSS地址就會(huì)更多。太多的RSS容易分流訂閱客戶(hù),而且也不利于品牌推廣。
一般的修改方法是通過(guò)更改.htaccess來(lái)進(jìn)行,此外,還可以通過(guò)以下的代碼來(lái)實(shí)現(xiàn)。
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 捐贈(zèng)簡(jiǎn)碼
當(dāng)你寫(xiě)完一篇以后,可以在文章里面插入paypal 捐贈(zèng)按鈕,方便讀者捐贈(zèng)。以下的代碼可以讓你非常輕松的做到這一點(diǎn)。
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" .$account.'&item_name=Donation?for?'.$for.'">'.$text.'</a>';
}
add_shortcode('donate',?'donate_shortcode');
23,設(shè)定文章從發(fā)布到出現(xiàn)在RSS中的時(shí)間長(zhǎng)短
通過(guò)RSS訂閱來(lái)閱讀博文的朋友可能都會(huì)有這個(gè)體驗(yàn):經(jīng)常發(fā)現(xiàn)RSS中的文字或者細(xì)節(jié)有錯(cuò)誤,而返回到頁(yè)面的時(shí)候卻發(fā)現(xiàn)錯(cuò)誤已經(jīng)沒(méi)有了。這種情況最有可能是因?yàn)?/p>
RSS最大的好處是快捷、直接,但這個(gè)最大的好處有時(shí)候?qū)ψ髡邅?lái)說(shuō)卻會(huì)引發(fā)某些尷尬。所以,有時(shí)候有必要讓文章發(fā)布后到讀者從RSS中按到有一個(gè)小小的時(shí)間差,方便作者排查某些問(wèn)題。以下的代碼可以做到以下幾點(diǎn):
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’);
這段代碼設(shè)置的時(shí)間是10分鐘,你可以把10改成任何你想要的時(shí)間。
24,自定義摘要輸出時(shí)的符號(hào)
一般設(shè)定自動(dòng)摘要輸出,你會(huì)經(jīng)常在WordPress博客的首頁(yè)看到“[...]”這樣的符號(hào)。為了界面的美觀,或者是個(gè)性化的需要,你可以把這個(gè)默認(rèn)的符號(hào)改變?yōu)槠渌姆?hào)。而以下的代碼就是為了實(shí)現(xiàn)這個(gè)而寫(xiě):
//?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,自定義摘要輸出的文字長(zhǎng)度
假如你比較懶,不想在撰寫(xiě)文章的時(shí)候每篇文章都輸入摘要,就可以讓系統(tǒng)自動(dòng)截取一定長(zhǎng)度的文字來(lái)作為摘要輸出。下面的代碼默認(rèn)是100個(gè)字節(jié),也就是50個(gè)漢字。你可以把數(shù)值修改成符合你需要的數(shù)字。
function new_excerpt_length($length)?{
return 100;
}
add_filter('excerpt_length',?'new_excerpt_length');
26,顯示精確評(píng)論數(shù)
WordPress默認(rèn)是把trackbacks 和 pings 都算作評(píng)論的,因此當(dāng)你設(shè)置不顯示trackbacks 和 ping的時(shí)候,評(píng)論數(shù)看起來(lái)總是不對(duì)頭。以下的代碼則以讓W(xué)ordPress只計(jì)算評(píng)論的數(shù)量,而不把trackbacks 和 pings也計(jì)算進(jìn)去。
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));
return count($comments_by_type['comment']);
} else {
return $count;
}
}
27,取消RSS輸出
對(duì)于某些博客而言,或者因?yàn)楸惶嗳瞬杉耍蛘咭驗(yàn)椴幌胱寗e人通過(guò)RSS訂閱,想取消RSS輸出。WordPress默認(rèn)是沒(méi)有這個(gè)功能的,但你可以通過(guò)以下的代碼來(lái)取消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 的訂閱數(shù)以及其他資料
Twitter系統(tǒng)以及很多第三方的客戶(hù)端都可以讓你在WordPress博客的側(cè)邊欄暫時(shí)Twitter的訂閱數(shù)以及一些其他的資料。這種做法往往很多時(shí)候都沒(méi)辦法跟博客已有的界面結(jié)合的很好。而以下的代碼則可以讓你自定義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];
}
把上面的代碼復(fù)制到 functions.php后,再把下面代碼復(fù)制到你想出現(xiàn)的地方即可。
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,彩色標(biāo)簽云
把代碼復(fù)制到 functions.php后,原本單色的標(biāo)簽云,會(huì)變成多彩的.
//彩色標(biāo)簽云
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);
30,評(píng)論回應(yīng)郵件通知
一般該功能都通過(guò)插件實(shí)現(xiàn),把代碼復(fù)制到 functions.php后,會(huì)在評(píng)論部分自動(dòng)插入可選擇的評(píng)論回應(yīng)郵件通知功能.
//?評(píng)論回應(yīng)郵件通知
function comment_mail_notify($comment_id)?{
$admin_notify =?'1'; //?admin?要不要收回復(fù)通知?(?'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?發(fā)出點(diǎn),?no-reply?可改為可用的?e-mail.
$to =?trim(get_comment($parent_id)->comment_author_email);
$subject =?'您在?['?.?get_option("blogname")?.?']?的留言有了回應(yīng)';
$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)?.?'?給您的回應(yīng):<br?/>'
.?trim($comment->comment_content)?.?'<br?/></p>
<p>您可以點(diǎn)擊?<a?href="'?.?htmlspecialchars(get_comment_link($parent_id))?.?'">查看回應(yīng)完整內(nèi)容</a></p>
<p>歡迎您再度光臨?<a?href="'?.?get_option('home')?.?'">'?.?get_option('blogname')?.?'</a></p>
<p>(此郵件由系統(tǒng)自動(dòng)發(fā)出,請(qǐng)勿回復(fù).)</p>
</div>';
$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');
//?自動(dòng)勾選
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">有人回復(fù)時(shí)郵件通知我</label>';
}
add_action('comment_form',?'add_checkbox');
?
?
?