我已经阅读了几个教程,用于将自定义样式添加到WYSIWYG(TinyMCE)编辑器。他们似乎都没有在Wordpress的最新版本中工作。我正在使用v3.3.2。 instructions from the codex工作,但以有限的方式...WordPress的自定义样式TinyMCE
注意:要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
。我怀疑这是一个常见的错误。
您能否提供完整的工作代码?我试图让这个工作,但还没有任何运气。 – INT
使用问题中的原始代码,但用'style_formats'替换'theme_advanced_styles'。 – emersonthis
仅供参考...对于那些试图让它工作 - 它确实 - 在答案中没有提及的技巧(但是在问题中)是你必须在分配它之前将'$ style_formats'数组赋值给'$ style_formats'数组到'style_formats'设置 – Ben