【发布时间】:2012-05-12 18:26:37
【问题描述】:
我已经阅读了一些关于向 WYSIWYG (TinyMCE) 编辑器添加自定义样式的教程。它们似乎都不适用于最新版本的 Wordpress。我正在使用 v3.3.2。 instructions from the codex 工作,但以有限的方式......
注意:为了 100% 清楚,我正在尝试添加一个“样式”下拉列表,作者可以使用该下拉列表将我的自定义样式应用于文本。 (请不要将我的问题与如何使用 editor-style.css 设置编辑器自身的样式...)
我设法让代码正常工作,但只使用my_mce_before_init() 中的注释行。这个版本的问题是它添加了带有通用<span> 的类。我正在尝试使用更强大的代码版本(如下所示),但有些地方不太对劲。样式下拉框出现,但它是空白的。如果我点击它,第一项是“样式”,但什么也不做。我怀疑我的阵列有问题。希望比我知识渊博的人能直截了当。
这是我主题的functions.php中的相关代码...
这是我添加按钮的方法:
// Add the Style selectbox to the second row of MCE buttons
function my_mce_buttons_2($buttons)
{
array_unshift($buttons, 'styleselect');
return $buttons;
}
add_filter('mce_buttons_2', 'my_mce_buttons_2');
这是我添加样式的方法(当我取消注释时它会起作用):
//Define the actual styles that will be in the box
function my_mce_before_init($init_array)
{
// add classes using a ; separated values
//$init_array['theme_advanced_styles'] = "Section Head=section-head;Sub Section Head=sub-section-head";
$temp_array['theme_advanced_styles'] = array(
array(
'title' => 'Section Head',
'block' => 'h3',
'classes' => 'section-head'
),
array(
'title' => 'Sub Section Head',
'block' => 'h4',
'classes' => 'sub-section-head'
)
);
$styles_array = json_encode( $temp_array['theme_advanced_styles'] );
// THIS IS THE PROBLEM !!!! READ BELOW
$init_array['theme_advanced_styles'] = $styles_array;
return $init_array;
}
add_filter('tiny_mce_before_init', 'my_mce_before_init');
更新:我想通了(请参阅下面的答案)。在向下滚动之前,请注意在上面的代码中,theme_advanced_styles 是错误的键。以我正在做的方式定义自定义样式时,它应该是style_formats。我怀疑这是一个常见的错误。
【问题讨论】:
标签: wordpress tinymce wordpress-theming