wordpress后台编辑器优化
WordPress编辑器(特别是和WooCommerce结合时)确实加载了很多不必要的内容。让我们来优化编辑器的加载:
// 禁用不需要的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) {$settingstinymce = false; // 禁用可视化编辑器$settingsmedia_buttons = false; // 禁用媒体按钮$settingsquicktags = false; // 禁用快速标签}return $settings;}
// 优化编辑器脚本加载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);}}
// 精简TinyMCE编辑器工具栏add_filter('tiny_mce_before_init', 'simplify_product_tinymce_settings');function simplify_product_tinymce_settings($settings) {if (get_post_type() === 'product') {// 只保留基本格式化选项$settings = 'bold,italic,bullist,numlist,link';$settings = '';// 禁用不必要的插件$settings = lists,paste;// 禁用调整大小功能$settings = false;// 简化粘贴操作$settings = true;}return $settings;}// 简化产品描述编辑器add_filter('woocommerce_product_tabs', 'simplify_product_description_editor', 98);function simplify_product_description_editor($tabs) {// 移除产品描述标签页unset($tabs);unset($tabs);unset($tabs);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;}}// 禁用产品页面的元框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);}// 优化编辑器加载性能add_action('admin_init', 'optimize_product_editor_performance');function optimize_product_editor_performance() {if (get_post_type() === 'product') {// 禁用Heartbeat APIwp_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);}}// 添加性能监控add_action('admin_footer', 'monitor_editor_performance');function monitor_editor_performance() {if (get_post_type() === 'product') {?scriptconsole.log('页面加载性能:', performance.timing);console.log('已加载的脚本:', performance.getEntriesByType('resource'));/script?php}}使用建议:选择性应用:根据您的具体需求选择需要的优化代码不要一次性应用所有优化测试每个优化的效果注意事项:保存一份优化前的配置备份在测试环境中先进行测试确保核心编辑功能正常监控变化:观察编辑器加载速度检查功能是否正常收集用户反馈可能需要保留的功能:基本文本编辑图片上传产品基本信息编辑如果您在实施过程中遇到具体问题,或者需要针对特定功能进行优化,请告诉我,我可以提供更详细的解决方案。
wordpress后台编辑器优化 https://cdnanqi.cn/wangluoyingxiao/16860.html
400电话办理