目录

WordPress优化指南

misaraty 更新 | 2022-01-11
前言
旧的建站方案基于WordPress 4.8.13 (WP),模板采用Twenty Seventeen。

WP插件安装

  • 首页显示定制化摘要,Advanced Excerpt

  • 语法高亮,Enlighter - Customizable Syntax Highlighter

  • google字体修改,Google Font Fix

  • 登录防护,Login LockDown

  • 站点地图,XML Sitemap & Google News

  • 静态缓存,WP Super Cache

  • 禁止自动更新,Disable All WordPress Updates、Disable plugins/themes updates

  • 清理草稿、修订版本、数据库,Easy WP Cleaner

  • 访问统计,WPS Visitor Counter Plugin

WP源码修改

  • 修改style.csswp-includes/js/tinymce/skins/wordpress/wp-content.csswp-content/plugins/enlighter/resources/EnlighterJS.min.css/WEB/wp-content/plugins/enlighter/cache/TinyMCE.css默认字体。
1
font-family: Arial, "Microsoft YaHei", "Droid Sans Fallback", "Hiragino Sans GB", Simsun, sans-serif;
  • 修改站名字体颜色,将~/twentyseventeen/inc/custom-header.php
1
2
3
body.has-header-video.colors-custom .site-description {
  color: #<?php echo esc_attr( $header_text_color ); ?>;
}

          改为,

1
2
3
4
5
body.has-header-video.colors-custom .site-description {
  color: #1f78b4;
        font-weight: bold;
        text-transform:none;
}
  • 修改站名字体粗细,将style.css
1
2
3
4
5
6
7
8
html[lang^="zh-"] h1,
html[lang^="zh-"] h2,
html[lang^="zh-"] h3,
html[lang^="zh-"] h4,
html[lang^="zh-"] h5,
html[lang^="zh-"] h6 {
  font-weight: 700;
}

          改为,

1
2
3
4
5
6
7
8
html[lang^="zh-"] h1,
html[lang^="zh-"] h2,
html[lang^="zh-"] h3,
html[lang^="zh-"] h4,
html[lang^="zh-"] h5,
html[lang^="zh-"] h6 {
  font-weight: bold;
}
  • 修改首页博文标题颜色及粗体样式,将style.css
 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
.page .panel-content .entry-title,
.page-title,
body.page:not(.twentyseventeen-front-page) .entry-title {
  color: #222;
  font-size: 14px;
  font-size: 0.875rem;
  font-weight: 800;
  letter-spacing: 0.14em;
  text-transform: uppercase;
}
.entry-header .entry-title {
  margin-bottom: 0.25em;
}
.entry-title a {
  color: #333;
  text-decoration: none;
  margin-left: -2px;
}

          改为,

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
.page .panel-content .entry-title,
.page-title,
body.page:not(.twentyseventeen-front-page) .entry-title {
  color: #222;
  font-size: 14px;
  font-size: 0.875rem;
  font-weight: 800;
  letter-spacing: 0.14em;
  text-transform: uppercase;
}
.entry-header .entry-title {
  margin-bottom: 0.25em;
}
.entry-title a {
  color: #1f78b4;
    font-weight: bold;
  text-decoration: none;
  margin-left: -2px;
}
  • 修改子页标题颜色,将style.css
1
2
3
4
5
.single-post .entry-title,
.page .entry-title {
  font-size: 26px;
  font-size: 1.625rem;
}

          改为,

1
2
3
4
5
6
.single-post .entry-title,
.page .entry-title {
  font-size: 26px;
  font-size: 1.625rem;
      color: #1f78b4;
}
  • 修改作者名大小写,将style.css
1
2
3
4
5
6
7
8
9
.entry-meta {
  color: #767676;
  font-size: 11px;
  font-size: 0.6875rem;
  font-weight: bold;
  letter-spacing: 0.1818em;
  padding-bottom: 0.25em;
  text-transform: uppercase;
}

          改为,

1
2
3
4
5
6
7
8
9
.entry-meta {
  color: #767676;
  font-size: 11px;
  font-size: 0.6875rem;
  font-weight: bold;
  letter-spacing: 0.1818em;
  padding-bottom: 0.25em;
  text-transform: none;
}
  • 删除首页“文章”提示,将index.php
1
2
3
4
5
6
7
8
9
<div class="wrap">
  <?php if ( is_home() && ! is_front_page() ) : ?>
    <header class="page-header">
      <h1 class="page-title"><?php single_post_title(); ?></h1>
    </header>
  <?php else : ?>
  <header class="page-header">
    <h2 class="page-title"><?php _e( 'Posts', 'twentyseventeen' ); ?></h2>
  </header>

          改为,

1
2
3
4
5
<div class="wrap">
  <?php if ( is_home() && ! is_front_page() ) : ?>
    <header class="page-header">
      <h1 class="page-title"><?php single_post_title(); ?></h1>
    </header>
  • 修改footer.php
 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
