* @license MIT License
* @version 2020.06.13
*/
// 文章链接添加 target 和 rel
function imgnofollow($content)
{
$regexp = "]*href=(\"??)([^\" >]*?)\\1[^>]*>";
if (preg_match_all("/$regexp/siU", $content, $matches, PREG_SET_ORDER)) {
if (!empty($matches)) {
$srcUrl = get_option('siteurl');
for ($i = 0; $i < count($matches); $i++) {
$tag = $matches[$i][0];
$tag2 = $matches[$i][0];
$url = $matches[$i][0];
$noFollow = '';
$pattern = '/target\s*=\s*"\s*_blank\s*"/';
preg_match($pattern, $tag2, $match, PREG_OFFSET_CAPTURE);
if (count($match) < 1) {
$noFollow .= ' target="_blank" ';
}
$pattern = '/rel\s*=\s*"\s*[n|d]ofollow\s*"/';
preg_match($pattern, $tag2, $match, PREG_OFFSET_CAPTURE);
if (count($match) < 1) {
$noFollow .= ' rel="nofollow" ';
}
$pos = strpos($url, $srcUrl);
if ($pos === false) {
$tag = rtrim($tag, '>');
$tag .= $noFollow . '>';
$content = str_replace($tag2, $tag, $content);
}
}
}
}
$content = str_replace(']]>', ']]>', $content);
return $content;
}
add_filter('the_content', 'imgnofollow');
// 文章点赞
function love()
{
global $wpdb, $post;
$id = $_POST["um_id"];
$action = $_POST["um_action"];
if ($action == 'love') {
$raters = get_post_meta($id, 'love', true);
$expire = time() + 99999999;
$domain = ($_SERVER['HTTP_HOST'] != 'localhost') ? $_SERVER['HTTP_HOST'] : false;
setcookie('love_' . $id, $id, $expire, '/', $domain, false);
if (!$raters || !is_numeric($raters)) {
update_post_meta($id, 'love', 1);
} else {
update_post_meta($id, 'love', ($raters + 1));
}
echo get_post_meta($id, 'love', true);
}
die;
}
add_action('wp_ajax_nopriv_love', 'love');
add_action('wp_ajax_love', 'love');
// 文章阅读次数统计
function set_post_views()
{
if (is_singular()) {
global $post;
$post_ID = $post->ID;
if ($post_ID) {
$post_views = (int) get_post_meta($post_ID, 'views', true);
if (!update_post_meta($post_ID, 'views', ($post_views + 1))) {
add_post_meta($post_ID, 'views', 1, true);
}
}
}
}
add_action('wp_head', 'set_post_views');
function get_post_views($echo = 1)
{
global $post;
$post_ID = $post->ID;
$views = (int) get_post_meta($post_ID, 'views', true);
return $views;
}
// 文章列表简介内容
function excerpt_length($length)
{
return 260;
}
add_filter('excerpt_length', 'excerpt_length');
// 开启特色图
add_theme_support("post-thumbnails");
// 文章特色图片
function post_thumbnail()
{
global $post;
$img_id = get_post_thumbnail_id();
$img_url = wp_get_attachment_image_src($img_id, array(720, 435));
if (is_array($img_url)) {
$img_url = $img_url[0];
}
if (has_post_thumbnail()) {
echo '';
} else {
$content = $post->post_content;
$img_preg = "/
/";
preg_match($img_preg, $content, $img_src);
$img_count = count($img_src) - 1;
if (isset($img_src[$img_count])) {
$img_val = $img_src[$img_count];
}
if (!empty($img_val)) {
echo '
';
} else {
/* 正文里没图、也没设特色图时用默认特色图。
空值(用户清过那一格)同样落到包内默认图。 */
$img = yang_option_image('g_postthumbnail');
if ($img !== '') {
echo '
';
}
}
}
}
// 文章列表分页
function pagelist($range = 5)
{
global $paged, $wp_query, $max_page;
if (!$max_page) {$max_page = $wp_query->max_num_pages;}
if ($max_page > 1) {if (!$paged) {$paged = 1;}
echo "