1
0
Fork 0
mirror of https://github.com/yunluo/gdk.git synced 2024-05-10 18:26:59 +02:00
gdk/functions/seo.php

311 lines
10 KiB
PHP
Raw Normal View History

2020-01-25 12:25:07 +01:00
<?php
2020-01-27 05:06:36 +01:00
//加载网站地图xml
2020-03-15 14:29:06 +01:00
if (gdk_option('gdk_sitemap_xml')) {
2021-10-10 15:48:53 +02:00
include GDK_ROOT_PATH.'public/sitemap-xml.php';
include GDK_ROOT_PATH.'public/sitemap.php';
2020-01-27 05:06:36 +01:00
}
2020-01-27 15:28:34 +01:00
// 屏蔽蜘蛛爬取作者页面
2020-03-15 14:29:06 +01:00
if (gdk_option('gdk_no_author_page')) {
function gdk_no_author_page()
{
if (is_author()) {
wp_no_robots();
}
}
add_action('wp_head', 'gdk_no_author_page');
2020-01-27 15:28:34 +01:00
}
2020-01-27 05:06:36 +01:00
//robots.txt优化功能
2020-03-15 14:29:06 +01:00
if (gdk_option('gdk_robots')) {
add_filter('robots_txt', 'gdk_robots_txt', 10, 2);
function gdk_robots_txt($robotext)
{
if (gdk_option('gdk_sitemap_xml')) {
2021-10-10 15:48:53 +02:00
$sitemap = 'Sitemap: '.home_url('/sitemap.xml');
2020-03-15 14:29:06 +01:00
} else {
$sitemap = '';
}
2021-10-10 15:48:53 +02:00
return "User-agent: *
2020-01-27 05:06:36 +01:00
Disallow: /wp-admin/
Disallow: /wp-content/plugins/
Disallow: /wp-includes/
Disallow: /*/trackback
Disallow: /feed
Disallow: /*/feed
Disallow: /attachment/
Disallow: /wp-content/themes/
{$sitemap}";
2020-03-15 14:29:06 +01:00
}
2020-01-27 05:06:36 +01:00
}
//文章自动内链
2020-03-15 14:29:06 +01:00
if (gdk_option('gdk_tag_link')) {
function gdk_tag_link($content)
{
$post_tags = get_the_tags();
if ($post_tags) {
foreach ($post_tags as $tag) {
2021-10-10 15:48:53 +02:00
$link = get_tag_link($tag->term_id);
$keyword = $tag->name;
2020-03-15 14:29:06 +01:00
$cleankeyword = stripslashes($keyword);
2021-10-10 15:48:53 +02:00
$url = '<a target="_blank" href="'.$link.'" title="'.str_replace('%s', addcslashes($cleankeyword, '$'), '查看更多关于%s的文章').'">'.addcslashes($cleankeyword, '$').'</a>';
$regEx = '\'(?!((<.*?)|(<a.*?)))('.$cleankeyword.')(?!(([^<>]*?)>)|([^>]*?</a>))\'s';
$content = preg_replace($regEx, $url, $content, gdk_option('gdk_tag_num') ?? 5);
2020-03-15 14:29:06 +01:00
}
}
2021-10-10 15:48:53 +02:00
2020-03-15 14:29:06 +01:00
return $content;
}
add_filter('the_content', 'gdk_tag_link', 1);
2020-01-27 05:06:36 +01:00
}
2020-01-27 15:28:34 +01:00
// 自动添加nofloow
2020-03-15 14:29:06 +01:00
if (gdk_option('gdk_nofollow')) {
add_filter('the_content', 'gdk_nofollow');
add_filter('the_excerpt', 'gdk_nofollow');
function gdk_nofollow($content)
{
return preg_replace_callback('/<a[^>]+/', 'gdk_nofollow_callback', $content);
}
function gdk_nofollow_callback($matches)
{
2021-10-10 15:48:53 +02:00
$link = $matches[0];
2020-03-15 14:29:06 +01:00
$site_link = get_bloginfo('url');
2021-10-10 15:48:53 +02:00
if (false === strpos($link, 'rel')) {
$link = preg_replace("%(href=\\S(?!{$site_link}))%i", 'rel="nofollow" $1', $link);
} elseif (preg_match("%href=\\S(?!{$site_link})%i", $link)) {
2020-03-15 14:29:06 +01:00
$link = preg_replace('/rel=\S(?!nofollow)\S*/i', 'rel="nofollow"', $link);
}
2021-10-10 15:48:53 +02:00
2020-03-15 14:29:06 +01:00
return $link;
}
2020-01-27 15:28:34 +01:00
}
2020-02-02 19:46:18 +01:00
//百度主动推送
if (gdk_option('gdk_baidu_push')) {
2020-03-15 14:29:06 +01:00
function gdk_baidu_submit($post_ID)
{
2020-01-27 15:28:34 +01:00
global $post;
2021-10-10 15:48:53 +02:00
$bd_submit_site = get_bloginfo('url');
2020-02-02 19:46:18 +01:00
$bd_submit_token = gdk_option('gdk_baidu_token');
2020-03-15 14:29:06 +01:00
if (empty($post_ID) || empty($bd_submit_site) || empty($bd_submit_token)) {
return;
}
2021-10-10 15:48:53 +02:00
if (1 == get_post_meta($post_ID, 'gdk_baidu_submit', true)) {
2020-03-15 14:29:06 +01:00
return;
}
2021-10-10 15:48:53 +02:00
$url = get_permalink($post_ID);
$api = $api = 'http://data.zz.baidu.com/urls?site='.$bd_submit_site.'&token='.$bd_submit_token;
2020-01-27 15:28:34 +01:00
$status = $post->post_status;
2021-10-10 15:48:53 +02:00
if ('' != $status && 'publish' != $status) {
$request = new WP_Http();
$result = $request->request($api, [
'method' => 'POST',
'body' => $url,
2020-03-15 14:29:06 +01:00
'headers' => 'Content-Type: text/plain',
2021-10-10 15:48:53 +02:00
]);
if (is_array($result) && !is_wp_error($result) && '200' == $result['response']['code']) {
error_log('baidu_submit_result'.$result['body']);
2020-02-02 19:46:18 +01:00
$result = json_decode($result['body'], true);
}
if (array_key_exists('success', $result)) {
add_post_meta($post_ID, 'gdk_baidu_submit', 1, true);
}
2020-01-27 15:28:34 +01:00
}
}
2020-02-02 19:46:18 +01:00
add_action('publish_post', 'gdk_baidu_submit', 0);
2020-02-04 18:14:04 +01:00
add_action('wp_footer', 'gdk_baidu_auto_push', 500);
2020-02-02 19:46:18 +01:00
}
2020-02-04 18:14:04 +01:00
//百度自动推送
2020-03-15 14:29:06 +01:00
function gdk_baidu_auto_push()
{
echo '<script>
2020-01-28 07:51:13 +01:00
(function(){
var bp = document.createElement(\'script\');
var curProtocol = window.location.protocol.split(\':\')[0];
if (curProtocol === \'https\') {
2020-03-15 14:29:06 +01:00
bp.src = \'https://zz.bdstatic.com/linksubmit/push.js\';
2020-01-28 07:51:13 +01:00
}
else {
bp.src = \'http://push.zhanzhang.baidu.com/push.js\';
}
var s = document.getElementsByTagName("script")[0];
s.parentNode.insertBefore(bp, s);
})();
2020-01-27 15:28:34 +01:00
</script>';
2020-02-02 19:46:18 +01:00
}
2020-01-27 15:28:34 +01:00
2020-03-15 14:29:06 +01:00
if (gdk_option('gdk_seo_img')) {
//给文章图片自动添加alt和title信息
function gdk_imagesalt($content)
{
global $post;
2021-10-10 15:48:53 +02:00
$pattern = "/<a(.*?)href=('|\")(.*?).(bmp|gif|jpeg|jpg|png)('|\")(.*?)>/i";
$replacement = '<a$1href=$2$3.$4$5 alt="'.strip_tags($post->post_title).'" title="'.strip_tags($post->post_title).'"$6>';
$content = preg_replace($pattern, $replacement, $content);
2020-03-15 14:29:06 +01:00
return $content;
}
add_filter('the_content', 'gdk_imagesalt');
function gdk_image_alt_tag($content)
{
global $post;
preg_match_all('/<img (.*?)\/>/', $content, $images);
if (!is_null($images)) {
foreach ($images[1] as $index => $value) {
2021-10-10 15:48:53 +02:00
$new_img = str_replace('<img', '<img alt="'.strip_tags($post->post_title).'-'.get_option('blogname').'"', $images[0][$index]);
2020-03-15 14:29:06 +01:00
$content = str_replace($images[0][$index], $new_img, $content);
}
}
2021-10-10 15:48:53 +02:00
2020-03-15 14:29:06 +01:00
return $content;
}
add_filter('the_content', 'gdk_image_alt_tag', 99999);
2020-01-27 15:28:34 +01:00
}
2020-01-25 12:25:07 +01:00
//关键字
2020-03-15 14:29:06 +01:00
function gdk_keywords()
{
global $s, $post;
$keywords = '';
if (is_single()) {
if (get_the_tags($post->ID)) {
foreach (get_the_tags($post->ID) as $tag) {
2021-10-10 15:48:53 +02:00
$keywords .= $tag->name.', ';
2020-03-15 14:29:06 +01:00
}
}
foreach (get_the_category($post->ID) as $category) {
2021-10-10 15:48:53 +02:00
$keywords .= $category->cat_name.', ';
2020-03-15 14:29:06 +01:00
}
$keywords = substr_replace($keywords, '', -2);
} elseif (is_home() || is_front_page()) {
$keywords = gdk_option('gdk_keywords');
} elseif (is_tag()) {
if (in_string(tag_description(), '@@')) {
$keywords = gdk_term_meta('tag', 'keyword');
} else {
$keywords = single_tag_title('', false);
}
} elseif (is_category()) {
if (in_string(category_description(), '@@')) {
$keywords = gdk_term_meta('cat', 'keyword');
} else {
$keywords = single_cat_title('', false);
}
} elseif (is_search()) {
$keywords = esc_html($s, 1);
} else {
$keywords = trim(wp_title('', false));
}
if ($keywords) {
2021-10-10 15:48:53 +02:00
echo "<meta name=\"keywords\" content=\"{$keywords}\">\n";
2020-03-15 14:29:06 +01:00
}
2020-01-25 12:25:07 +01:00
}
2020-02-06 12:53:07 +01:00
add_action('wp_head', 'gdk_keywords');
2020-01-25 12:32:28 +01:00
2020-01-25 12:25:07 +01:00
//网站描述
2020-03-15 14:29:06 +01:00
function gdk_description()
{
global $s, $post;
$description = '';
2021-10-10 15:48:53 +02:00
$blog_name = get_bloginfo('name');
$excerpt = $post->post_excerpt;
2020-03-15 14:29:06 +01:00
if (is_singular()) {
if (!empty($excerpt)) {
$text = $excerpt;
} else {
$text = strip_shortcodes($post->post_content);
}
2021-10-10 15:48:53 +02:00
$description = trim(str_replace([
2020-03-15 14:29:06 +01:00
"\r\n",
"\r",
"\n",
2021-10-10 15:48:53 +02:00
' ',
' ',
], ' ', str_replace('"', "'", strip_tags($text))));
2020-03-15 14:29:06 +01:00
if (!($description)) {
2021-10-10 15:48:53 +02:00
$description = $blog_name.'-'.trim(wp_title('', false));
2020-03-15 14:29:06 +01:00
}
} elseif (is_home() || is_front_page()) {
$description = gdk_option('gdk_description'); // 首頁要自己加
} elseif (is_tag()) {
if (in_string(tag_description(), '@@')) {
2021-10-10 15:48:53 +02:00
$description = $blog_name."'".gdk_term_meta('tag', 'des')."'";
2020-03-15 14:29:06 +01:00
} else {
2021-10-10 15:48:53 +02:00
$description = $blog_name."'".single_tag_title('', false)."'";
2020-03-15 14:29:06 +01:00
}
} elseif (is_category()) {
if (in_string(category_description(), '@@')) {
2021-10-10 15:48:53 +02:00
$description = $blog_name."'".trim(strip_tags(gdk_term_meta('tag', 'des')));
2020-03-15 14:29:06 +01:00
} else {
2021-10-10 15:48:53 +02:00
$description = $blog_name."'".trim(strip_tags(category_description()));
2020-03-15 14:29:06 +01:00
}
} elseif (is_archive()) {
2021-10-10 15:48:53 +02:00
$description = $blog_name."'".trim(wp_title('', false))."'";
2020-03-15 14:29:06 +01:00
} elseif (is_search()) {
2021-10-10 15:48:53 +02:00
$description = $blog_name.": '".esc_html($s, 1)."' 的搜索結果";
2020-03-15 14:29:06 +01:00
} else {
2021-10-10 15:48:53 +02:00
$description = $blog_name."'".trim(wp_title('', false))."'";
2020-03-15 14:29:06 +01:00
}
$description = mb_substr($description, 0, 220, 'utf-8');
2021-10-10 15:48:53 +02:00
echo "<meta name=\"description\" content=\"{$description}\">\n";
2020-01-25 12:25:07 +01:00
}
2020-02-06 12:53:07 +01:00
add_action('wp_head', 'gdk_description');
2020-01-25 12:25:07 +01:00
2020-02-04 18:14:04 +01:00
//添加Open Graph Meta
2020-03-15 14:29:06 +01:00
function meta_og()
{
global $post;
if (is_single()) {
if (has_post_thumbnail($post->ID)) {
$img_src = gdk_thumbnail_src();
}
2021-10-10 15:48:53 +02:00
$excerpt = strip_tags($post->post_content);
2020-03-15 14:29:06 +01:00
$excerpt_more = '';
if (strlen($excerpt) > 155) {
2021-10-10 15:48:53 +02:00
$excerpt = substr($excerpt, 0, 155);
2020-03-15 14:29:06 +01:00
$excerpt_more = ' ...';
}
2021-10-10 15:48:53 +02:00
$excerpt = str_replace('"', '', $excerpt);
$excerpt = str_replace("'", '', $excerpt);
2020-03-15 14:29:06 +01:00
$excerptwords = preg_split('/[\n\r\t ]+/', $excerpt, -1, PREG_SPLIT_NO_EMPTY);
array_pop($excerptwords);
2021-10-10 15:48:53 +02:00
$excerpt = implode(' ', $excerptwords).$excerpt_more; ?>
<meta name="author" content="Your Name">
<meta name="description" content="<?php echo $excerpt; ?>">
<meta property="og:title" content="<?php echo the_title(); ?>">
<meta property="og:description" content="<?php echo $excerpt; ?>">
<meta property="og:type" content="article">
<meta property="og:url" content="<?php echo the_permalink(); ?>">
<meta property="og:site_name"
content="<?php echo get_bloginfo('name'); ?>">
<meta property="og:image" content="<?php ?>">
<?php
} else {
2020-03-15 14:29:06 +01:00
return;
}
2020-02-04 18:14:04 +01:00
}
add_action('wp_head', 'meta_og', 5);
2020-01-25 12:32:28 +01:00
2020-01-25 12:25:07 +01:00
//评论分页的seo处理
2020-03-15 14:29:06 +01:00
function gdk_canonical()
{
2020-01-25 12:25:07 +01:00
global $post;
if (get_query_var('paged') > 1) {
echo "\n";
echo "<link rel='canonical' href='";
echo get_permalink($post->ID);
echo "' />\n";
2021-10-10 15:48:53 +02:00
echo '<meta name="robots" content="noindex,follow">';
2020-01-25 12:25:07 +01:00
}
}
2021-10-10 15:48:53 +02:00
add_action('wp_head', 'gdk_canonical');