详情页

wordpress常见优化教程,后台优化代码

时间:2023年12月24日

编辑:佚名

优化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 );
    }
);
全选代码复制
禁用 REST API 链接
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
);
全选代码复制
禁用自 Pingbacks
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'] );
    }
} );
全选代码复制
禁用WordPress短链接
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 );
全选代码复制
禁用 wlwmanifest 链接
remove_action('wp_head', 'wlwmanifest_link');
全选代码复制
禁用WordPress REST API
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(),
            )
        );
    }
);
全选代码复制
禁用WordPress站点地图
add_filter('wp_sitemaps_enabled', '__return_false');
全选代码复制
禁用 XML-RPC
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 );
    }
} );
全选代码复制
从管理栏中删除WordPress徽标
add_action( 'wp_before_admin_bar_render', function () {
    global $wp_admin_bar;
    $wp_admin_bar->remove_menu( 'wp-logo' );
}, 0 );
全选代码复制
删除WordPress版本号
add_filter('the_generator', '__return_empty_string');
全选代码复制
删除 WP 块库 CSS
add_action( 'wp_enqueue_scripts', function() {
    wp_dequeue_style( 'wp-block-library' );
    wp_dequeue_style( 'wp-block-library-theme' );
}, 110 );
相关文章
猜你需要