WordPress不用插件怎么SEO,纯代码优化指南

来自:素雅营销研究院

头像 方知笔记
2025年07月05日 10:40

在WordPress网站优化中,许多人依赖SEO插件如Yoast或All in One SEO Pack,但其实通过纯代码方式也能实现出色的SEO效果。本文将介绍不用插件的WordPress SEO优化方法。

1. 优化网站标题和描述

在主题的header.php文件中,可以手动添加SEO标题和描述:

<title><?php
if (is_home()) {
bloginfo('name'); echo " - "; bloginfo('description');
} elseif (is_category()) {
single_cat_title(); echo " - "; bloginfo('name');
} elseif (is_single()) {
single_post_title(); echo " - "; bloginfo('name');
} else {
wp_title('',true); echo " - "; bloginfo('name');
}
?></title>
<meta name="description" content="<?php
if (is_single()) {
echo strip_tags(get_the_excerpt());
} else {
bloginfo('description');
}
?>" />

2. 优化永久链接结构

在WordPress后台”设置”→”固定链接”中,选择”文章名”或自定义结构如/%category%/%postname%/,这比默认的?p=123格式更SEO友好。

3. 添加规范的URL

在header.php中添加以下代码防止重复内容问题:

<link rel="canonical" href="<?php the_permalink(); ?>" />

4. 优化图片SEO

在主题的functions.php中添加自动为图片添加alt标签的功能:

function auto_image_alt($content) {
global $post;
$pattern = '/<img(.*?)src="(.*?)"(.*?)>/i';
$replacement = '<img$1src="$2" alt="'.$post->post_title.'"$3>';
$content = preg_replace($pattern, $replacement, $content);
return $content;
}
add_filter('the_content', 'auto_image_alt');

5. 创建XML站点地图

在主题目录下创建sitemap.php文件,然后通过functions.php注册路由:

function generate_sitemap() {
$postsForSitemap = get_posts(array(
'numberposts' => -1,
'orderby' => 'modified',
'post_type'  => array('post','page'),
'order'    => 'DESC'
));

$sitemap = '<?xml version="1.0" encoding="UTF-8"?>';
$sitemap .= '<urlset xmlns="http://www.sitemaps.org/schemas/sitemap/0.9">';

foreach($postsForSitemap as $post) {
$sitemap .= '<url>'.
'<loc>'. get_permalink($post->ID) .'</loc>'.
'<lastmod>'. date("Y-m-d", strtotime($post->post_modified)) .'</lastmod>'.
'<changefreq>monthly</changefreq>'.
'</url>';
}

$sitemap .= '</urlset>';

$fp = fopen(ABSPATH . 'sitemap.xml', 'w');
fwrite($fp, $sitemap);
fclose($fp);
}
add_action("publish_post", "generate_sitemap");
add_action("publish_page", "generate_sitemap");

6. 优化网站速度

速度是SEO的重要因素,可通过以下方式优化:

  1. 在.htaccess中添加缓存控制:
<IfModule mod_expires.c>
ExpiresActive On
ExpiresByType image/jpg "access 1 year"
ExpiresByType image/jpeg "access 1 year"
ExpiresByType image/gif "access 1 year"
ExpiresByType image/png "access 1 year"
ExpiresByType text/css "access 1 month"
ExpiresByType text/html "access 1 month"
ExpiresByType application/pdf "access 1 month"
ExpiresByType text/x-javascript "access 1 month"
ExpiresByType application/x-shockwave-flash "access 1 month"
ExpiresByType image/x-icon "access 1 year"
ExpiresDefault "access 1 month"
</IfModule>
  1. 压缩HTML输出:
function compress_html($buffer) {
$search = array('/\>[^\S ]+/s', '/[^\S ]+\</s', '/(\s)+/s');
$replace = array('>', '<', '\\1');
$buffer = preg_replace($search, $replace, $buffer);
return $buffer;
}
if (!is_admin()) {
ob_start("compress_html");
}

7. 结构化数据标记

在header.php中添加基本的JSON-LD结构化数据:

function add_structured_data() {
if (is_single()) {
$logo = get_theme_mod('custom_logo');
$image = wp_get_attachment_image_src($logo , 'full');
$logo_url = $image[0];
?>
<script type="application/ld+json">
{
"@context": "https://schema.org",
"@type": "Article",
"headline": "<?php the_title(); ?>",
"image": "<?php echo get_the_post_thumbnail_url(); ?>",
"author": {
"@type": "Person",
"name": "<?php the_author(); ?>"
},
"publisher": {
"@type": "Organization",
"name": "<?php bloginfo('name'); ?>",
"logo": {
"@type": "ImageObject",
"url": "<?php echo $logo_url; ?>"
}
},
"datePublished": "<?php echo get_the_date('Y-m-d'); ?>",
"dateModified": "<?php echo get_the_modified_date('Y-m-d'); ?>",
"mainEntityOfPage": "<?php the_permalink(); ?>"
}
</script>
<?php
}
}
add_action('wp_head', 'add_structured_data');

8. 防止垃圾评论

在functions.php中添加:

function check_referrer() {
if (!isset($_SERVER['HTTP_REFERER']) {
wp_die( __('请勿直接提交评论!','textdomain') );
}
}
add_action('check_comment_flood', 'check_referrer');

9. 优化404页面

创建自定义404.php模板,包含搜索框和相关文章推荐。

10. 移动端优化

确保主题是响应式的,并在header.php中添加:

<meta name="viewport" content="width=device-width, initial-scale=1.0">

通过以上方法,您可以在不使用任何SEO插件的情况下,全面优化WordPress网站的SEO表现。这些代码优化不仅减少了插件依赖,还能提高网站性能,为搜索引擎提供更友好的内容结构。