分享SEM、信息流广告推广优化经验。
面向企业提供专业SEM优化顾问服务。

WordPress 主题 Functions.php 常用优化代码

Functions.php在经典主题、块主题和子主题均可使用。是您向 WordPress 主题添加独特功能的地方。

它可用于挂钩 WordPress 的核心功能,使您的主题更加模块化、可扩展性和功能性。

Functions.php文件的行为类似于 WordPress 插件,可向 WordPress 站点添加特性和功能。您可以使用它来调用 WordPress 函数并定义您自己的函数。

WordPress functions.php

一个functions.php文件:

  • 不需要唯一的标题文本;
  • 存储在 wp-content/themes 中主题的子目录中;
  • 仅在活动主题的目录中执行;
  • 仅适用于该主题(如果更改主题,则无法再使用该功能);
  • 可以有许多代码块用于许多不同的目的。

functions.php 每个主题都有自己的函数文件,但实际上只运行活动主题中的代码 。如果您的主题已经有一个函数文件,您可以向其中添加代码。functions.php如果没有,您可以在主题目录下创建一个。

如果functions.php中的代码较多,不利于管理,可以把代码拆分在不同的文件中引入:

require get_stylesheet_directory() . '/functions-theme.php';
require get_stylesheet_directory() . '/functions-other.php';

常用代码(供参考,更新中)

<?php

// 屏蔽标题<title>破折号转换代码
add_filter('run_wptexturize', '__return_false');

// 把摘要[...]换为...
function new_excerpt_more($more) {
	return '...';
}
add_filter('excerpt_more', 'new_excerpt_more');

// Remove rel='prev'.
add_action( 'aioseop_prev_link', '__return_false' );
add_action( 'aioseop_next_link', '__return_false' );

// Remove All SEO in Pack Prev Link
add_filter( 'aioseo_prev_link', 'aioseo_filter_prev_link' );
function aioseo_filter_prev_link( $previous ) {
	return '';
}
add_filter( 'aioseo_next_link', 'aioseo_filter_next_link' );
function aioseo_filter_next_link( $next ) {
	return '';
}

// WordPress 5.0+移除 block-library CSS
add_action( 'wp_enqueue_scripts', 'fanly_remove_block_library_css', 100 );
function fanly_remove_block_library_css() {
	wp_dequeue_style( 'wp-block-library' );
}

// WordPress 6.0移除
function disable_classic_theme_styles() {
	wp_deregister_style('classic-theme-styles');
	wp_dequeue_style('classic-theme-styles');
}
add_filter('wp_enqueue_scripts', 'disable_classic_theme_styles', 100);

// 增加链接管理器
add_filter( 'pre_option_link_manager_enabled', '__return_true' );

// 评论链接增加 rel="nofollow"
function add_nofollow_to_comments_popup_link() {
	return ' rel="nofollow"';
}
add_filter('comments_popup_link_attributes', 'add_nofollow_to_comments_popup_link');

// 如果不是管理页面,移除无用Header
if (!is_admin()) {
	remove_action( 'wp_head', 'feed_links_extra', 3 );
	// Display the links to the extra feeds such as category feeds
	remove_action( 'wp_head', 'feed_links', 2 );
	// Display the links to the general feeds: Post and Comment Feed
	remove_action( 'wp_head', 'rsd_link' );
	// Display the link to the Really Simple Discovery service endpoint, EditURI link
	remove_action( 'wp_head', 'wlwmanifest_link' );
	// Display the link to the Windows Live Writer manifest file.
	remove_action( 'wp_head', 'index_rel_link' );
	// index link
	remove_action( 'wp_head', 'parent_post_rel_link', 10, 0 );
	// prev link
	remove_action( 'wp_head', 'start_post_rel_link', 10, 0 );
	// start link
	remove_action( 'wp_head', 'adjacent_posts_rel_link', 10, 0 );
	// Display relational links for the posts adjacent to the current post.
	remove_action( 'wp_head', 'wp_generator' );
	// Display the XHTML generator that is generated on the wp_head hook, WP version
	remove_action( 'wp_head', 'wp_shortlink_wp_head', 10, 0 );
	remove_action('template_redirect', 'wp_shortlink_header', 11, 0);
	//remove_action('wp_head', 'rel_canonical');
	remove_action( 'wp_head', 'adjacent_posts_rel_link_wp_head', 10, 0 );
	/*** Disable the emoji's  **/
	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_filter( 'the_content_feed', 'wp_staticize_emoji' );
	remove_filter( 'comment_text_rss', 'wp_staticize_emoji' );
	remove_filter( 'wp_mail', 'wp_staticize_emoji_for_email' );
}
// 摘要字数限制
function custom_excerpt_length( $length ) {
	return 40;
	//填写需要截取的字符数,1个汉字=2个字符
}
add_filter( 'excerpt_length', 'custom_excerpt_length', 999 );

