如何清理WordPress網站Header部分不必要程式碼?

如何清理WordPress網站Header部分不必要程式碼?

很長一段時間以來,我一直在使用幾個外掛來清理WordPress Headers中的一些欄位。在大多數情況下,我會說您通常不需要很多資訊。

為什麼WordPress將這些功能和連結新增到您的網站?

嗯,原因很明顯。WordPress是一個非常大的CMS平臺,被超過27%的線上部落格網站使用。每個出版商都有自己的要求。有些喜歡wp-admin瀏覽器頁面釋出文章,有些使用第三方工具,有些使用 iOS或Android應用。

如果您使用網路版本在您的WordPress網站上釋出文章,那麼您應該從您的WordPress網站中刪除所有不必要的連結。

有什麼優勢?

  1. 頁面載入速度肯定更快
  2. 內容程式碼比增加
  3. 您的重要網站內容現在略高於搜尋引擎的讀取方式

讓我們看一下WordPress Headers中的一些連結。以下步驟將幫助您清理和優化WordPress網站的header部分。

WordPress新增EditURI到您的網站Header,如果您通過第三方工具釋出文章,這是必需的。

Plain text
Copy to clipboard
Open code in new window
EnlighterJS 3 Syntax Highlighter
<link type="application/rsd+xml" title="RSD" href="https://crunchify.com/xmlrpc.php?rsd">
<link type="application/rsd+xml" title="RSD" href="https://crunchify.com/xmlrpc.php?rsd">
<link type="application/rsd+xml" title="RSD" href="https://crunchify.com/xmlrpc.php?rsd">

怎麼禁用?將下方程式碼新增到您的主題functions.php檔案中:

Plain text
Copy to clipboard
Open code in new window
EnlighterJS 3 Syntax Highlighter
remove_action ('wp_head', 'rsd_link');
remove_action ('wp_head', 'rsd_link');
remove_action ('wp_head', 'rsd_link');

2.刪除WordPress版本號

版本號可能會暴露您的WordPress版本資訊,讓某些有預謀的人有機可乘。

Plain text
Copy to clipboard
Open code in new window
EnlighterJS 3 Syntax Highlighter
<meta name="generator" content="WordPress 4.9.2">
<meta name="generator" content="WordPress 4.9.2">
<meta name="generator" content="WordPress 4.9.2">

下面的程式碼將從站點中刪除WordPress版本號。

Plain text
Copy to clipboard
Open code in new window
EnlighterJS 3 Syntax Highlighter
function crunchify_remove_version() {
return '';
}
add_filter('the_generator', 'crunchify_remove_version');
function crunchify_remove_version() { return ''; } add_filter('the_generator', 'crunchify_remove_version');
function crunchify_remove_version() {
    return '';
}
add_filter('the_generator', 'crunchify_remove_version');

如果您不使用Windows Live Writer寫作,完全可以移除WordPress網站Header的以下內容。

Plain text
Copy to clipboard
Open code in new window
EnlighterJS 3 Syntax Highlighter
<link type="application/wlwmanifest+xml" href="https://www.wbolt.com/tw/wp-includes/wlwmanifest.xml">
<link type="application/wlwmanifest+xml" href="https://www.wbolt.com/tw/wp-includes/wlwmanifest.xml">
<link type="application/wlwmanifest+xml" href="https://www.wbolt.com/tw/wp-includes/wlwmanifest.xml">

在你的主題function新增以下程式碼:

Plain text
Copy to clipboard
Open code in new window
EnlighterJS 3 Syntax Highlighter
remove_action( 'wp_head', 'wlwmanifest_link');
remove_action( 'wp_head', 'wlwmanifest_link');
remove_action( 'wp_head', 'wlwmanifest_link');

如果您使用其他的固定連結形式,WordPress預設的短連線是完全沒有意義的,可以考慮移除。

Plain text
Copy to clipboard
Open code in new window
EnlighterJS 3 Syntax Highlighter
<link href="https://www.wbolt.com/tw/?p=8112">
<link href="https://www.wbolt.com/tw/?p=8112">
<link href="https://www.wbolt.com/tw/?p=8112">

在你的主題function新增以下程式碼:

Plain text
Copy to clipboard
Open code in new window
EnlighterJS 3 Syntax Highlighter
remove_action( 'wp_head', 'wp_shortlink_wp_head');
remove_action( 'wp_head', 'wp_shortlink_wp_head');
remove_action( 'wp_head', 'wp_shortlink_wp_head');

5.從所有靜態資源中刪除查詢字串

靜態資源查詢字串

靜態資源查詢字串

新增下面的程式碼,所有查詢字串都將被刪除。

Plain text
Copy to clipboard
Open code in new window
EnlighterJS 3 Syntax Highlighter
function wbolt_cleanup_query_string( $src ){
$parts = explode( '?', $src );
return $parts[0];}
add_filter( 'script_loader_src', 'wbolt_cleanup_query_string', 15, 1 );
add_filter( 'style_loader_src', 'wbolt_cleanup_query_string', 15, 1 );
function wbolt_cleanup_query_string( $src ){ $parts = explode( '?', $src ); return $parts[0];} add_filter( 'script_loader_src', 'wbolt_cleanup_query_string', 15, 1 ); add_filter( 'style_loader_src', 'wbolt_cleanup_query_string', 15, 1 );
function wbolt_cleanup_query_string( $src ){
    $parts = explode( '?', $src );
    return $parts[0];}
