10個實用的WordPress程式碼片段

10個實用的WordPress程式碼片段

WordPress 最棒的地方之一就是可以自定義各個方面。您可以定製的東西太多了,很難知道從哪裡開始,尤其是如果您是 WordPress 的新手。我們為您提供了一些我們最喜歡的 WordPress 程式碼片段,供您在網站上使用。

您可以使用 Code Snippets 外掛或在主題的 functions.php 中新增這些程式碼片段。本文底部還將向你展示如何在 WordPress 中新增程式碼片段

下面是一些有用的 WordPress 程式碼片段:

1. 禁用管理工具欄

有些快取系統要求您不要為登入使用者和公共使用者設定不同的程式碼,因此禁用 WordPress 管理工具欄在這種情況下非常有用。

如果你想在所有頁面上為登入使用者禁用 WordPress 管理工具欄,請在主題 functions.php 中使用以下程式碼段。

Plain text
Copy to clipboard
Open code in new window
EnlighterJS 3 Syntax Highlighter
<?php
//Disable WordPress admin bar for all logged in users
add_filter('show_admin_bar', '__return_false');
<?php //Disable WordPress admin bar for all logged in users add_filter('show_admin_bar', '__return_false');
<?php
//Disable WordPress admin bar for all logged in users
add_filter('show_admin_bar', '__return_false');

2. 在 RSS 源中顯示文章縮圖

預設情況下,WordPress 只會在 RSS 源中顯示文字,但如果您想在 RSS 源中顯示您設定的特色圖片,此程式碼段就可以實現這一功能。

這將在網站 RSS 源的內容之前新增文章的特色縮圖。

Plain text
Copy to clipboard
Open code in new window
EnlighterJS 3 Syntax Highlighter
<?php
//This will prepend your WordPress RSS feed content with the featured image
add_filter('the_content', 'smartwp_featured_image_in_rss_feed');
function smartwp_featured_image_in_rss_feed( $content ) {
global $post;
if( is_feed() ) {
if ( has_post_thumbnail( $post->ID ) ){
$prepend = '<div>' . get_the_post_thumbnail( $post->ID, 'medium', array( 'style' => 'margin-bottom: 10px;' ) ) . '</div>';
$content = $prepend . $content;
}
}
return $content;
}
<?php //This will prepend your WordPress RSS feed content with the featured image add_filter('the_content', 'smartwp_featured_image_in_rss_feed'); function smartwp_featured_image_in_rss_feed( $content ) { global $post; if( is_feed() ) { if ( has_post_thumbnail( $post->ID ) ){ $prepend = '<div>' . get_the_post_thumbnail( $post->ID, 'medium', array( 'style' => 'margin-bottom: 10px;' ) ) . '</div>'; $content = $prepend . $content; } } return $content; }
<?php
//This will prepend your WordPress RSS feed content with the featured image
add_filter('the_content', 'smartwp_featured_image_in_rss_feed');
function smartwp_featured_image_in_rss_feed( $content ) {
 global $post;
 if( is_feed() ) {
 if ( has_post_thumbnail( $post->ID ) ){
 $prepend = '<div>' . get_the_post_thumbnail( $post->ID, 'medium', array( 'style' => 'margin-bottom: 10px;' ) ) . '</div>';
 $content = $prepend . $content;
 }
 }
 return $content;
}

3. 更改摘要的 “Read More” 文字

無論您是想更改 WordPress 中的 “Continue Reading” 文字,還是想用 HTML 將其變成一個按鈕,下面的程式碼段都能讓您更改摘要的 “Read more”。