// 关闭 XML-RPC 功能 
add_filter('xmlrpc_enabled', '__return_false');

// 屏蔽 REST API
add_filter('rest_enabled', '__return_false');
add_filter('rest_jsonp_enabled', '__return_false');

// 移除Remove YARPP Style CSS
add_filter( "yarpp_enqueue_related_style", "__return_false" );

// 移除 rel='dns-prefetch'
function remove_dns_prefetch( $hints, $relation_type ) {
	if ( 'dns-prefetch' === $relation_type ) {
		return array_diff( wp_dependencies_unique_hosts(), $hints );
	}
	return $hints;
}
add_filter( 'wp_resource_hints', 'remove_dns_prefetch', 10, 2 );

// 移除wp-json
remove_action( 'template_redirect', 'rest_output_link_header', 11, 0 );
remove_action( 'wp_head','rest_output_link_wp_head' );
remove_action( 'wp_head', 'wp_oembed_add_discovery_links' );

// 隐藏管理导航
add_filter('show_admin_bar', 'hide_admin_bar');
function hide_admin_bar($flag) {
	return false;
}

// 禁止Ping自己 no self Pingback
add_action('pre_ping', '_noself_ping');
function _noself_ping(&$links) {
	$home = get_option('home');
	foreach ($links as $l => $link) {
		if (0 === strpos($link, $home)) {
			unset($links[$l]);
		}
	}
}

// 禁用新版古腾堡编辑器	
add_filter('use_block_editor_for_post', '__return_false');
remove_action( 'wp_enqueue_scripts', 'wp_common_block_scripts_and_styles' );

// 禁用文章修订版本及自动保存
add_filter( 'wp_revisions_to_keep', 'specs_wp_revisions_to_keep', 10, 2 );
function specs_wp_revisions_to_keep( $num, $post ) {
	return 0;
}

// 禁用自动保存
add_action('wp_print_scripts','disable_autosave');
function disable_autosave() {
	wp_deregister_script('autosave');
}

// 删除前台WordPress 自带Jquery脚本
if ( !is_admin() ) {
	function no_more_jquery() {
		wp_deregister_script('jquery');
	}
	add_action('wp_enqueue_scripts', 'no_more_jquery');
}

// 屏蔽文章 Embed 功能
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);
remove_action('wp_head', 'wp_oembed_add_discovery_links');
remove_action('wp_head', 'wp_oembed_add_host_js');

// 移除 WordPress 加载的JS和CSS链接中的版本号
function wpdaxue_remove_cssjs_ver( $src ) {
	if( strpos( $src, 'ver=' ) )
				$src = remove_query_arg( 'ver', $src );
	return $src;
}
add_filter( 'style_loader_src', 'wpdaxue_remove_cssjs_ver', 999 );
add_filter( 'script_loader_src', 'wpdaxue_remove_cssjs_ver', 999 );

// 找回上传图片设置
if(get_option('upload_path')=='wp-content/uploads' || get_option('upload_path')==null) {
	update_option('upload_path',WP_CONTENT_DIR.'/uploads');
}

// WordPress 移除头部 global-styles-inline-css
add_action('wp_enqueue_scripts', 'fanly_remove_global_styles_inline');
function fanly_remove_global_styles_inline() {
	wp_deregister_style( 'global-styles' );
	wp_dequeue_style( 'global-styles' );
}

//  自定义JPEG图片压缩质量
function wpdx_custom_jpeg_quality() {
	//根据实际需求,修改下面的数字即可
	return 100;
}
add_filter( 'jpeg_quality', 'wpdx_custom_jpeg_quality');

?>