add_filter( 'script_loader_src', 'wbolt_cleanup_query_string', 15, 1 );
add_filter( 'style_loader_src', 'wbolt_cleanup_query_string', 15, 1 );

注: explore ('?', $src)將刪除?符號後的所有內容。如果您只想刪除with ver的查詢字串,請替換??ver.

WordPress REST API使CMS能夠與其他Web屬性進行通訊,無論它們是用什麼程式語言編寫的。

但是,很多網站不使用它,因此在大多數情況下,它只是不必要的程式碼。預設情況下,每個站點的標題中都包含一個連結:

Plain text
Copy to clipboard
Open code in new window
EnlighterJS 3 Syntax Highlighter
<link href="https://www.wbolt.com/tw/wp-json/">
<link href="https://www.wbolt.com/tw/wp-json/">
<link href="https://www.wbolt.com/tw/wp-json/">

在你的主題function新增以下程式碼:

Plain text
Copy to clipboard
Open code in new window
EnlighterJS 3 Syntax Highlighter
remove_action('wp_head', 'rest_output_link_wp_head', 10);
remove_action('template_redirect', 'rest_output_link_header', 11, 0);
remove_action('wp_head', 'rest_output_link_wp_head', 10); remove_action('template_redirect', 'rest_output_link_header', 11, 0);
remove_action('wp_head', 'rest_output_link_wp_head', 10);
remove_action('template_redirect', 'rest_output_link_header', 11, 0);

禁用WordPress網站上的嵌入將執行以下操作:

  • 它可以防止其他人嵌入您的網站,也可以防止您嵌入網站。
  • 刪除oEmbed特定的JavaScript。
  • 禁用對oEmbed結果的過濾。
  • 刪除oEmbed發現連結。
  • 關閉oEmbed自動發現。
  • 刪除所有嵌入重寫規則。
Plain text
Copy to clipboard
Open code in new window
EnlighterJS 3 Syntax Highlighter
remove_action('wp_head', 'wp_oembed_add_discovery_links', 10);
remove_action('wp_head', 'wp_oembed_add_discovery_links', 10);
remove_action('wp_head', 'wp_oembed_add_discovery_links', 10);

這是一個完整的程式碼:

將以下程式碼新增到主題的functions.php檔案中,一切就緒。

Plain text
Copy to clipboard
Open code in new window
EnlighterJS 3 Syntax Highlighter
// ******************** Clean up WordPress Header START ********************** //
function wbolt_remove_version() {
return '';
}
add_filter('the_generator', 'wbolt_remove_version');
remove_action('wp_head', 'rest_output_link_wp_head', 10);
remove_action('wp_head', 'wp_oembed_add_discovery_links', 10);
remove_action('template_redirect', 'rest_output_link_header', 11, 0);
remove_action ('wp_head', 'rsd_link');
remove_action( 'wp_head', 'wlwmanifest_link');
remove_action( 'wp_head', 'wp_shortlink_wp_head');
function wbolt_cleanup_query_string( $src ){
$parts = explode( '?', $src );
return $parts[0];
}
add_filter( 'script_loader_src', 'wbolt_cleanup_query_string', 15, 1 );
add_filter( 'style_loader_src', 'wbolt_cleanup_query_string', 15, 1 );
// ******************** Clean up WordPress Header END ********************** //
// ******************** Clean up WordPress Header START ********************** // function wbolt_remove_version() { return ''; } add_filter('the_generator', 'wbolt_remove_version'); remove_action('wp_head', 'rest_output_link_wp_head', 10); remove_action('wp_head', 'wp_oembed_add_discovery_links', 10); remove_action('template_redirect', 'rest_output_link_header', 11, 0); remove_action ('wp_head', 'rsd_link'); remove_action( 'wp_head', 'wlwmanifest_link'); remove_action( 'wp_head', 'wp_shortlink_wp_head'); function wbolt_cleanup_query_string( $src ){ $parts = explode( '?', $src ); return $parts[0]; } add_filter( 'script_loader_src', 'wbolt_cleanup_query_string', 15, 1 ); add_filter( 'style_loader_src', 'wbolt_cleanup_query_string', 15, 1 ); // ******************** Clean up WordPress Header END ********************** //
// ******************** Clean up WordPress Header START ********************** //
function wbolt_remove_version() {
    return '';
}
add_filter('the_generator', 'wbolt_remove_version');

remove_action('wp_head', 'rest_output_link_wp_head', 10);
remove_action('wp_head', 'wp_oembed_add_discovery_links', 10);
remove_action('template_redirect', 'rest_output_link_header', 11, 0);

remove_action ('wp_head', 'rsd_link');
remove_action( 'wp_head', 'wlwmanifest_link');
remove_action( 'wp_head', 'wp_shortlink_wp_head');

function wbolt_cleanup_query_string( $src ){
    $parts = explode( '?', $src );
    return $parts[0];
}
add_filter( 'script_loader_src', 'wbolt_cleanup_query_string', 15, 1 );
add_filter( 'style_loader_src', 'wbolt_cleanup_query_string', 15, 1 );
// ******************** Clean up WordPress Header END ********************** //

評論留言