Plain text
Copy to clipboard
Open code in new window
EnlighterJS 3 Syntax Highlighter
<?php
// Changing excerpt more
function smartwp_change_excerpt_more_text( $more ){
global $post;
return '&hellip; <a class="read-more" href="'.get_permalink($post->ID).'" title="'.esc_attr(get_the_title($post->ID)).'">'.'Read More &raquo;'.'</a>';
}
add_filter('excerpt_more', 'smartwp_change_excerpt_more_text');
<?php // Changing excerpt more function smartwp_change_excerpt_more_text( $more ){ global $post; return '&hellip; <a class="read-more" href="'.get_permalink($post->ID).'" title="'.esc_attr(get_the_title($post->ID)).'">'.'Read More &raquo;'.'</a>'; } add_filter('excerpt_more', 'smartwp_change_excerpt_more_text');
<?php
// Changing excerpt more
function smartwp_change_excerpt_more_text( $more ){
 global $post;
 return '&hellip; <a class="read-more" href="'.get_permalink($post->ID).'" title="'.esc_attr(get_the_title($post->ID)).'">'.'Read More &raquo;'.'</a>';
}
add_filter('excerpt_more', 'smartwp_change_excerpt_more_text');

4. 更改文章摘要長度

預設情況下,WordPress 中的摘要長度為 55 個字。此程式碼片段將把摘要長度改為 24 個字,但您也可以根據自己的需要隨意調整。

Plain text
Copy to clipboard
Open code in new window
EnlighterJS 3 Syntax Highlighter
<?php
//Change the default excerpt length in WordPress (default is 55 words)
function smartwp_change_excerpt_length( $length ) {
return 24;
}
add_filter( 'excerpt_length', 'smartwp_change_excerpt_length', 9999);
<?php //Change the default excerpt length in WordPress (default is 55 words) function smartwp_change_excerpt_length( $length ) { return 24; } add_filter( 'excerpt_length', 'smartwp_change_excerpt_length', 9999);
<?php
//Change the default excerpt length in WordPress (default is 55 words)
function smartwp_change_excerpt_length( $length ) {
 return 24;
}
add_filter( 'excerpt_length', 'smartwp_change_excerpt_length', 9999);

5. 用 PHP 新增管理員使用者

面對現實,我們都曾被鎖定在 WordPress 網站之外,或者不得不在沒有登入資訊的情況下在新網站上工作。

這段程式碼可以使用主題的 function.php 為網站新增一個新的管理員。

我曾在很多 WordPress 網站上工作過,這些網站沒有正確配置電子郵件伺服器,因此即使有登入資訊也很難訪問網站。

該程式碼段將使用變數中設定的使用者名稱/密碼/電子郵件建立一個使用者。值得注意的是,它只會在使用者名稱/電子郵件不存在的情況下嘗試建立使用者,因此如果你已經有一個帶有電子郵件地址的賬戶,你可以用虛擬資料填寫電子郵件。

Plain text
Copy to clipboard
Open code in new window
EnlighterJS 3 Syntax Highlighter
<?php
//Create an admin user
function smartwp_create_admin_user(){
$username = 'yourusername';
$password = '2JyAEQJ9B9Jf5T8a';
$email = 'change@me.com';
//This will ensure it only tries to create the user once (based on email/username)
if ( !username_exists( $username ) && !email_exists( $email ) ) {
$userid = wp_create_user( $username, $password, $email );
$user = new WP_User( $userid );
$user->set_role( 'administrator' );
}
}
add_action('init', 'smartwp_create_admin_user');
<?php //Create an admin user function smartwp_create_admin_user(){ $username = 'yourusername'; $password = '2JyAEQJ9B9Jf5T8a'; $email = 'change@me.com'; //This will ensure it only tries to create the user once (based on email/username) if ( !username_exists( $username ) && !email_exists( $email ) ) { $userid = wp_create_user( $username, $password, $email ); $user = new WP_User( $userid ); $user->set_role( 'administrator' ); } } add_action('init', 'smartwp_create_admin_user');
<?php
//Create an admin user
function smartwp_create_admin_user(){
 $username = 'yourusername';
 $password = '2JyAEQJ9B9Jf5T8a';
 $email = 'change@me.com';
 //This will ensure it only tries to create the user once (based on email/username)
 if ( !username_exists( $username ) && !email_exists( $email ) ) {
 $userid = wp_create_user( $username, $password, $email );
 $user = new WP_User( $userid );
 $user->set_role( 'administrator' );
 }
}
add_action('init', 'smartwp_create_admin_user');

6. 在文字小工具中啟用簡碼

簡碼在 WordPress 中非常強大,在部件中使用它們很有幫助。此程式碼段將允許您在文字小工具中新增簡碼並執行它們。

Plain text
Copy to clipboard
Open code in new window
EnlighterJS 3 Syntax Highlighter
<?php
//Enable shortcodes in text widgets
add_filter('widget_text', 'do_shortcode');
<?php //Enable shortcodes in text widgets add_filter('widget_text', 'do_shortcode');
<?php
//Enable shortcodes in text widgets
add_filter('widget_text', 'do_shortcode');

7. 新增自定義儀表盤 Logo

如果您想為 WordPress 網站新增更多品牌標識,此程式碼段將替換儀表盤左上角的 Logo。

自定義儀表盤 Logo

確保上傳 admin-icon.png 到主題目錄。您還可以更改 CSS,在 background-image 屬性中連結到任何檔案。

Plain text
Copy to clipboard
Open code in new window
EnlighterJS 3 Syntax Highlighter
<?php
//Adds a custom logo to the top left of the WordPress admin
function smartwp_custom_logo_wp_dashboard() {
echo "<style type='text/css'>
#wpadminbar #wp-admin-bar-wp-logo > .ab-item .ab-icon:before {
background-image: url('" . get_bloginfo('stylesheet_directory') . "/admin-icon.png');
background-size: contain;
background-position: 0 0;
color:rgba(0, 0, 0, 0);
}
#wpadminbar #wp-admin-bar-wp-logo > .ab-item .ab-icon {
background-position: 0 0;
}
</style>";
}
add_action('wp_before_admin_bar_render', 'smartwp_custom_logo_wp_dashboard');
<?php //Adds a custom logo to the top left of the WordPress admin function smartwp_custom_logo_wp_dashboard() { echo "<style type='text/css'> #wpadminbar #wp-admin-bar-wp-logo > .ab-item .ab-icon:before { background-image: url('" . get_bloginfo('stylesheet_directory') . "/admin-icon.png'); background-size: contain; background-position: 0 0; color:rgba(0, 0, 0, 0); } #wpadminbar #wp-admin-bar-wp-logo > .ab-item .ab-icon { background-position: 0 0; } </style>"; } add_action('wp_before_admin_bar_render', 'smartwp_custom_logo_wp_dashboard');
<?php
//Adds a custom logo to the top left of the WordPress admin
function smartwp_custom_logo_wp_dashboard() {
 echo "<style type='text/css'>
 #wpadminbar #wp-admin-bar-wp-logo > .ab-item .ab-icon:before {
 background-image: url('" . get_bloginfo('stylesheet_directory') . "/admin-icon.png');
 background-size: contain;
 background-position: 0 0;
 color:rgba(0, 0, 0, 0);
 }
 #wpadminbar #wp-admin-bar-wp-logo > .ab-item .ab-icon {
 background-position: 0 0;
 }
 </style>";
}
add_action('wp_before_admin_bar_render', 'smartwp_custom_logo_wp_dashboard');

8. 允許上傳 SVG

SVG 格式越來越受歡迎,尤其是徽標檔案。當然,您可以使用外掛或程式碼片段來啟用這一功能。

出於安全考慮,WordPress 預設不允許上傳 SVG,但我們的程式碼片段只允許網站管理員上傳 SVG 檔案。

Plain text
Copy to clipboard
Open code in new window
EnlighterJS 3 Syntax Highlighter
<?php
//Enable SVG upload
function smartwp_enable_svg_upload( $mimes ) {
//Only allow SVG upload by admins
if ( !current_user_can( 'administrator' ) ) {
return $mimes;
}
$mimes['svg'] = 'image/svg+xml';
$mimes['svgz'] = 'image/svg+xml';
return $mimes;
}
add_filter('upload_mimes', 'smartwp_enable_svg_upload');
<?php //Enable SVG upload function smartwp_enable_svg_upload( $mimes ) { //Only allow SVG upload by admins if ( !current_user_can( 'administrator' ) ) { return $mimes; } $mimes['svg'] = 'image/svg+xml'; $mimes['svgz'] = 'image/svg+xml'; return $mimes; } add_filter('upload_mimes', 'smartwp_enable_svg_upload');
<?php
//Enable SVG upload
function smartwp_enable_svg_upload( $mimes ) {
 //Only allow SVG upload by admins
 if ( !current_user_can( 'administrator' ) ) {
 return $mimes;
 }
 $mimes['svg'] = 'image/svg+xml';
 $mimes['svgz'] = 'image/svg+xml';
 
 return $mimes;
}
add_filter('upload_mimes', 'smartwp_enable_svg_upload');

9. 在 WordPress 中禁用 XML-RPC

你很少需要在 WordPress 網站上啟用 XML-RPC,但啟用它可能會導致一系列安全問題。

如果你使用 WordPress 應用程式,你可能需要繼續啟用它,但我很少看到啟用 XML-RPC 的情況。

本程式碼段將禁用 XML-RPC,以提高網站安全性。

Plain text
Copy to clipboard
Open code in new window
EnlighterJS 3 Syntax Highlighter
<?php
//Disable XML-RPC
add_filter('xmlrpc_enabled', '__return_false');
<?php //Disable XML-RPC add_filter('xmlrpc_enabled', '__return_false');
<?php
//Disable XML-RPC
add_filter('xmlrpc_enabled', '__return_false');

10. 移除 jQuery Migrate

如果您正試圖提高網站效能,您可能已經注意到 jQuery Migrate 正在您的網站上載入。如果開啟控制檯,你會看到 “JQMIGRATE: Migrate is installed, version 1.4.1″。

jQuery Migrate

jQuery Migrate 增加了對舊版本 jQuery 的支援,通常對舊主題很有用。根據我的經驗,很少需要它,所以最好將其移除,以減少頁面載入時的一個請求。

下面的程式碼段將移除網站上的  jQuery Migrate。移除 jQuery Migrate 後,檢視幾個頁面,確保網站仍能正常執行。

Plain text
Copy to clipboard
Open code in new window
EnlighterJS 3 Syntax Highlighter
<?php
//Remove jQuery migrate
function smartwp_remove_jquery_migrate( $scripts ) {
if ( !is_admin() && !empty( $scripts->registered['jquery'] ) ) {
$scripts->registered['jquery']->deps = array_diff( $scripts->registered['jquery']->deps, ['jquery-migrate'] );
}
}
add_action('wp_default_scripts', 'smartwp_remove_jquery_migrate');
<?php //Remove jQuery migrate function smartwp_remove_jquery_migrate( $scripts ) { if ( !is_admin() && !empty( $scripts->registered['jquery'] ) ) { $scripts->registered['jquery']->deps = array_diff( $scripts->registered['jquery']->deps, ['jquery-migrate'] ); } } add_action('wp_default_scripts', 'smartwp_remove_jquery_migrate');
<?php
//Remove jQuery migrate
function smartwp_remove_jquery_migrate( $scripts ) {
 if ( !is_admin() && !empty( $scripts->registered['jquery'] ) ) {
 $scripts->registered['jquery']->deps = array_diff( $scripts->registered['jquery']->deps, ['jquery-migrate'] );
 }
}
add_action('wp_default_scripts', 'smartwp_remove_jquery_migrate');

注:您可以通過安裝 WPTurbo 外掛快速實現上述諸如禁用 emoji、禁用前端管理員欄、禁用 XML-RPC 和移除 jquery migrate 等程式碼片段功能,而無需向WordPress新增程式碼片段或者安裝程式碼片段外掛實現。

如何在 WordPress 中新增程式碼片段

如果您需要幫助在 WordPress 中新增 PHP 程式碼片段最簡單直接的方法是通過 Code Snippets 外掛。

向 WordPress 網站新增程式碼的最簡單方法之一就是使用 Code Snippets 外掛。

這個 WordPress 外掛能讓你在一個簡單易用的介面上輕鬆組織和新增程式碼片段。

Code Snippets 外掛介面

Code Snippets 外掛介面

最重要的是,你可以輕鬆選擇程式碼片段的執行位置,並將其關閉或開啟。非常適合 PHP 程式碼片段初學者。

感謝您檢視這些實用的 WordPress 程式碼片段。這些程式碼片段可以新增到主題的 function.php 中,也可以使用 Code Snippets 這樣的外掛新增。

評論留言