丝袜av在线观看|日本美女三级片在线播放|性欧美一区二区三区|小骚热免费国产视频|黑人va在线观看|女同国产91视频|五月丁香色播Av|国产凸凹视频一区二区|伊人电影久久99|国产成人无码一区二区观看

WordPress實用代碼大全,提升網(wǎng)站功能與效率

來自:素雅營銷研究院

頭像 方知筆記
2025年04月24日 04:17

一、WordPress常用功能代碼片段

WordPress作為最受歡迎的內(nèi)容管理系統(tǒng),通過一些實用代碼可以擴(kuò)展其功能而無需安裝額外插件。以下是幾個常用的代碼片段:

  1. 禁用自動保存功能
add_action('admin_init', 'disable_autosave');
function disable_autosave() {
wp_deregister_script('autosave');
}
  1. 移除WordPress版本號(增強(qiáng)安全性):
remove_action('wp_head', 'wp_generator');
  1. 限制文章修訂版本數(shù)量
define('WP_POST_REVISIONS', 3); // 只保留3個修訂版本

二、優(yōu)化SEO的實用代碼

  1. 自動為圖片添加alt屬性
add_filter('the_content', 'auto_image_alt');
function auto_image_alt($content) {
global $post;
preg_match_all('/<img (.*?)\/>/', $content, $images);
if(!is_null($images)) {
foreach($images[1] as $index => $value) {
if(!preg_match('/alt=/', $value)) {
$new_img = str_replace('<img', '<img alt="'.$post->post_title.'"', $images[0][$index]);
$content = str_replace($images[0][$index], $new_img, $content);
}
}
}
return $content;
}
  1. 自動為外部鏈接添加nofollow屬性
add_filter('the_content', 'auto_nofollow');
function auto_nofollow($content) {
return preg_replace_callback('/<a[^>]+/', 'auto_nofollow_callback', $content);
}

function auto_nofollow_callback($matches) {
$link = $matches[0];
$site_link = get_bloginfo('url');
if (strpos($link, 'rel') === false) {
$link = preg_replace("%(href=\S(?!$site_link))%i", 'rel="nofollow" $1', $link);
} elseif (preg_match("%href=\S(?!$site_link)%i", $link)) {
$link = preg_replace('/rel=\S(?!nofollow)\S*/i', 'rel="nofollow"', $link);
}
return $link;
}

三、提升網(wǎng)站性能的代碼

  1. 延遲加載圖片
add_filter('the_content', 'lazy_load_content_images');
function lazy_load_content_images($content) {
return preg_replace('/<img([^>]*)src=/i', '<img$1src="data:image/gif;base64,R0lGODdhAQABAPAAAP///wAAACwAAAAAAQABAEACAkQBADs=" data-src=', $content);
}

add_action('wp_footer', 'lazy_load_scripts');
function lazy_load_scripts() {
?>
<script>
document.addEventListener("DOMContentLoaded", function() {
var lazyImages = [].slice.call(document.querySelectorAll("img[data-src]"));
if ("IntersectionObserver" in window) {
let lazyImageObserver = new IntersectionObserver(function(entries, observer) {
entries.forEach(function(entry) {
if (entry.isIntersecting) {
let lazyImage = entry.target;
lazyImage.src = lazyImage.dataset.src;
lazyImageObserver.unobserve(lazyImage);
}
});
});
lazyImages.forEach(function(lazyImage) {
lazyImageObserver.observe(lazyImage);
});
}
});
</script>
<?php
}
  1. 禁用Emoji表情(減少HTTP請求):
remove_action('wp_head', 'print_emoji_detection_script', 7);
remove_action('admin_print_scripts', 'print_emoji_detection_script');
remove_action('wp_print_styles', 'print_emoji_styles');
remove_action('admin_print_styles', 'print_emoji_styles');

四、增強(qiáng)安全性的代碼

  1. 限制登錄嘗試次數(shù)
add_filter('authenticate', 'check_attempted_login', 30, 3);
function check_attempted_login($user, $username, $password) {
if (get_transient('attempted_login')) {
$datas = get_transient('attempted_login');
if ($datas['tried'] >= 3) {
$until = get_option('_transient_timeout_'.'attempted_login');
$time = time_to_go($until);
return new WP_Error('too_many_tried', sprintf(__('<strong>錯誤</strong>: 您已嘗試登錄太多次,請%s后再試。'), $time));
}
}
return $user;
}

add_action('wp_login_failed', 'login_failed');
function login_failed($username) {
if (get_transient('attempted_login')) {
$datas = get_transient('attempted_login');
$datas['tried']++;
set_transient('attempted_login', $datas, 300);
} else {
$datas = array(
'tried' => 1
);
set_transient('attempted_login', $datas, 300);
}
}
  1. 禁用XML-RPC接口(防止暴力攻擊):
add_filter('xmlrpc_enabled', '__return_false');

五、實用小工具代碼

  1. 顯示當(dāng)前模板路徑(開發(fā)調(diào)試用):
add_action('wp_footer', 'show_template');
function show_template() {
if(is_super_admin()) {
global $template;
echo '<div style="position:fixed;bottom:10px;right:10px;background:#fff;padding:5px;z-index:9999;font-size:12px;">'.basename($template).'</div>';
}
}
  1. 在文章末尾自動添加版權(quán)信息
add_filter('the_content', 'add_copyright');
function add_copyright($content) {
if(is_single()) {
$content .= '<div class="copyright-notice">本文首發(fā)于<a href="'.get_bloginfo('url').'">'.get_bloginfo('name').'</a>,轉(zhuǎn)載請注明出處</div>';
}
return $content;
}

六、代碼使用方法

要將這些代碼添加到您的WordPress網(wǎng)站,有幾種方法:

  1. 使用子主題的functions.php文件 - 這是最推薦的方法
  2. 使用代碼片段插件 - 如Code Snippets插件
  3. 創(chuàng)建自定義插件 - 對于更復(fù)雜的代碼

注意事項

  • 添加代碼前務(wù)必備份網(wǎng)站
  • 一次只添加一個代碼片段并測試效果
  • 某些代碼可能需要根據(jù)您的具體需求進(jìn)行調(diào)整
  • 定期檢查代碼兼容性,特別是WordPress更新后

通過合理使用這些WordPress實用代碼,您可以顯著提升網(wǎng)站的功能性、性能和安全性,同時減少對插件的依賴,使網(wǎng)站運行更加高效穩(wěn)定。