在開發(fā)WordPress主題的過程中我們經(jīng)常發(fā)現(xiàn)有很多多余無用的WordPress自帶無效代碼。那么就分享一下本人自用的常用移除多余WordPress代碼集合。
- 原文參考:www.htm.fun/wordpress-jiao-cheng-chang-yon.html
在主題根目錄下的functions.php
文件中的<?php
下添加以下代碼并保存。
/**********************************************************************
去除多余代碼
**********************************************************************/
add_filter('show_admin_bar', '__return_false');
remove_action('wp_head', 'feed_links', 2);
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', 'parent_post_rel_link', 10, 0);
remove_action('wp_head', 'start_post_rel_link', 10, 0);
remove_action('wp_head', 'adjacent_posts_rel_link_wp_head', 10, 0);
remove_action('wp_head', 'locale_stylesheet');
remove_action('wp_head', 'noindex', 1);
remove_action('wp_head', 'wp_print_head_scripts', 9);
remove_action('wp_head', 'wp_generator');
remove_action('wp_head', 'rel_canonical');
remove_action('wp_head', 'wp_shortlink_wp_head', 10, 0);
remove_action('wp_head', 'wp_oembed_add_host_js');
remove_action('wp_head', 'wp_resource_hints', 2);
remove_action('wp_head', 'rest_output_link_wp_head', 10);
remove_action('wp_head', 'wp_oembed_add_discovery_links', 10);
remove_action('wp_footer', 'wp_print_footer_scripts');
remove_action('publish_future_post', 'check_and_publish_future_post', 10, 1);
remove_action('template_redirect', 'wp_shortlink_header', 11, 0);
remove_action('template_redirect', 'rest_output_link_header', 11, 0);
remove_action('rest_api_init', 'wp_oembed_register_route');
remove_filter('rest_pre_serve_request', '_oembed_rest_pre_serve_request', 10, 4);
remove_filter('oembed_dataparse', 'wp_filter_oembed_result', 10);
remove_filter('oembed_response_data', 'get_oembed_response_data_rich', 10, 4);
add_filter( 'pre_option_link_manager_enabled', '__return_true' );
add_filter('rest_enabled', '__return_false');
add_filter('rest_jsonp_enabled', '__return_false');
add_shortcode('reply', 'reply_to_read');
add_filter('pre_site_transient_update_core',create_function('$a',"return null;")); // 關(guān)閉核心提示
add_filter('pre_site_transient_update_plugins',create_function('$a',"return null;")); // 關(guān)閉插件提示
add_filter('pre_site_transient_update_themes',create_function('$a',"return null;")); // 關(guān)閉主題提示
remove_action('admin_init','_maybe_update_core');// 禁止 WordPress 檢查更新
remove_action('admin_init','_maybe_update_plugins');// 禁止 WordPress 更新插件
remove_action('admin_init','_maybe_update_themes'); // 禁止 WordPress 更新主題
//禁用文章自動保存
add_action('wp_print_scripts','disable_autosave');
function disable_autosave(){wp_deregister_script('autosave');
}
//禁用文章修訂版本
add_filter( 'wp_revisions_to_keep', 'specs_wp_revisions_to_keep', 10, 2 );
function specs_wp_revisions_to_keep( $num, $post ) {return 0;
}
// 阻止站內(nèi)文章互相Pingback
function theme_noself_ping( &$links ) {$home = get_theme_mod( 'home' );foreach ( $links as $l => $link )if ( 0 === strpos( $link, $home ) )unset($links[$l]);
}
add_action('pre_ping','theme_noself_ping');
好啦,這就是本人開發(fā)主題的時候自己用的代碼集合咯~如果你覺得不完善,請在下方留言,本人會完善的~