WordPress 功能函数库

2

美化时间

<?php echo timeago(get_gmt_from_date(get_the_time('Y-m-d G:i:s'))); ?>
function timeago($ptime)
{
    $ptime = strtotime($ptime);
    $etime = time() - $ptime;
    if ($etime < 1) {
        return '刚刚';
    }
    $interval = array(
		12 * 30 * 24 * 60 * 60 => '年前', 
		30 * 24 * 60 * 60 => '个月前', 
		7 * 24 * 60 * 60 => '周前',
		24 * 60 * 60 => '天前',
		60 * 60 => '小时前',
		60 => '分钟前',
		1 => '秒前'
	);
    foreach ($interval as $secs => $str) {
        $d = $etime / $secs;
        if (1 <= $d) {
            $r = round($d);
            return $r . $str;
        }
    }
}

防止冒充管理员

function usercheck($incoming_comment) {
	$isSpam = 0;
	if (trim($incoming_comment['comment_author']) == ''.love('admin_name').'')
	$isSpam = 1;
	if (trim($incoming_comment['comment_author_email']) == ''.love('admin_email').'')
	$isSpam = 1;
	if(!$isSpam)
	return $incoming_comment;
	err('<i class="fa fa-exclamation-circle"></i>请勿冒充管理员发表评论!');
}

文字展开

add_shortcode('s', 'show_more');
function show_more($atts, $content = null) {
return '<p>查看隐藏的内容<span class="show-more" title="文字折叠"><span><i class="fa fa-plus-square"></i>展开</span></span></p>
<div class="section-content" style="display: none;">
'.$content.'
</div>
';
}
//js
// 文字展开
$(".show-more span").click(function(e) {
 $(this).html(["<i class='fa fa-plus-square'></i>展开", "<i class='fa fa-minus-square'></i>折叠"][this.hutia ^= 1]);
 $(this.parentNode.parentNode).next().slideToggle();
 e.preventDefault();
});

关键词加链接

if (love('tag_c')) {
// 关键词加链接
$match_num_from = 1; //一个关键字少于多少不替换
$match_num_to = love('chain_n');

add_filter('the_content','tag_link',1);

function tag_sort($a, $b){
if ( $a->name == $b->name ) return 0;
return ( strlen($a->name) > strlen($b->name) ) ? -1 : 1;
}

function tag_link($content){
global $match_num_from,$match_num_to;
$posttags = get_the_tags();
	if ($posttags) {
		usort($posttags, "tag_sort");
		foreach($posttags as $tag) {
			$link = get_tag_link($tag->term_id);
			$keyword = $tag->name;
 			if (preg_match_all('|(<h[^>]+>)'.$keyword.'(</h[^>]*>)|U', $content, $matchs)) {continue;}
			if (preg_match_all('|(<a[^>]+>)(.*?)'.$keyword.'(.*?)(</h[^>]*>)|U', $content, $matchs)) {continue;}

			$cleankeyword = stripslashes($keyword);
			$url = "<a href=\"$link\" title=\"".str_replace('%s',addcslashes($cleankeyword, '$'),__('查看与 %s 相关的文章'))."\"";
			$url .= 'target="_blank"';
			$url .= ">".addcslashes($cleankeyword, '$')."</a>";
			$limit = rand($match_num_from,$match_num_to);

			$content = preg_replace( '|(<a[^>]+>)(.*)('.$ex_word.')(.*)(</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 . ')(?!(([^<>]*?)>)|([^>]*?</a>))\'s' . $case;
			$content = preg_replace($regEx,$url,$content,$limit);
			$content = str_replace( '%&&&&&%', stripslashes($ex_word), $content);
		}
	}
	return $content;
}
}

字数统计

function count_words ($text) {
	global $post;
	if ( '' == $text ) {
	   $text = $post->post_content;
	   if (mb_strlen($output, 'UTF-8') < mb_strlen($text, 'UTF-8')) $output .= '共 ' . mb_strlen(preg_replace('/\s/','',html_entity_decode(strip_tags($post->post_content))),'UTF-8') . '字';
	   return $output;
	}
}

自定义头部代码

add_action('wp_head', 'love_head_ding');  //自定义头部代码
add_action('wp_footer', 'love_footer_ding');  //自定义底部代码
// 自定义头部代码
function love_head_ding(){
    love_head_code();
}
function love_head_code(){
    if (love("head_code")) {
      echo  love("head_code");
    }
}

// 自定义底部代码
function love_footer_ding(){
    love_footer_code();
}
function love_footer_code(){
    if (love("footer_code")) {
      echo  love("footer_code");
    }
}

定制CSS

add_action('wp_head', 'head_css');  //自定义头部代码css
function head_css(){
    custom_css();
}
function custom_css(){
    if (love("custom_css")) {
        $css = substr(love("custom_css"), 0);
        echo "<style>" . $css . "</style>";
    }
}

 选择颜色

add_action('wp_head', 'head_color');
function head_color(){
    custom_color();
}
function custom_color(){
    if (love("custom_color")) {
        $color = substr(love("custom_color"), 1);
    }
    if ($color) {
        $styles .= "
.single-meta li a:hover {
    background: #" . $color . ";
    color: #fff;
    border: 1px solid #2f889a;
}";
}

评论等级

<?php if (love('vip')) { ?><?php get_author_class($comment->comment_author_email,$comment->user_id); ?><?php if(user_can($comment->user_id, 1)); ?><?php } ?>
// 评论等级
if (love('vip')) {
function get_author_class($comment_author_email,$user_id){
global $wpdb;
$adminEmail = 'timle@timle.cn';
$author_count = count($wpdb->get_results(
"SELECT comment_ID as author_count FROM $wpdb->comments WHERE comment_author_email = '$comment_author_email' "));
// 'manage_options' 
if( current_user_can(manage_options) ) {
    echo '<a  title="博主"><i class="fa fa-address-card" aria-hidden="true"></i>
<span class="lv">博主</span></a>';
}
if( current_user_can( 'publish_pages' ) && !current_user_can( 'manage_options' ) ) {
    echo '编辑员';
}

if( current_user_can( 'publish_posts' ) && !current_user_can( 'publish_pages' ) ) {
    echo '作者员';
}

if( current_user_can( 'edit_posts' ) && !current_user_can( 'publish_posts' ) ) {
    echo '投稿员';
}
if( current_user_can( 'read' ) && !current_user_can( 'edit_posts' ) ) {
    echo '订阅者';
}	

$linkurls = $wpdb->get_results(
"SELECT link_url FROM $wpdb->links WHERE link_url = '$comment_author_url'");

		if($author_count>=0 && $author_count<2)
			echo '<a class="vip1" title="评论达人 LV.1"></a>';
		else if($author_count>=2 && $author_count<5)
			echo '<a class="vip2" title="评论达人 LV.2"></a>';
		else if($author_count>=5 && $author_count<10)
			echo '<a class="vip3" title="评论达人 LV.3"></a>';
		else if($author_count>=10 && $author_count<20)
			echo '<a class="vip4" title="评论达人 LV.4"></a>';
		else if($author_count>=20 && $author_count<50)
			echo '<a class="vip5" title="评论达人 LV.5"></a>';
		else if($author_count>=50)
			echo '<a class="vip6" title="评论达人 LV.6"></a>';		
		
	}
}

6

预见

发表评论

:?::razz::sad::evil::!::smile::oops::grin::eek::shock::???::cool::lol::mad::twisted::roll::wink::idea::arrow::neutral::cry::mrgreen: