最合适的回答,由SO网友:Vlad K. 整理而成
我也有同样的问题,下面是你能做的。下面的代码禁用块格式部分中的h1标记。同样,您可以禁用其他标记,也可以添加自己的标记。但我不确定如何为它们添加自定义CSS样式。希望这段代码能提示您以何种方式进行挖掘。
//Modify TinyMCE editor to hide H1.
function tiny_mce_remove_unused_formats( $initFormats ) {
// Add block format elements you want to show in dropdown
$initFormats[\'block_formats\'] = \'Paragraph=p;Heading 2=h2;Heading 3=h3;Heading 4=h4;Heading 5=h5;Heading 6=h6;Preformatted=pre\';
return $initFormats;
}
add_filter( \'tiny_mce_before_init\', \'tiny_mce_remove_unused_formats\' );
Update: 你正在寻找的东西在Wordpress 3.9发布之前是可能的。早些时候,您必须编写这些代码行才能使之成为可能。但不幸的是,theme_advanced_styles
已弃用,因为WP 3.9将TinyMCE更新为版本4(see the Change Log). 有关Andrew Ozz blog. <这就是以前的情况(source):
function make_mce_awesome( $init ) {
// deprecated settings
$init[\'theme_advanced_blockformats\'] = \'h2,h3,h4,p\';
$init[\'theme_advanced_disable\'] = \'underline,spellchecker,wp_help\';
$init[\'theme_advanced_text_colors\'] = \'0f3156,636466,0486d3\';
$init[\'theme_advanced_buttons2_add\'] = \'styleselect\';
$init[\'theme_advanced_styles\'] = "bigTitle=bigTitle;Call To Action Button=ctaButton,Rounded Corners=rounded";
return $init;
}
add_filter(\'tiny_mce_before_init\', \'make_mce_awesome\');
Solution:无论如何,我有我的解决方案。您可以摆脱默认下拉列表,添加包含四种样式的格式下拉列表。这将帮助您避免与用户混淆,避免他们从下拉列表中选择样式。
禁用默认下拉列表:
function remove_default_format_select( $buttons ) {
//Remove the format dropdown select and text color selector
$remove = array( \'formatselect\' );
return array_diff( $buttons, $remove );
}
add_filter( \'mce_buttons\', \'remove_default_format_select\' );
添加新格式下拉列表(
more here):
// Callback function to insert \'styleselect\' into the $buttons array
function my_new_mce_buttons( $buttons ) {
array_unshift( $buttons, \'styleselect\' );
return $buttons;
}
// Register our callback to the appropriate filter
add_filter( \'mce_buttons\', \'my_new_mce_buttons\' );
// Callback function to filter the MCE settings
function my_mce_before_init_insert_formats( $init_array ) {
// Define the style_formats array
$style_formats = array(
array(
\'title\' => \'Headline\',
\'block\' => \'h1\'
),
array(
\'title\' => \'SubHeadline\',
\'block\' => \'h2\'
),
array(
\'title\' => \'Statement\',
\'block\' => \'div\',
\'classes\' => \'statement_class\',
\'wrapper\' => true
)
);
// Insert the array, JSON ENCODED, into \'style_formats\'
$init_array[\'style_formats\'] = json_encode( $style_formats );
return $init_array;
}
// Attach callback to \'tiny_mce_before_init\'
add_filter( \'tiny_mce_before_init\', \'my_mce_before_init_insert_formats\' );
最后一个。注册css文件以在编辑器中获得视觉效果:(
Learn more)
/**
* Registers an editor stylesheet for the theme.
*/
function wpdocs_theme_add_editor_styles() {
add_editor_style( \'custom-editor-style.css\' );
}
add_action( \'admin_init\', \'wpdocs_theme_add_editor_styles\' );
希望这有帮助。