wordpress常见优化教程,后台优化代码
优化wordpress代码或是后台代码,禁止一些可有可无的功能,会得到良好的加速和体验,教程开始:
添加以下代码在主题的functions.php文件
禁用自动更新
// Disable core auto-updates
add_filter( 'auto_update_core', '__return_false' );
// Disable auto-updates for plugins.
add_filter( 'auto_update_plugin', '__return_false' );
// Disable auto-updates for themes.
add_filter( 'auto_update_theme', '__return_false' );
禁用自动更新电子邮件
// Disable auto-update emails.
add_filter( 'auto_core_update_send_email', '__return_false' );
// Disable auto-update emails for plugins.
add_filter( 'auto_plugin_update_send_email', '__return_false' );
// Disable auto-update emails for themes.
add_filter( 'auto_theme_update_send_email', '__return_false' );
add_filter( 'comment_form_default_fields', function ($fields) {
if ( isset( $fields['url'] ) ) {
unset( $fields['url'] );
}
return $fields;
}, 150 );
/**
* Disable all embeds in WordPress.
*/
add_action( 'init', function () {
// Remove the REST API endpoint.
remove_action( 'rest_api_init', 'wp_oembed_register_route' );
// Turn off oEmbed auto discovery.
add_filter( 'embed_oembed_discover', '__return_false' );
// Don't filter oEmbed results.
remove_filter( 'oembed_dataparse', 'wp_filter_oembed_result', 10 );
// Remove oEmbed discovery links.
remove_action( 'wp_head', 'wp_oembed_add_discovery_links' );
// Remove oEmbed-specific JavaScript from the front-end and back-end.
remove_action( 'wp_head', 'wp_oembed_add_host_js' );
add_filter( 'tiny_mce_plugins', function ( $plugins ) {
return array_diff( $plugins, array( 'wpembed' ) );
} );
// Remove all embeds rewrite rules.
add_filter( 'rewrite_rules_array', function ( $rules ) {
foreach ( $rules as $rule => $rewrite ) {
if ( false !== strpos( $rewrite, 'embed=true' ) ) {
unset( $rules[ $rule ] );
}
}
return $rules;
} );
// Remove filter of the oEmbed result before any HTTP requests are made.
remove_filter( 'pre_oembed_result', 'wp_filter_pre_oembed_result', 10 );
}, 9999 );
add_filter( 'wp_resource_hints', function ( $urls, $relation_type ) {
if ( 'dns-prefetch' === $relation_type ) {
$emoji_svg_url = apply_filters( 'emoji_svg_url', 'https://s.w.org/images/core/emoji/2/svg/' );
$urls = array_diff( $urls, array( $emoji_svg_url ) );
}
return $urls;
}, 10, 2 );
} );
add_action(
'enqueue_block_editor_assets',
function () {
$script = "jQuery( window ).load(function() { const isFullscreenMode = wp.data.select( 'core/edit-post' ).isFeatureActive( 'fullscreenMode' ); if ( isFullscreenMode ) { wp.data.dispatch( 'core/edit-post' ).toggleFeature( 'fullscreenMode' ); } });";
wp_add_inline_script( 'wp-blocks', $script );
}
);
add_filter( 'block_editor_settings_all', function ( $settings ) {
$settings['codeEditingEnabled'] = current_user_can( 'manage_options' );
return $settings;
} );
add_filter( 'wp_lazy_loading_enabled', '__return_false' );
add_filter( 'enable_login_autofocus', '__return_false' );
function wpcode_send_new_user_notifications( $user_id, $notify = 'user' ) {
if ( empty( $notify ) || 'admin' === $notify ) {
return;
} elseif ( 'both' === $notify ) {
// Send new users the email but not the admin.
$notify = 'user';
}
wp_send_new_user_notifications( $user_id, $notify );
}
add_action(
'init',
function () {
// Disable default email notifications.
remove_action( 'register_new_user', 'wp_send_new_user_notifications' );
remove_action( 'edit_user_created_user', 'wp_send_new_user_notifications' );
// Replace with custom function that only sends to user.
add_action( 'register_new_user', 'wpcode_send_new_user_notifications' );
add_action( 'edit_user_created_user', 'wpcode_send_new_user_notifications', 10, 2 );
}
);
remove_action( 'xmlrpc_rsd_apis', 'rest_output_rsd' );
remove_action( 'wp_head', 'rest_output_link_wp_head' );
remove_action( 'template_redirect', 'rest_output_link_header', 11 );
// Prevent search queries.
add_action(
'parse_query',
function ( $query, $error = true ) {
if ( is_search() && ! is_admin() ) {
$query->is_search = false;
$query->query_vars['s'] = false;
$query->query['s'] = false;
if ( true === $error ) {
$query->is_404 = true;
}
}
},
15,
2
);
// Remove the Search Widget.
add_action(
'widgets_init',
function () {
unregister_widget( 'WP_Widget_Search' );
}
);
// Remove the search form.
add_filter( 'get_search_form', '__return_empty_string', 999 );
// Remove the core search block.
add_action(
'init',
function () {
if ( ! function_exists( 'unregister_block_type' ) || ! class_exists( 'WP_Block_Type_Registry' ) ) {
return;
}
$block = 'core/search';
if ( WP_Block_Type_Registry::get_instance()->is_registered( $block ) ) {
unregister_block_type( $block );
}
}
);
// Remove admin bar menu search box.
add_action(
'admin_bar_menu',
function ( $wp_admin_bar ) {
$wp_admin_bar->remove_menu( 'search' );
},
11
);
add_action( 'pre_ping', function( &$links ) {
$home = get_option( 'home' );
foreach ( $links as $l => $link ) {
if ( 0 === strpos( $link, $home ) ) {
unset( $links[ $l ] );
}
}
} );
// Remove Tools Submenu Item for Site Health.
add_action( 'admin_menu', function () {
remove_submenu_page( 'tools.php', 'site-health.php' );
} );
// Prevent direct access to the Site Health page.
add_action( 'current_screen', function () {
$screen = get_current_screen();
if ( 'site-health' === $screen->id ) {
wp_safe_redirect( admin_url() );
exit;
}
} );
// Disable the Site Health Dashboard Widget.
add_action( 'wp_dashboard_setup', function () {
global $wp_meta_boxes;
if ( isset( $wp_meta_boxes['dashboard']['normal']['core']['dashboard_site_health'] ) ) {
unset( $wp_meta_boxes['dashboard']['normal']['core']['dashboard_site_health'] );
}
} );
remove_action('wp_head', 'wp_shortlink_wp_head' );
add_action( 'admin_head', function () {
if ( current_user_can( 'update_core' ) ) {
return;
}
remove_action( 'admin_notices', 'update_nag', 3 );
}, 1 );
remove_action('wp_head', 'wlwmanifest_link');
add_filter(
'rest_authentication_errors',
function ( $access ) {
return new WP_Error(
'rest_disabled',
__( 'The WordPress REST API has been disabled.' ),
array(
'status' => rest_authorization_required_code(),
)
);
}
);
add_filter('wp_sitemaps_enabled', '__return_false');
add_filter( 'xmlrpc_enabled', '__return_false' );
add_action( 'login_footer', function () {
remove_action( 'login_footer', 'wp_shake_js', 12 );
} );
function wpcode_snippet_remove_query_strings_split( $src ) {
$output = preg_split( "/(&ver|\\?ver)/", $src );
return $output ? $output[0] : '';
}
add_action( 'init', function () {
if ( ! is_admin() ) {
add_filter( 'script_loader_src', 'wpcode_snippet_remove_query_strings_split', 15 );
add_filter( 'style_loader_src', 'wpcode_snippet_remove_query_strings_split', 15 );
}
} );
add_action( 'wp_before_admin_bar_render', function () {
global $wp_admin_bar;
$wp_admin_bar->remove_menu( 'wp-logo' );
}, 0 );
add_filter('the_generator', '__return_empty_string');
add_action( 'wp_enqueue_scripts', function() {
wp_dequeue_style( 'wp-block-library' );
wp_dequeue_style( 'wp-block-library-theme' );
}, 110 );
版权申明:本站所有文章,如无特殊说明或标注,均为本站原创发布。任何个人或组织,在未征得本站同意时,禁止复制、盗用、采集、发布本站内容到任何网站、书籍等各类媒体平台。如若本站内容侵犯了原著者的合法权益,可联系我们进行处理。
蓝大富博客 » wordpress常见优化教程,后台优化代码
蓝大富博客 » wordpress常见优化教程,后台优化代码