WordPress程序/functions.php收集整理的各种自定义优化代码分享

  • A+
所属分类:WordPress技巧
  1. <!--以下代码为主题目录下functions.php文件内代码!-->

  2. add_filter('automatic_updater_disabled', '__return_true');

  3. remove_action('init', 'wp_schedule_update_checks');

  4. wp_clear_scheduled_hook('wp_version_check');

  5. wp_clear_scheduled_hook('wp_update_plugins');

  6. wp_clear_scheduled_hook('wp_update_themes');

  7. wp_clear_scheduled_hook('wp_maybe_auto_update');

  8. remove_action( 'admin_init', '_maybe_update_core' );

  9. remove_action( 'load-plugins.php', 'wp_update_plugins' );

  10. remove_action( 'load-update.php', 'wp_update_plugins' );

  11. remove_action( 'load-update-core.php', 'wp_update_plugins' );

  12. remove_action( 'admin_init', '_maybe_update_plugins' );

  13. remove_action( 'load-themes.php', 'wp_update_themes' );

  14. remove_action( 'load-update.php', 'wp_update_themes' );

  15. remove_action( 'load-update-core.php', 'wp_update_themes' );

  16. remove_action( 'admin_init', '_maybe_update_themes' );

  17. remove_action( 'wp_head', 'rest_output_link_wp_head', 10 );

  18. remove_action( 'wp_head', 'wp_oembed_add_discovery_links', 10 );

  19. remove_action( 'template_redirect', 'rest_output_link_header', 11 );

  20. add_filter('pre_site_transient_update_core', create_function('$a', "return null;"));

  21. add_filter('pre_site_transient_update_plugins', create_function('$a', "return null;"));

  22. add_filter('pre_site_transient_update_themes', create_function('$a', "return null;"));

  23. function bf_google_font($content)

  24. {

  25. return str_replace('fonts.googleapis.com', 'fonts.useso.com', $content);

  26. return str_replace('ajax.googleapis.com', 'ajax.useso.com', $content);

  27. }

  28. add_filter('xmlrpc_enabled', '__return_false');

  29. add_filter('xmlrpc_methods', 'remove_xmlrpc_pingback_ping');

  30. function remove_xmlrpc_pingback_ping( $methods ) {

  31. unset( $methods['pingback.ping'] );

  32. return $methods;

  33. }

  34. function wp_remove_open_sans_from_wp_core() {

  35. wp_deregister_style( 'open-sans' );

  36. wp_register_style( 'open-sans', false );

  37. wp_enqueue_style('open-sans','');

  38. }

  39. add_action( 'init', 'wp_remove_open_sans_from_wp_core' );

  40. class Disable_Google_Fonts {

  41. public function __construct() {

  42. add_filter( 'gettext_with_context', array( $this, 'disable_open_sans' ), 888, 4 );

  43. }

  44. public function disable_open_sans( $translations, $text, $context, $domain ) {

  45. if ( 'Open Sans font: on or off' == $context && 'on' == $text ) {

  46. $translations = 'off';

  47. }

  48. return $translations;

  49. }

  50. }

  51. $disable_google_fonts = new Disable_Google_Fonts;

  52. add_filter('json_enabled', '__return_false');

  53. add_filter('json_jsonp_enabled', '__return_false');

  54. function no_self_ping( &$links ) {

  55. $home = get_option( 'home' );

  56. foreach ( $links as $l => $link )

  57. if ( 0 === strpos( $link, $home ) )

  58. unset($links[$l]);

  59. }

  60. add_action( 'pre_ping', 'no_self_ping' );

  61. add_filter('xmlrpc_enabled', '__return_false');

  62. add_filter('xmlrpc_methods', 'remove_xmlrpc_pingback_ping');

  63. function remove_xmlrpc_pingback_ping( $methods ) {

  64. unset( $methods['pingback.ping'] );

  65. return $methods;

  66. }

  67. function mytheme_get_avatar($avatar) {

  68. $avatar = str_replace(array("www.gravatar.com","0.gravatar.com","1.gravatar.com","2.gravatar.com"),"gravatar.duoshuo.com",$avatar);

  69. return $avatar;

  70. }

  71. add_filter( 'get_avatar', 'mytheme_get_avatar', 10, 3 );

  72. function my_modify_jquery() {

  73. if (!is_admin()) {

  74. wp_deregister_script('jquery');

  75. wp_deregister_script('jquery-form');

  76. wp_register_script('jquery', ('https://cdn.bootcss.com/jquery/2.2.4/jquery.min.js'), false, null, true);

  77. wp_register_script('jquery-form', ('https://cdn.bootcss.com/jquery.form/4.2.2/jquery.form.min.js'), array(jquery), null, true);

  78. }

  79. }

  80. add_filter('get_avatar', function ($avatar) {

  81. return str_replace([

  82. 'www.gravatar.com/avatar/',

  83. '0.gravatar.com/avatar/',

  84. '1.gravatar.com/avatar/',

  85. '2.gravatar.com/avatar/',

  86. 'secure.gravatar.com/avatar/',

  87. 'cn.gravatar.com/avatar/'

  88. ], 'sdn.geekzu.org/avatar/', $avatar);

  89. });

  90. function disable_embeds_init() {

  91. global $wp;

  92. $wp->public_query_vars = array_diff( $wp->public_query_vars, array(

  93. 'embed',

  94. ) );

  95. remove_action( 'rest_api_init', 'wp_oembed_register_route' );

  96. add_filter( 'embed_oembed_discover', '__return_false' );

  97. remove_filter( 'oembed_dataparse', 'wp_filter_oembed_result', 10 );

  98. remove_action( 'wp_head', 'wp_oembed_add_discovery_links' );

  99. remove_action( 'wp_head', 'wp_oembed_add_host_js' );

  100. add_filter( 'tiny_mce_plugins', 'disable_embeds_tiny_mce_plugin' );

  101. add_filter( 'rewrite_rules_array', 'disable_embeds_rewrites' );

  102. }

  103. add_action( 'init', 'disable_embeds_init', 9999 );

  104. function disable_embeds_tiny_mce_plugin( $plugins ) {

  105. return array_diff( $plugins, array( 'wpembed' ) );

  106. }

  107. function disable_embeds_rewrites( $rules ) {

  108. foreach ( $rules as $rule => $rewrite ) {

  109. if ( false !== strpos( $rewrite, 'embed=true' ) ) {

  110. unset( $rules[ $rule ] );

  111. }

  112. }

  113. return $rules;

  114. }

  115. function disable_embeds_remove_rewrite_rules() {

  116. add_filter( 'rewrite_rules_array', 'disable_embeds_rewrites' );

  117. flush_rewrite_rules();

  118. }

  119. register_activation_hook( __FILE__, 'disable_embeds_remove_rewrite_rules' );

  120. function disable_embeds_flush_rewrite_rules() {

  121. remove_filter( 'rewrite_rules_array', 'disable_embeds_rewrites' );

  122. flush_rewrite_rules();

  123. }

  124. register_deactivation_hook( __FILE__, 'disable_embeds_flush_rewrite_rules' );

  125. add_filter( 'automatic_updater_disabled', '__return_true' );

  126. function remove_dns_prefetch( $hints, $relation_type ) {

  127. if ( 'dns-prefetch' === $relation_type ) {

  128. return array_diff( wp_dependencies_unique_hosts(), $hints );

  129. }

  130. return $hints;

  131. }

  132. add_filter( 'wp_resource_hints', 'remove_dns_prefetch', 10, 2 );

  133. function hc_cdn_callback($buffer) {

  134. return str_replace('ajax.googleapis.com/ajax/libs/jqueryui/1.8.21/themes/smoothness/jquery-ui.css', 'cdn.bootcss.com/jqueryui/1.12.1/jquery-ui.min.css', $buffer);

  135. }

  136. function hc_buffer_start() {

  137. ob_start("hc_cdn_callback");

  138. }

  139. function izt_buffer_end() {

  140. ob_end_flush();

  141. }

  142. add_action('init', 'hc_buffer_start');

  143. add_action('shutdown', 'hc_buffer_end');

  144. add_action( 'init', 'my_deregister_heartbeat', 1 );

  145. function my_deregister_heartbeat() {

  146. global $pagenow;

  147. if ( 'post.php' != $pagenow && 'post-new.php' != $pagenow )

  148. wp_deregister_script('heartbeat');

  149. }

  150. add_action('init', 'wpdaxue_change_author_base');

  151. function wpdaxue_change_author_base() {

  152. global $wp_rewrite;

  153. $author_slug = '500';

  154. $wp_rewrite->author_base = $author_slug;

  155. }

  156. add_filter( 'author_link', 'modify_author_link', 10, 1 );

  157. function modify_author_link( $link ) {

  158. $link = 'https://www.baidu.com/';

  159. return $link;

  160. }

  161. add_filter('use_block_editor_for_post', '__return_false');

  162. remove_action('wp_enqueue_scripts', 'wp_common_block_scripts_and_styles');

  163. if( wb_opt('gutenberg_switch') ) {

  164. add_filter('use_block_editor_for_post_type',function($is_user,$post_type){return false;},10,2);

  165. add_action( 'wp_enqueue_scripts', 'tonjay_remove_block_library_css', 100 );

  166. function tonjay_remove_block_library_css() {

  167. wp_dequeue_style( 'wp-block-library' );

  168. }

  169. }

  170. add_filter('the_content','the_content_nofollow',999);

  171. function the_content_nofollow($content){

  172. preg_match_all('/href="/go.php?url=KC4qPyk=" rel="external nofollow" /',$content,$matches);

  173. if($matches){

  174. foreach($matches[1] as $val){

  175. if( strpos($val,home_url())===false ) $content=str_replace("href=\"$val\"", "href=\"$val\" rel=\"external nofollow\" ",$content);

  176. }

  177. }

  178. return $content;

  179. }

  180. if(!function_exists('Baidu_Submit')){

  181. function Baidu_Submit($post_ID) {

  182. $WEB_TOKEN = '';

  183. $WEB_DOMAIN = get_option('home');

  184. if(get_post_meta($post_ID,'Baidusubmit',true) == 1) return;

  185. $url = get_permalink($post_ID);

  186. $api = 'http://data.zz.baidu.com/urls?site='.$WEB_DOMAIN.'&token='.$WEB_TOKEN;

  187. $request = new WP_Http;

  188. $result = $request->request( $api , array( 'method' => 'POST', 'body' => $url , 'headers' => 'Content-Type: text/plain') );

  189. $result = json_decode($result['body'],true);

  190. if (array_key_exists('success',$result)) {

  191. add_post_meta($post_ID, 'Baidusubmit', 1, true);

  192. }

  193. }

  194. add_action('publish_post', 'Baidu_Submit', 0);

  195. }

  196. add_filter( 'post_thumbnail_html', 'fanly_remove_images_attribute', 10 );

  197. add_filter( 'image_send_to_editor', 'fanly_remove_images_attribute', 10 );

  198. function fanly_remove_images_attribute( $html ) {

  199. $html = preg_replace( '/width="(\d*)"\s+height="(\d*)"\s+class="[^"]*"/', "", $html );

  200. $html = preg_replace( '/ /', "", $html );

  201. return $html;

  202. }

  203. add_action( 'admin_menu', 'remove_site_health_menu' );

  204. function remove_site_health_menu(){

  205. remove_submenu_page( 'tools.php','site-health.php' );

  206. }

  207. function my_filter_head(){

  208. remove_action( 'wp_head','_admin_bar_bump_cb');

  209. remove_action( 'wp_head','feed_links_extra',3 );

  210. remove_action( 'wp_head','feed_links',2 );

  211. remove_action( 'wp_head','rsd_link' );

  212. remove_action( 'wp_head','wlwmanifest_link' );

  213. remove_action( 'wp_head','index_rel_link' );

  214. remove_action( 'wp_head','parent_post_rel_link',10,0 );

  215. remove_action( 'wp_head','start_post_rel_link',10,0 );

  216. remove_action( 'wp_head','adjacent_posts_rel_link',10,0 );

  217. remove_action( 'wp_head','wp_generator' );

  218. }

  219. add_action('get_header','my_filter_head');

  220. remove_action( 'admin_print_scripts', 'print_emoji_detection_script');

  221. remove_action( 'admin_print_styles', 'print_emoji_styles');

  222. remove_action( 'wp_head', 'print_emoji_detection_script', 7);

  223. remove_action( 'wp_print_styles', 'print_emoji_styles');

  224. remove_filter( 'the_content_feed', 'wp_staticize_emoji');

  225. remove_filter( 'comment_text_rss', 'wp_staticize_emoji');

  226. remove_filter( 'wp_mail', 'wp_staticize_emoji_for_email');

  227. remove_action( 'wp_head', 'feed_links_extra', 3 );

  228. remove_action( 'wp_head', 'feed_links', 2 );

  229. remove_action( 'wp_head', 'rsd_link' );

  230. remove_action( 'wp_head', 'wlwmanifest_link' );

  231. remove_action( 'wp_head', 'index_rel_link' );

  232. remove_action( 'wp_head', 'parent_post_rel_link', 10, 0 );

  233. remove_action( 'wp_head', 'start_post_rel_link', 10, 0 );

  234. remove_action( 'wp_head', 'wp_shortlink_wp_head', 10, 0 );

  235. remove_action( 'wp_head', 'adjacent_posts_rel_link', 10, 0 );

  236. remove_action( 'wp_head', 'wp_generator' );

  237. function ludou_remove_width_height_attribute($content){

  238. preg_match_all('/<[img|IMG].*?src=[\'|"](.*?(?:[\.gif|\.jpg|\.png\.bmp]))[\'|"].*?[\/]?>/', $content, $images);

  239. if(!empty($images)) {

  240. foreach($images[0] as $index => $value){

  241. $new_img = preg_replace('/(width|height)="\d*"\s/', "", $images[0][$index]);

  242. $content = str_replace($images[0][$index], $new_img, $content);

  243. }

  244. }

  245. return $content;

  246. }

  247. function v7v3_remove_image_size($sizes) {

  248. unset( $sizes['small'] );

  249. unset( $sizes['medium'] );

  250. unset( $sizes['large'] );

  251. return $sizes;

  252. }

  253. add_filter('image_size_names_choose', 'v7v3_remove_image_size');

  254. function delete_post_and_attachments($post_ID) {

  255. global $wpdb;

  256. $thumbnails = $wpdb->get_results( "SELECT * FROM $wpdb->postmeta WHERE meta_key = '_thumbnail_id' AND post_id = $post_ID" );

  257. foreach ( $thumbnails as $thumbnail ) {

  258. wp_delete_attachment( $thumbnail->meta_value, true );

  259. }

  260. $attachments = $wpdb->get_results( "SELECT * FROM $wpdb->posts WHERE post_parent = $post_ID AND post_type = 'attachment'" );

  261. foreach ( $attachments as $attachment ) {

  262. wp_delete_attachment( $attachment->ID, true );

  263. }

  264. $wpdb->query( "DELETE FROM $wpdb->postmeta WHERE meta_key = '_thumbnail_id' AND post_id = $post_ID" );

  265. }

  266. add_action('before_delete_post', 'delete_post_and_attachments');

  267. function shapeSpace_disable_image_sizes($sizes) {

  268. unset($sizes['thumbnail']);

  269. unset($sizes['medium']);

  270. unset($sizes['large']);

  271. unset($sizes['medium_large']);

  272. unset($sizes['1536x1536']);

  273. unset($sizes['2048x2048']);

  274. return $sizes;

  275. }

  276. add_action('intermediate_image_sizes_advanced', 'shapeSpace_disable_image_sizes');

  277. add_filter('big_image_size_threshold', '__return_false');

  278. function shapeSpace_disable_other_image_sizes() {

  279. remove_image_size('post-thumbnail');

  280. remove_image_size('another-size');

  281. }

  282. add_action('init', 'shapeSpace_disable_other_image_sizes');

  283. if(strlen($_SERVER['REQUEST_URI']) > 366 ||

  284. strpos($_SERVER['REQUEST_URI'], "eval(") ||

  285. strpos($_SERVER['REQUEST_URI'], "base64")) {

  286. @header("HTTP/1.1 414 Request-URI Too Long");

  287. @header("Status: 414 Request-URI Too Long");

  288. @header("Connection: Close");

  289. @exit;

  290. }

  291. session_start();

  292. $timestamp = time();

  293. $ll_nowtime = $timestamp;

  294. if ($_SESSION){

  295. $ll_lasttime = $_SESSION['ll_lasttime'];

  296. $ll_times = $_SESSION['ll_times'] + 1;

  297. $_SESSION['ll_times'] = $ll_times;

  298. }else{

  299. $ll_lasttime = $ll_nowtime;

  300. $ll_times = 1;

  301. $_SESSION['ll_times'] = $ll_times;

  302. $_SESSION['ll_lasttime'] = $ll_lasttime;

  303. }

  304. function tin_custom_upload_name($file){

  305. if(preg_match('/[一-龥]/u',$file['name'])):

  306. $ext=ltrim(strrchr($file['name'],'.'),'.');

  307. $file['name']=preg_replace('#^www\.#', '', strtolower($_SERVER['SERVER_NAME'])).'_'.date('Y-m-d_H-i-s').'.'.$ext;

  308. endif;

  309. return $file;

  310. }

  311. add_filter('wp_handle_upload_prefilter','tin_custom_upload_name',5,1);

  312. function cx_archive_link( $paged = true ) {

  313. $link = false;

  314. if ( is_front_page() ) {

  315. $link = home_url( '/' );

  316. } else if ( is_home() && "page" == get_option('show_on_front') ) {

  317. $link = get_permalink( get_option( 'page_for_posts' ) );

  318. } else if ( is_tax() || is_tag() || is_category() ) {

  319. $term = get_queried_object();

  320. $link = get_term_link( $term, $term->taxonomy );

  321. } else if ( is_post_type_archive() ) {

  322. $link = get_post_type_archive_link( get_post_type() );

  323. } else if ( is_author() ) {

  324. $link = get_author_posts_url( get_query_var('author'), get_query_var('author_name') );

  325. } else if ( is_single() ) {

  326. $link = get_permalink( $id );

  327. } else if ( is_archive() ) {

  328. if ( is_date() ) {

  329. if ( is_day() ) {

  330. $link = get_day_link( get_query_var('year'), get_query_var('monthnum'), get_query_var('day') );

  331. } else if ( is_month() ) {

  332. $link = get_month_link( get_query_var('year'), get_query_var('monthnum') );

  333. } else if ( is_year() ) {

  334. $link = get_year_link( get_query_var('year') );

  335. }

  336. }

  337. }

  338. if ( $paged && $link && get_query_var('paged') > 1 ) {

  339. global $wp_rewrite;

  340. if ( !$wp_rewrite->using_permalinks() ) {

  341. $link = add_query_arg( 'paged', get_query_var('paged'), $link );

  342. } else {

  343. $link = user_trailingslashit( trailingslashit( $link ) . trailingslashit( $wp_rewrite->pagination_base ) . get_query_var('paged'), 'archive' );

  344. }

  345. }

  346. echo '';

  347. }

  348. add_action('wp_head', 'cx_archive_link');

  349. $qmr_work_tags = array(

  350. 'the_content',

  351. );

  352. foreach ( $qmr_work_tags as $qmr_work_tag ) {

  353. remove_filter ($qmr_work_tag, 'wptexturize');

  354. }

  355. function baw_no_admin_access() {

  356. if( !current_user_can( 'administrator' ) ) {

  357. wp_redirect( home_url() );

  358. die();

  359. }

  360. }

  361. add_action( 'admin_init', 'baw_no_admin_access', 1 );

  362. function mail_smtp( $phpmailer ){

  363. $phpmailer->From = '[email protected]';

  364. $phpmailer->FromName = '腾讯网';

  365. $phpmailer->Host = 'smtp.qq.com';

  366. $phpmailer->Port = '465';

  367. $phpmailer->SMTPSecure = 'ssl';

  368. $phpmailer->Username = '[email protected]';

  369. $phpmailer->Password = '0123456789';

  370. $phpmailer->IsSMTP();

  371. $phpmailer->SMTPAuth = true;

  372. }

  373. add_action('phpmailer_init','mail_smtp');

WordPress程序/functions.php收集整理的各种自定义优化代码分享

  • 我的微信
  • 这是我的微信扫一扫
  • weinxin
  • 我的微信公众号
  • 我的微信公众号扫一扫
  • weinxin