WordPress编辑器(特别是和WooCommerce结合时)确实加载了很多不必要的内容。让我们来优化编辑器的加载:
1. 精简Gutenberg编辑器区块
// 禁用不需要的Gutenberg区块
add_filter('allowed_block_types_all', 'restrict_blocks_in_product', 10, 2);
function restrict_blocks_in_product($allowed_blocks, $editor_context) {
if (!empty($editor_context->post) && $editor_context->post->post_type === 'product') {
// 只保留必要的区块
return array(
'core/paragraph', // 段落
'core/image', // 图片
'core/heading', // 标题
'core/list', // 列表
'core/table' // 表格
);
}
return $allowed_blocks;
}
// 移除不必要的编辑器模式
add_filter('wp_editor_settings', 'simplify_product_editor_settings', 10, 2);
function simplify_product_editor_settings($settings, $editor_id) {
if (get_post_type() === 'product') {
$settings['tinymce'] = false; // 禁用可视化编辑器
$settings['media_buttons'] = false; // 禁用媒体按钮
$settings['quicktags'] = false; // 禁用快速标签
}
return $settings;
}
2. 优化编辑器脚本加载
// 优化编辑器脚本加载
add_action('admin_enqueue_scripts', 'optimize_product_editor_scripts', 999);
function optimize_product_editor_scripts($hook) {
if (!in_array($hook, array('post.php', 'post-new.php')) || get_post_type() !== 'product') {
return;
}
// 移除不必要的编辑器脚本
wp_dequeue_script('wplink'); // 移除链接工具
wp_dequeue_script('editor-buttons'); // 移除编辑器按钮
// 移除表情符号支持
remove_action('admin_print_scripts', 'print_emoji_detection_script');
// 移除不必要的编辑器样式
wp_dequeue_style('editor-buttons');
wp_dequeue_style('wp-edit-blocks');
}
// 禁用产品编辑页面的自动保存和修订版本
add_action('admin_init', 'disable_product_editor_features');
function disable_product_editor_features() {
if (get_post_type() === 'product') {
// 禁用自动保存
wp_deregister_script('autosave');
// 禁用修订版本
remove_post_type_support('product', 'revisions');
}
}
3. 精简TinyMCE编辑器(如果使用)
// 精简TinyMCE编辑器工具栏
add_filter('tiny_mce_before_init', 'simplify_product_tinymce_settings');
function simplify_product_tinymce_settings($settings) {
if (get_post_type() === 'product') {
// 只保留基本格式化选项
$settings['toolbar1'] = 'bold,italic,bullist,numlist,link';
$settings['toolbar2'] = '';
// 禁用不必要的插件$settings[‘plugins’] = ‘lists,paste’;
// 禁用调整大小功能$settings[‘resize’] = false;
// 简化粘贴操作$settings[‘paste_as_text’] = true;
}
return $settings;
}
4. 优化产品描述编辑器
// 简化产品描述编辑器
add_filter('woocommerce_product_tabs', 'simplify_product_description_editor', 98);
function simplify_product_description_editor($tabs) {
// 移除产品描述标签页
unset($tabs['description']);
unset($tabs['additional_information']);
unset($tabs['reviews']);
return $tabs;
}
// 自定义产品编辑器的容量add_action(‘admin_head’, ‘customize_product_editor_height’);
function customize_product_editor_height() {
if (get_post_type() === ‘product’) {
echo ‘<style>
.wp-editor-area {
height: 200px !important;
}
.block-editor-writing-flow {
max-width: 800px;
margin: 0 auto;
}
</style>’;
}
}
5. 禁用不必要的元框和面板
// 禁用产品页面的元框
add_action('add_meta_boxes', 'remove_product_editor_metaboxes', 999);
function remove_product_editor_metaboxes() {
// 移除产品页面的元框
remove_meta_box('woocommerce-product-images', 'product', 'side');
remove_meta_box('tagsdiv-product_tag', 'product', 'side');
remove_meta_box('product_catdiv', 'product', 'side');
// 移除其他不必要的元框remove_meta_box(‘postcustom’, ‘product’, ‘normal’);
remove_meta_box(‘commentstatusdiv’, ‘product’, ‘normal’);
remove_meta_box(‘commentsdiv’, ‘product’, ‘normal’);
}
6. 优化编辑器加载性能
// 优化编辑器加载性能
add_action('admin_init', 'optimize_product_editor_performance');
function optimize_product_editor_performance() {
if (get_post_type() === 'product') {
// 禁用Heartbeat API
wp_deregister_script('heartbeat');
// 禁用主题自定义remove_action(‘wp_head’, ‘wp_custom_css_cb’, 101);
// 禁用embedsremove_action(‘wp_head’, ‘wp_oembed_add_discovery_links’);
remove_action(‘wp_head’, ‘wp_oembed_add_host_js’);
}
}
7. 添加编辑器性能监控(可选,用于调试)
// 添加性能监控
add_action('admin_footer', 'monitor_editor_performance');
function monitor_editor_performance() {
if (get_post_type() === 'product') {
<script>
console.log('页面加载性能:', performance.timing);
console.log('已加载的脚本:', performance.getEntriesByType('resource'));
</script>
}
}
使用建议:
- 选择性应用:
- 根据您的具体需求选择需要的优化代码
- 不要一次性应用所有优化
- 测试每个优化的效果
- 注意事项:
- 保存一份优化前的配置备份
- 在测试环境中先进行测试
- 确保核心编辑功能正常
- 监控变化:
- 观察编辑器加载速度
- 检查功能是否正常
- 收集用户反馈
- 可能需要保留的功能:
- 基本文本编辑
- 图片上传
- 产品基本信息编辑
如果您在实施过程中遇到具体问题,或者需要针对特定功能进行优化,请告诉我,我可以提供更详细的解决方案。