logo头像

博学广问,自律静思

wordpress无插件实现六个经典功能

wordpress建站系统为我们提供了丰富的插件功能,但是使用太多的插件往往会拖慢网站的速度,其实一些小插件的功能我们完全可以自己用代码实现,下面是我为大家整理的一些wordpress经典功能的无插件纯代码实现方法,代码来自互联网。如果没有特殊说明,下面的代码请放在主题文件夹(…/wp-content/themes/你的主题/)的模板函数functions.php文件里,修改方法:将下面代码复制粘贴到functions.php的局域内。

wordpress无插件实现六个经典功能

修改wordpress代码实现smtp邮件功能

相关插件:easy wp smtp 下载地址((经本人汉化))[button class=”download” size=”lg” href=”http://pan.baidu.com/s/1mgqV5Rq“ title=”easy wp smtp”]密码:0bmd [/button]

//使用smtp发邮件
add_action(‘phpmailer_init’, ‘mail_smtp’);
function mail_smtp( $phpmailer ) {
$phpmailer->IsSMTP();
$phpmailer->SMTPAuth = true;//启用SMTPAuth服务
$phpmailer->Port = 465;//MTP邮件发送端口,这个和下面的对应,如果这里填写25,则下面为空白
$phpmailer->SMTPSecure =”ssl”;//是否验证 ssl,这个和上面的对应,如果不填写,则上面的端口须为25
$phpmailer->Host =”smtp.gmail.com”;//邮箱的SMTP服务器地址,如果是QQ的则为:smtp.exmail.qq.com
$phpmailer->Username = “admin@gmail.com“;//你的邮箱地址
$phpmailer->Password =”******”;//你的邮箱登陆密码
}

 

WordPress无插件代码实现评论回复邮件通知

 

//comment_mail_notify(所有的回复都会发邮件通知)

function comment_mail_notify($comment_id) {
$comment = get_comment($comment_id);
$parent_id = $comment->comment_parent ? $comment->comment_parent : ‘’;
$spam_confirmed = $comment->comment_approved;
if (($parent_id != ‘’) && ($spam_confirmed != ‘spam’)) {
$wp_email = ‘no-reply@’ . preg_replace(‘#^www.#’, ‘’, strtolower($_SERVER[‘SERVER_NAME’]));//发件人e-mail地址
$to = trim(get_comment($parent_id)->comment_author_email);
$subject = ‘您在[‘.get_option(“blogname”).’]的留言有了回复’;
$message = ‘

'.trim(get\_comment($parent\_id)->comment_author).', 您好!

这是您在《'.get\_the\_title($comment->comment\_post\_ID).'》中的留言:
' .trim(get\_comment($parent\_id)->comment_content).'

以下是'.trim($comment->comment_author).' 给您的回复:
' .trim($comment->comment_content).'

您可以点击这里查看回复的完整内容.

欢迎再度光临 ' . get\_option('blogname') . '

(注:此邮件由系统自动发出,请勿回复!)

'; $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, '
' , $subject, $message; // for testing } } add\_action('comment\_post', 'comment\_mail\_notify');

 

文章图片自动添加alt和title信息

/* 文章图片自动添加alt和title信息 */
function tin_image_alt($content){
global $post;
$pattern = “/<img(.*?)src=(‘|\“)(.*?).(bmp|gif|jpeg|jpg|png)(‘|\“)(.*?)>/i”;
$replacement = ‘<img$1src=$2$3.$4$5 alt=”‘.$post->post_title.’” title=”‘.$post->post_title.’”$6>’;
$content = preg_replace($pattern,$replacement,$content);
return $content;
}
add_filter(‘the_content’,’tin_image_alt’,15);

  WordPress文字标签关键词自动内链

 

/* WordPress文字标签关键词自动内链*/
$match_num_from = 1; //一篇文章中同一個標籤少於幾次不自動鏈接
$match_num_to = 4; //一篇文章中同一個標籤最多自動鏈接幾次
function tag_sort($a, $b){
if ( $a->name == $b->name ) return 0;
return ( strlen($a->name) > strlen($b->name) ) ? -1 : 1;
}
function tin_tag_link($content){
global $match_num_from,$match_num_to;
$posttags = get_the_tags();
if ($posttags) {
usort($posttags, “tag_sort”);
$ex_word = ‘’;
$case = ‘’;
foreach($posttags as $tag) {
$link = get_tag_link($tag->term_id);
$keyword = $tag->name;
$cleankeyword = stripslashes($keyword);
$url = “<a href=\“$link\“ class=\“tooltip-trigger tin\“ title=\“”.str_replace(‘%s’,addcslashes($cleankeyword, ‘$’),__(‘查看更多关于 %s 的文章’)).”\“”;
$url .= ‘ target=”_blank”‘;
$url .= “>”.addcslashes($cleankeyword, ‘$’).”“;
$limit = rand($match_num_from,$match_num_to);
$content = preg_replace( ‘|(<a[^>]+>)(.)<pre.*?>(‘.$ex_word.’)(.)<\/pre>(</a[^>]>)|U’.$case, ‘$1$2$4$5’, $content);
$content = preg_replace( ‘|(<img)(.*?)(‘.$ex_word.’)(.*?)(>)|U’.$case, ‘$1$2$4$5’, $content);
$cleankeyword = preg_quote($cleankeyword,’\‘’);
$regEx = ‘\‘(?!((<.*?)|(<a.*?)))(‘. $cleankeyword . ‘)(?!(([^<>]
?)>)|([^>]*?))\‘s’ . $case;
$content = preg_replace($regEx,$url,$content,$limit);
$content = str_replace( ‘’, stripslashes($ex_word), $content);
}
}
return $content;
}
add_filter(‘the_content’,’tin_tag_link’,12);

 Avatar改用多说gravatar服务器或SSL链接

 

/* Avatar改用多说gravatar服务器或SSL链接 /
function mytheme_get_avatar($avatar) {
if(ot_get_option(‘gravatar_source’) == ‘duoshuo’){
$avatar = str_replace(array(“www.gravatar.com","0.gravatar.com","1.gravatar.com","2.gravatar.com"),"gravatar.duoshuo.com",$avatar);
}elseif(ot_get_option(‘gravatar_source’) == ‘ssl’){
$avatar = preg_replace(‘/.\/avatar\/(.)\?s=([\d]+)(&?.
)/‘,’‘,$avatar);
}
return $avatar;
}
add_filter( ‘get_avatar’, ‘mytheme_get_avatar’);

 替换文章或评论内容外链为内链

 

/* 替换文章或评论内容外链为内链*/
function convert_to_internal_links($content){
preg_match_all(‘/\shref=(\‘|\“)([^\‘\“#]*?)(\‘|\“)([\s]?)/‘,$content,$matches);
if($matches){
foreach($matches[2] as $val){
if(strpos($val,home_url())===false){
$rep = $matches[1][0].$val.$matches[3][0];
$new = ‘“‘.home_url().’/redirect?url=’.base64_encode($val).’”‘;
$content = str_replace(“$rep”,”$new”,$content);
}
}
}
return $content;
}
add_filter(‘the_content’,’convert_to_internal_links’,99);
add_filter(‘comment_text’,’convert_to_internal_links’,99);
add_filter(‘get_comment_author_link’,’convert_to_internal_links’,99);

@hezhezhiyu22
hezhezhiyu22 commented at 2015-03-02

对于我等菜鸟而言,你不说清在哪里添加代码,让我们怎么用啊! :mad:

@retravel
retravel commented at 2015-02-13

代码不是我写的哦

@以楽会友
以楽会友 commented at 2015-02-13

cool啊

@retravel
retravel commented at 2015-01-29

未完待续,正在整理,还有些代码没放上去

@俊霖
俊霖 commented at 2015-01-29

好评,不过你的代码,有点乱