WordPress 优化代码

ugOk.gif

WordPress,全球广泛使用的知名免费开源网站博客程序。WordPress是一种使用PHP语言开发的博客平台,用户可以在支持PHP和MySQL数据库的服务器上架设自己的网站,也可以把WordPress 当作一个内容管理系统(CMS)来使用。WordPress 还是一个免费的开源项目,在GNU通用公共许可证下授权发布。

/*彻底关闭自动更新(核心程序/主题/插件/翻译自动更新*/
add_filter('automatic_updater_disabled', '__return_true');
/*关闭更新检查定时作业*/
remove_action('init', 'wp_schedule_update_checks');
/*移除已有的版本检查定时作业*/
wp_clear_scheduled_hook('wp_version_check');
/*移除已有的插件更新定时作业*/
wp_clear_scheduled_hook('wp_update_plugins');
/*移除已有的主题更新定时作业*/
wp_clear_scheduled_hook('wp_update_themes');
/*移除已有的自动更新定时作业*/
wp_clear_scheduled_hook('wp_maybe_auto_update');
/*移除后台内核更新检查*/
remove_action('admin_init', '_maybe_update_core');
/*移除后台插件更新检查*/
remove_action('load-plugins.php', 'wp_update_plugins');
remove_action('load-update.php', 'wp_update_plugins');
remove_action('load-update-core.php', 'wp_update_plugins');
remove_action('admin_init', '_maybe_update_plugins');
/*移除后台主题更新检查*/
remove_action('load-themes.php', 'wp_update_themes');
remove_action('load-update.php', 'wp_update_themes');
remove_action('load-update-core.php', 'wp_update_themes');
remove_action('admin_init', '_maybe_update_themes');
/*关闭程序更新提示*/
add_filter('pre_site_transient_update_core', function($a){ return null; });
/*关闭插件更新提示*/
add_filter('pre_site_transient_update_plugins', function($a){return null;});
/*关闭主题更新提示*/
add_filter('pre_site_transient_update_themes', function($a){return null;});
//关闭WordPress的XML-RPC功能
add_filter('xmlrpc_enabled', '__return_false');
/* 关闭XML-RPC的pingback端口 */
add_filter('xmlrpc_methods', 'remove_xmlrpc_pingback_ping');
函数 remove_xmlrpc_pingback_ping( $methods ) {
未设置($methods['pingback.ping']);
返回$方法;
}
//禁用 pingbacks, enclosures, trackbacks
remove_action('do_pings', 'do_all_pings', 10);
//去掉 _encloseme 和 do_ping 操作
remove_action('publish_post','_publish_post_hook',5);
/* 禁止加载s.w.org获取表情和头像 */
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');
函数 remove_dns_prefetch( $hints, $relation_type ) {
if ('dns-prefetch' === $relation_type) {
返回array_diff(wp_dependencies_unique_hosts(),$提示);
}
返回$提示;
}
add_filter('wp_resource_hints', 'remove_dns_prefetch', 10, 2);
/* 完全禁止REST API、移除wp-json链接 */
函数 lerm_disable_rest_api( $access ) {
返回新的 WP_Error(
'停止!',
'Soooooryyyy',
大批(
'状态' => 403,
)
);
}
add_filter('rest_authentication_errors', 'lerm_disable_rest_api');
remove_action('wp_head', 'rest_output_link_wp_head', 10);
/* 禁止查询网站静态资源连接版本字符 */
函数 _remove_script_version ( $src ){
$parts = explode('?', $src);
返回 $parts[0];
}
add_filter('script_loader_src', '_remove_script_version', 15, 1);
add_filter('style_loader_src', '_remove_script_version', 15, 1);
/* 移除前端网页源代码内的头部冗余代码 */
remove_action('wp_head', 'feed_links_extra', 3);
remove_action('wp_head', 'rsd_link');
remove_action('wp_head', 'wlwmanifest_link');
remove_action('wp_head', 'index_rel_link');
remove_action('wp_head', 'start_post_rel_link', 10, 0);
remove_action('wp_head', 'wp_generator');
/* 禁止新版文章编辑器加载前端样式 */
函数 wpassist_remove_block_library_css(){
wp_dequeue_style('wp-block-library');
}
remove_action('wp_enqueue_scripts', 'wp_common_block_scripts_and_styles');
add_action('wp_enqueue_scripts','wpassist_remove_block_library_css');
/* 移除新版本站点健康状态面板和菜单项 */
add_action('admin_menu', 'remove_site_health_menu');
功能删除站点健康菜单(){
remove_submenu_page('tools.php','site-health.php');
}
/* 禁用5.5版后自带的XML站点地图 */
add_filter('wp_sitemaps_enabled', '__return_false');
/* 移除前后台顶部工具栏指定菜单 */
功能 admin_bar_item ( WP_Admin_Bar $admin_bar ) {
$admin_bar->remove_menu('wp-logo'); //移动wp的logo
$admin_bar->remove_menu('site-name'); //移动站点名称
$admin_bar->remove_menu('updates'); //移动更新提示
$admin_bar->remove_menu('comments'); //移动评论提示
/*$admin_bar->remove_menu('new-content'); //移除新建按钮 */
}
add_action('admin_bar_menu', 'admin_bar_item', 500);
//移除后台仪表盘站点健康状态面板
add_action('wp_dashboard_setup', 'remove_site_health_dashboard_widget');
功能 remove_site_health_dashboard_widget()
{
remove_meta_box('dashboard_site_health', 'dashboard', 'normal');
}
//移除后台仪表盘菜单:站点健康状态
add_action('admin_menu', 'remove_site_health_menu');
功能删除站点健康菜单(){
remove_submenu_page('tools.php','site-health.php');
}
//移除后台仪表盘菜单:活动、新闻
功能 bzg_remove_dashboard_widgets() {
全局$wp_meta_boxes;
#移除 "活动"
未设置($wp_meta_boxes['dashboard']['normal']['core']['dashboard_activity']);
#移除 "WordPress 新闻"
未设置($wp_meta_boxes['dashboard']['side']['core']['dashboard_primary']);
}
add_action('wp_dashboard_setup', 'bzg_remove_dashboard_widgets' );
//移除后台仪表盘菜单:帮助
功能 bzg_remove_help() {
get_current_screen()->remove_help_tabs();
}
add_action('admin_head', 'bzg_remove_help');
//移除后台页面title标题的wordpress后缀
add_filter('admin_title', 'delAdminTitle', 10, 2);
函数 delAdminTitle($admin_title, $title){
返回 $title。‹ '.get_bloginfo('名称');
}
//移除登陆页面title标题的wordpress后缀
add_filter('login_title', 'remove_login_title', 10, 2);
功能 remove_login_title($login_title, $title){
返回 $title。‹ '.get_bloginfo('名称');
}
/* 彻底禁止4.4+版之后响应式图片功能及缩略图裁剪功能*/
// 禁止生成图像尺寸
功能 zm_customize_image_sizes( $sizes ){
未设置($sizes['缩略图']);
未设置($大小['中等']);
未设置($sizes['medium_large']);
未设置($sizes['大']);
未设置($sizes['full']);
未设置($尺寸['1536x1536']);
未设置($尺寸['2048x2048']);
返回$大小;
}
add_filter('intermediate_image_sizes_advanced', 'zm_customize_image_sizes');
// 禁止缩放图片尺寸
add_filter('big_image_size_threshold', '__return_false');
// 禁止生成其它图像尺寸
功能 shapeSpace_disable_other_image_sizes() {
// 禁止通过set_post_thumbnail_size()函数生成的图片尺寸
remove_image_size('后缩略图');
// 禁止添加其它图像尺寸
remove_image_size('另一个尺寸');
}
add_action('init', 'shapeSpace_disable_other_image_sizes');
//切换经典文章编辑器(v5.x开始默认古腾堡编辑器)
add_filter('use_block_editor_for_post', '__return_false');
//替换评论用户头像链接为国内镜像加速访问
add_filter('get_avatar', function ($avatar) {
返回str_replace([
'www.gravatar.com/avatar/',
'0.gravatar.com/avatar/',
'1.gravatar.com/avatar/',
'2.gravatar.com/avatar/',
'secure.gravatar.com/avatar/',
'cn.gravatar.com/avatar/'
], 'cravatar.cn/', $avatar);
});
//取消内容转义
remove_filter('the_content', 'wptexturize');
//取消摘要转义
remove_filter('the_excerpt', 'wptexturize');
//取消评论转义
remove_filter('comment_text', 'wptexturize');
//禁止转义引号字符
remove_filter('the_content', 'wptexturize'); // 禁止英文引号转义为中文引号
//文章的插件图片自动移除 img 的宽度、高度、类属性;
add_filter('post_thumbnail_html', 'fanly_remove_images_attribute', 10);
add_filter('image_send_to_editor', 'fanly_remove_images_attribute', 10);
功能 fanly_remove_images_attribute( $html ) {
//$html = preg_replace('/(width|height)="\d*"\s/', "", $html);
$html = preg_replace('/width="(\d*)"\s+height="(\d*)"\s+class="[^"]*"/', "", $html);
$html = preg_replace('/ /', "", $html);
返回 $html;
}
//(分辨率图片重新删除ludwidth和height
){
preg_match_all('/img|IMG].*?src:[\'|"](.*?[\.gif|\.jpg|\.png\.webp]))[\'|"].*? [\/]?>/', $content, $images);
if(!empty($images)) {
foreach($images[0] as $index => $value){
$new_img = preg_replace('/(width|height)="\d*"\s/', " ", $images[0][$index]);
$content = str_replace($images[0][$index], $new_img, $content);
}
}
返回$内容;
}
// 判断是否是移动设备浏览
如果(wp_is_mobile()){
#删除文章内容中img的width和height属性
add_filter('the_content', 'ludou_remove_width_height_attribute', 99);
}
/* 删除文章时删除附件 */
功能 delete_post_and_attachments($post_ID) {
全球 $wpdb;
#删除特色图片
$thumbnails = $wpdb->get_results("SELECT * FROM $wpdb->postmeta WHERE meta_key = '_thumbnail_id' AND post_id = $post_ID");
foreach ( $thumbnails 作为 $thumbnail ) {
wp_delete_attachment($thumbnail->meta_value, true);
}
#删除图片附件
$attachments = $wpdb->get_results("SELECT * FROM $wpdb->posts WHERE post_parent = $post_ID AND post_type = 'attachment'");
foreach ( $attachments 作为 $attachment ) {
wp_delete_attachment($attachment->ID, true);
}
$wpdb->query("从 $wpdb->postmeta 中删除 meta_key = '_thumbnail_id' AND post_id = $post_ID" );
add_action('before_delete_post', 'delete_post_and_attachments');

使用方法

添加优化代码到主题目录functions.php文件即可

感谢您的来访,获取更多精彩文章请收藏本站。

© 版权声明
THE END
喜欢就支持一下吧
点赞24赞赏 分享
评论 共1条
头像
欢迎提交您的评论,共创文明良好交流环境,请勿提交违规言论!
提交
头像

昵称

取消
昵称表情代码图片
    • 头像nimabi0