本文最后更新于 1313 天前,其中的信息可能已经有所发展或是发生改变。
通常下载的一些博客主题中,已经有了这些优化,请忽略本文,本文针对自己开发的主题、网站模板转wordpress的主题。
将以下代码添加进主题目录中的functions.php文件末尾的” ?> “之前
使用经典编辑器
wordpress加入的可视化编辑器,加载缓慢,使用起来不太适应,可以禁用而使用经典编辑器。
add_filter('use_block_editor_for_post','__return_false');
网页源码中js、css、feed等移除
以下内容默认会出现在网页源码中,因为wordpress会调用自己的js、css文件。
add_action('admin_init', 'ssid_add');
remove_action( 'wp_head', 'wp_generator' ); //移除WordPress版本
remove_action( 'wp_head', 'wp_enqueue_scripts', 1 ); //移出wordpress的js调用
remove_action( 'wp_head', 'rsd_link' ); //移除离线编辑器开放接口
remove_action( 'wp_head', 'wlwmanifest_link' ); //移除离线编辑器开放接口
remove_action( 'wp_head', 'wp_print_styles', 8 );//载入css
add_filter( 'embed_oembed_discover', '__return_false' );// 禁用oEmbed 发现功能
remove_filter( 'oembed_dataparse', 'wp_filter_oembed_result', 10 );// 移除oEmbed 结果
remove_action( 'wp_head', 'wp_oembed_add_discovery_links' );// 移除oEmbed 链接
remove_action( 'wp_head', 'wp_oembed_add_host_js' );// 移除oEmbed 使用的 JavaScript 文件
移除自动更新提示
wordpress的更新检查由于指向墙外,所以难免加载非常之慢。我的做法是移除wordpress的自动更新。
add_filter('automatic_updater_disabled', '__return_true');// 彻底关闭自动更新
禁用后台谷歌字体
移除后台谷歌字体,一定程度上提升了网站后台响应速度。
add_filter( 'gettext_with_context', 'wpdx_disable_open_sans', 888, 4 );
function wpdx_disable_open_sans( $translations, $text, $context, $domain ) {
if ( 'Open Sans font: on or off' == $context && 'on' == $text ) {
$translations = 'off';
}
return $translations;
}
function remove_open_sans() {
wp_deregister_style( 'open-sans' );
wp_register_style( 'open-sans', false );
wp_enqueue_style('open-sans','');
}
add_action( 'init', 'remove_open_sans' );
wordpress后台功能移除
wordpress一些功能,如果你用不到,可以移除他。
function disable_dashboard_widgets() {
remove_meta_box('dashboard_recent_comments', 'dashboard', 'normal');//近期评论
remove_meta_box('dashboard_recent_drafts', 'dashboard', 'normal');//近期草稿
remove_meta_box('dashboard_primary', 'dashboard', 'core');//wordpress博客
remove_meta_box('dashboard_secondary', 'dashboard', 'core');//wordpress其它新闻
remove_meta_box('dashboard_right_now', 'dashboard', 'core');//wordpress概况
remove_meta_box('dashboard_incoming_links', 'dashboard', 'core');//wordresss链入链接
remove_meta_box('dashboard_plugins', 'dashboard', 'core');//wordpress链入插件
remove_meta_box('dashboard_quick_press', 'dashboard', 'core');//wordpress快速发布
}
add_action('admin_menu', 'disable_dashboard_widgets');
wordpress后台菜单隐藏
如果你的主题中用不到评论功能,你又是个强迫症,那么你可以隐藏它。(只是隐藏)
add_action('admin_menu', 'remove_menus', 102);
function remove_menus()
{
global $submenu;
remove_menu_page( 'edit-comments.php' ); // Comments
remove_submenu_page ( 'index.php', 'update-core.php' ); //Dashboard->Updates
}
WordPress API自动推送百度站长
需要注册百度站长,并添加自己的网站,获取到api推送的token代码
if(!function_exists('Baidu_Submit')){
function Baidu_Submit($post_ID) {
$WEB_TOKEN = '这里填入自己的token值'; //这里请换成你的网站的百度主动推送的token值
$WEB_DOMAIN = get_option('home');
//已成功推送的文章不再推送
if(get_post_meta($post_ID,'Baidusubmit',true) == 1) return;
$url = get_permalink($post_ID);
$api = 'http://data.zz.baidu.com/urls?site='.$WEB_DOMAIN.'&token='.$WEB_TOKEN;
$request = new WP_Http;
$result = $request->request( $api , array( 'method' => 'POST', 'body' => $url , 'headers' => 'Content-Type: text/plain') );
$result = json_decode($result['body'],true);
//如果推送成功则在文章新增自定义栏目Baidusubmit,值为1
if (array_key_exists('success',$result)) {
add_post_meta($post_ID, 'Baidusubmit', 1, true);
}
}
add_action('publish_post', 'Baidu_Submit', 0);
}







