當(dāng)有人反饋某篇文章下載鏈接失效,縉哥哥更新后仍舊不知道,所以如題,我們有時(shí)候會(huì)因?yàn)橐恍┕δ艿牟煌昝阑蛘叽a的時(shí)效性、下載地址的更新等,對(duì)一些舊文章進(jìn)行一些不定期更新,但是這種更新一旦沒(méi)有及時(shí)通知訪(fǎng)客,往往就沒(méi)有什么意義了,訪(fǎng)客很難知道。縉哥哥從“龍笑天下”和“閑魚(yú)”博主那看過(guò)一篇文章《WordPress修改更新文章后郵件通知評(píng)論過(guò)那篇文章的用戶(hù)》《WordPress純代碼實(shí)現(xiàn)展示最近更新過(guò)的文章》,縉哥哥覺(jué)得這個(gè)方法也很好,但是也有其局限性,比如郵件太多會(huì)造成垃圾郵件、有限的評(píng)論訪(fǎng)客導(dǎo)致文章更新擴(kuò)散不廣等等。縉哥哥學(xué)習(xí)閑魚(yú)大神將二者結(jié)合起來(lái),并做此記錄。
展示更新文章代碼
將以下代碼添加到主題的 functions.php里
// Recently Updated Posts by zwwooooo | zww.me
function recently_updated_posts($num=10,$days=7) {
if( !$recently_updated_posts = get_option('recently_updated_posts') ) {
query_posts('post_status=publish&orderby=modified&posts_per_page=-1');
$i=0;
while ( have_posts() && $i<$num ) : the_post();
if (current_time('timestamp') - get_the_time('U') > 60*60*24*$days) {
$i++;
$the_title_value=get_the_title();
$recently_updated_posts.='<li><a href="'.get_permalink().'" title="'.$the_title_value.'">'
.$the_title_value.'</a><span class="updatetime"><br />? 修改時(shí)間: '
.get_the_modified_time('Y.m.d G:i').'</span></li>';
}
endwhile;
wp_reset_query();
if ( !empty($recently_updated_posts) ) update_option('recently_updated_posts', $recently_updated_posts);
}
$recently_updated_posts=($recently_updated_posts == '') ? '<li>None data.</li>' : $recently_updated_posts;
echo $recently_updated_posts;
}
function clear_cache_zww() {
update_option('recently_updated_posts', ''); // 清空 recently_updated_posts
}
add_action('save_post', 'clear_cache_zww'); // 新發(fā)表文章/修改文章時(shí)觸發(fā)更新
調(diào)用方式
8 為展示文章數(shù)量,15 指15天內(nèi)發(fā)表的文章除外,具體使用的時(shí)候可以根據(jù)自己的情況修改這兩個(gè)參數(shù)。
<h3>Recently Updated Posts</h3>
<ul>
<?php if ( function_exists('recently_updated_posts') ) recently_updated_posts(8,15); ?>
</ul>
縉哥哥有添加數(shù)據(jù)庫(kù)緩存方式,所以在修改文章/刪除文章/發(fā)表文章時(shí)才會(huì)更新緩存。
相關(guān)參數(shù)說(shuō)明:$num – 展示數(shù)量,$days – 幾天內(nèi)的新文章除外。
更新文章后郵件通知評(píng)論過(guò)的用戶(hù)
將以下代碼添加到functions.php中,會(huì)在修改編輯文章的頁(yè)面添加一個(gè)選項(xiàng),只有勾選才會(huì)郵件通知。
/**
* WordPress展示最近更新過(guò)的文章 并通知評(píng)論過(guò)的用戶(hù)
* https://www.dujin.org/fenxiang/wp/9809.html
*/
//修改更新文章時(shí)郵件通知評(píng)論用戶(hù)
add_action( 'submitpost_box', 'lxtx_fo_submit_box');
function lxtx_fo_submit_box( ){
echo '<div id="fo_side-sortables" class="meta-box-sortables ui-sortable">';
echo '<div id="fo_submit_box" class="postbox ">';
echo '<div class="handlediv" title="點(diǎn)擊以切換"><br></div>';
echo '<h3 class="hndle"><span>郵件通知</span></h3>';
echo '<div class="inside"><div class="submitbox">';
echo ' <div style="padding: 10px 10px 0;text-align: left;"><label class="selectit" title="慎用此功能,重要文章才勾選嘛,以免引起讀者反感哈"><input type="checkbox" name="FO_emaill_report_user" value="true" title="勾選此項(xiàng),將郵件通知本文所有評(píng)論者"/>郵通知本文所有評(píng)論者</label></div>';
echo '</div></div>';
echo '</div>';
echo '</div>';
}
//開(kāi)始
add_action( 'publish_post', 'lxtx_fo_emaill_report_users' );
function lxtx_fo_emaill_report_users($post_ID)
{
//如果未勾選保存,不進(jìn)行任何操作
if($_POST['FO_emaill_report_user'] != 'true'){
return;
}
//修訂版本不通知,以免濫用
if( wp_is_post_revision($post_ID) ){
return;
}
//獲取wp數(shù)據(jù)操作類(lèi)
global $wpdb,$post;
// 讀數(shù)據(jù)庫(kù),獲取文章的所有用戶(hù)的email并且不重復(fù)
$emailauthor != '你自己的郵箱';
$wp_user_emails = $wpdb->get_results("SELECT DISTINCT comment_author, comment_author_email FROM $wpdb->comments WHERE TRIM(comment_author_email) IS NOT NULL AND TRIM(comment_author_email) != '' AND TRIM(comment_author_email) != '$emailauthor' AND comment_post_ID = $post->ID");
// 獲取博客名稱(chēng)
$blogname = wp_specialchars_decode(get_option('blogname'), ENT_QUOTES);
// 獲取博客URL
$blogurl = get_bloginfo("siteurl");
//文章鏈接
$post_link = get_permalink($post_ID);
//文章標(biāo)題$post -> post_title
$post_title = strip_tags($_POST['post_title']);
//文章內(nèi)容$post->post_content
$post_content = strip_tags($_POST['post_content']);
//文章摘要
$output = preg_replace('/^(?:[\x00-\x7F]|[\xC0-\xFF][\x80-\xBF]+){0,0}((?:[\x00-\x7F]|[\xC0-\xFF][\x80-\xBF]+){0,200}).*/s','\1',$post_content).'......';
//郵件頭,以免亂碼
$message_headers = "Content-Type: text/html; charset=\"utf-8\"\n";
// 郵件標(biāo)題
$subject = '您曾經(jīng)來(lái)訪(fǎng)過(guò)的《'.$post_title.'》文章更新通知。';
foreach ( $wp_user_emails as $wp_user_email )
{
// 郵件內(nèi)容
$message = '
<div style="MARGIN-RIGHT: auto; MARGIN-LEFT: auto;">
<strong style="line-height: 1.5; font-family:Microsoft YaHei;">
親愛(ài)的'.$wp_user_email->comment_author.':
</strong>
<p style="FONT-SIZE: 14px; PADDING-TOP: 6px">
您曾經(jīng)來(lái)訪(fǎng)過(guò)的《'.$post_title.'》有更新,博主覺(jué)得有必要通知您,希望不會(huì)騷擾到您。
</p>
<p style="FONT-SIZE: 14px; PADDING-TOP: 6px">
文章標(biāo)題:<a title="'.$post_title.'" href="'.$post_link.'" target="_top">'.$post_title.'</a>
<br/>
文章摘要:'.$output.'
</p>
<p style="FONT-SIZE: 14px; PADDING-TOP: 6px">
您可以點(diǎn)擊鏈接
<a href="'.$blogurl.'" style="line-height: 1.5;">'.$blogname.'</a>
>
<a title="'.$post_title.'" href="'.$post_link.'" target="_top">'.$post_title.'</a>
詳細(xì)查看
</p>
<p style="font-size: 14px; padding-top: 6px; text-align: left;">
<span style="line-height: 1.5; color: rgb(153, 153, 153);">
來(lái)自:
</span>
<a href="'.$blogurl.'" style="line-height: 1.5;">'.$blogname.'</a>
</p>
<div style="font-size: 12px; border-top-color: rgb(204, 204, 204); border-top-width: 1px; border-top-style: solid; height: 35px; width: 500px; color: rgb(102, 102, 102); line-height: 35px; background-color: rgb(245, 245, 245);">
該郵件為系統(tǒng)發(fā)送郵件,請(qǐng)勿直接回復(fù)!如有打擾,請(qǐng)向博主留言反映。灰常感謝您的閱讀!
</div>
</div>';
wp_mail($wp_user_email->comment_author_email, $subject, $message, $message_headers);
}
}
效果截圖
在WordPress后臺(tái)編輯文章頁(yè)面右上方出,有“郵件通知本文所有評(píng)論者”。
請(qǐng)無(wú)視縉哥哥少了一個(gè)字,我懶得換圖片。







沒(méi)有啊
這個(gè)不錯(cuò) 我正需要這個(gè) 希望能成功。
我還想請(qǐng)教一個(gè)問(wèn)題,我的網(wǎng)站 設(shè)置了 須經(jīng)由管理員審核 選項(xiàng)。
但是,一般游客評(píng)論后無(wú)法看到 自己評(píng)論的內(nèi)容和 在評(píng)論顯示之前 須經(jīng)由管理員審核的提示。
游客評(píng)論后什么都看不見(jiàn)。都不知道有沒(méi)有評(píng)論成功。很不方便。
管理者能看到 待審核的游客評(píng)論。審核通過(guò)以后都正常顯示了。
應(yīng)該是你的主題的問(wèn)題,一般正規(guī)點(diǎn)的主題都會(huì)有提示 等待審核 的,建議你聯(lián)系主題作者問(wèn)問(wèn)吧