<?php
/**
 * The template for displaying the footer
 *
 * Contains the closing of the #content div and all content after.
 *
 * @link https://developer.wordpress.org/themes/basics/template-files/#template-partials
 *
 * @package WordPress
 * @subpackage Twenty_Seventeen
 * @since Twenty Seventeen 1.0
 * @version 1.2
 */
?>
    </div><!-- #content -->
    <footer id="colophon" class="site-footer" role="contentinfo">
      <div class="wrap">
        <?php
        get_template_part( 'template-parts/footer/footer', 'widgets' );
        if ( has_nav_menu( 'social' ) ) :
          ?>
          <nav class="social-navigation" role="navigation" aria-label="<?php esc_attr_e( 'Footer Social Links Menu', 'twentyseventeen' ); ?>">
            <?php
              wp_nav_menu(
                array(
                  'theme_location' => 'social',
                  'menu_class'     => 'social-links-menu',
                  'depth'          => 1,
                  'link_before'    => '<span class="screen-reader-text">',
                  'link_after'     => '</span>' . twentyseventeen_get_svg( array( 'icon' => 'chain' ) ),
                )
              );
            ?>
          </nav><!-- .social-navigation -->
          <?php
        endif;
        get_template_part( 'template-parts/footer/site', 'info' );
        ?>
      </div><!-- .wrap -->
    </footer><!-- #colophon -->
  </div><!-- .site-content-contain -->
</div><!-- #page -->
<?php wp_footer(); ?>
</body>
</html>

          改为,

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
<?php
/**
 * The template for displaying the footer
 *
 * Contains the closing of the #content div and all content after.
 *
 * @link https://developer.wordpress.org/themes/basics/template-files/#template-partials
 *
 * @package WordPress
 * @subpackage Twenty_Seventeen
 * @since 1.0
 * @version 1.2
 */
?>
    </div><!-- #content -->
    <footer id="colophon" class="site-footer" role="contentinfo">
      <div class="wrap"> 
        <left><a href="https://www.misaraty.com/" target='_blank' >Copyright © Misaraty</a><left>
        </div><!-- .wrap -->
      
    </footer><!-- #colophon -->
  </div><!-- .site-content-contain -->
</div><!-- #page -->
<?php wp_footer(); ?>
</body>
</html>
  • 增加ICO

          上传a.ico于根目录。修改主题下面的header.php,在<head>...</head>中插入,

1
<link id="favicon" href="https://www.misaraty.com/a.ico" rel="icon" type="image/x-icon" />
  • 禁止英文引号转中文

          打开wp-includes/formatting.php,注释以下部分,

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
$curl = str_replace( $static_characters, $static_replacements, $curl );
if ( false !== strpos( $curl, "'" ) ) {
  $curl = preg_replace( $dynamic_characters['apos'], $dynamic_replacements['apos'], $curl );
  $curl = wptexturize_primes( $curl, "'", $prime, $open_sq_flag, $closing_single_quote );
  $curl = str_replace( $apos_flag, $apos, $curl );
  $curl = str_replace( $open_sq_flag, $opening_single_quote, $curl );
}
if ( false !== strpos( $curl, '"' ) ) {
  $curl = preg_replace( $dynamic_characters['quote'], $dynamic_replacements['quote'], $curl );
  $curl = wptexturize_primes( $curl, '"', $double_prime, $open_q_flag, $closing_quote );
  $curl = str_replace( $open_q_flag, $opening_quote, $curl );
}
if ( false !== strpos( $curl, '-' ) ) {
  $curl = preg_replace( $dynamic_characters['dash'], $dynamic_replacements['dash'], $curl );
}
  • 文章以摘要模式显示

          修改index.php

1
get_template_part( 'template-parts/post/content', get_post_format() );

          改为,

1
get_template_part( 'template-parts/post/content','excerpt',get_post_format() );

          修改archive.php

1
get_template_part( 'template-parts/post/content', get_post_format() );

          改为,

1
get_template_part( 'template-parts/post/content', 'excerpt', get_post_format() );

          修改wp-includes/formatting.php

1
$excerpt_length = apply_filters( 'excerpt_length', 55 );

          改为,

1
$excerpt_length = apply_filters( 'excerpt_length', 100 );
  • 为搜索关键词加引号,修改search.php
1
printf( __( 'Search Results for: %s', 'twentyseventeen' ), '<span>' . get_search_query() . '</span>' );

          改为,

1
printf( __( 'Search Results for: %s', 'twentyseventeen' ), '<span>&#34;' . get_search_query() . '&#34;</span>' );
  • 侧栏文本标题字体大小,将style.css
1
2
3
4
5
h2.widget-title {
  font-size: 11px;
  font-size: 0.6875rem;
  margin-bottom: 2em;
}

          改为,

1
2
3
4
5
h2.widget-title {
  font-size: 11px;
  font-size: 0.875rem;
  margin-bottom: 2em;
}
注意