You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

如何通过add_editor_style为不同页面的经典编辑器加载不同CSS文件?

问题描述

我希望根据页面slug为WordPress经典编辑器加载不同的CSS文件,主题目录下已创建editor-style文件夹存放相关CSS文件。但编写的代码既无法加载预期的CSS文件,也无法加载默认的editor-style-default.css。最初用switch语句也无效;移除$post_type判断后,所有页面编辑器只加载默认CSS。

原代码(含$post_type判断):

function custom_editor_styles() {

  global $post;

  $post_type = get_post_type($post);
  $page_slug = $post->post_name;

  if ($post_type === 'page') {
    // change editor-style dynamically
    if($page_slug === 'about') {
      add_editor_style('editor-style/editor-style-about.css');
    } elseif ($page_slug === 'contact') {
      add_editor_style('editor-style/editor-style-contact.css');
    } elseif ($page_slug === 'news') {
      add_editor_style('editor-style/editor-style-news.css');
    } else {
      add_editor_style('editor-style/editor-style-default.css');
    }
  }
}

add_action('admin_init', 'custom_editor_styles');

移除$post_type判断后的代码:

function custom_editor_styles() {

  global $post;

  $post_type = get_post_type($post);
  $page_slug = $post->post_name;

  // if ($post_type === 'page') {  // removed this line
    // change editor-style dynamically
    if($page_slug === 'about') {
      add_editor_style('editor-style/editor-style-about.css');
    } elseif ($page_slug === 'contact') {
      add_editor_style('editor-style/editor-style-contact.css');
    } elseif ($page_slug === 'news') {
      add_editor_style('editor-style/editor-style-news.css');
    } else {
      add_editor_style('editor-style/editor-style-default.css');
    }
  // } // removed this line
}

add_action('admin_init', 'custom_editor_styles');
问题原因
  1. 钩子触发时机过早:admin_init钩子在后台初始化阶段就执行,此时当前编辑的$post对象还未完全加载,无法正确获取页面的slug和类型,导致所有条件判断失效,最终只能加载默认CSS或无法加载样式。
  2. 全局$post不可靠:在admin_init阶段,全局$post可能为空或者指向非当前编辑的对象,无法拿到正确的页面信息。
解决方案

替换为更合适的钩子,并通过可靠方式获取当前编辑的页面对象,修正后的代码如下:

function custom_editor_styles() {
    // 仅在页面编辑的经典编辑器页面执行
    if ( ! is_admin() || get_current_screen()->base !== 'post' || get_current_screen()->post_type !== 'page' ) {
        return;
    }

    // 获取当前编辑的页面ID
    $post_id = isset( $_GET['post'] ) ? intval( $_GET['post'] ) : ( isset( $_POST['post_ID'] ) ? intval( $_POST['post_ID'] ) : 0 );
    if ( ! $post_id ) {
        return;
    }

    // 获取可靠的页面对象
    $post = get_post( $post_id );
    if ( ! $post ) {
        return;
    }

    $page_slug = $post->post_name;

    // 根据slug加载对应编辑器样式
    switch ( $page_slug ) {
        case 'about':
            add_editor_style( 'editor-style/editor-style-about.css' );
            break;
        case 'contact':
            add_editor_style( 'editor-style/editor-style-contact.css' );
            break;
        case 'news':
            add_editor_style( 'editor-style/editor-style-news.css' );
            break;
        default:
            add_editor_style( 'editor-style/editor-style-default.css' );
            break;
    }
}
// 绑定到页面编辑页面加载完成后的钩子
add_action( 'load-post.php', 'custom_editor_styles' );
add_action( 'load-post-new.php', 'custom_editor_styles' );
关键说明
  • 钩子选择:使用load-post.php和load-post-new.php钩子,确保在页面编辑界面完全加载后再执行代码,此时能正确获取页面的所有信息。
  • 环境判断:通过get_current_screen()限制仅在页面编辑的经典编辑器页面执行,避免影响其他后台页面。
  • 可靠获取页面对象:通过URL参数或POST参数获取页面ID,再用get_post()获取页面对象,替代不可靠的全局$post。
  • 路径正确性:add_editor_style接收的是相对主题目录的路径,会自动转换为正确的前端URL,无需手动拼接。

内容的提问来源于stack exchange,提问作者user23586300

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.06.14 18:43:11