Answers:
您无法增强下拉列表formatselect
。但是您可以tiny_mce_before_init
通过第二个下拉菜单来增强功能,styleselect
默认的WordPress安装中包含隐藏内容。该文档列出了所有默认值和可能性。
请注意,默认情况下,样式下拉列表在WordPress中是隐藏的。首先,将用于自定义格式的按钮添加到TinyMCE的菜单栏中,例如在第2行中带有hook mce_buttons_2
。
add_filter( 'mce_buttons_2', 'fb_mce_editor_buttons' );
function fb_mce_editor_buttons( $buttons ) {
array_unshift( $buttons, 'styleselect' );
return $buttons;
}
然后增强此按钮的下拉菜单。ASLO有用通过在阵列中的附加价值enancement,参见抄本的这个例子。
/**
* Add styles/classes to the "Styles" drop-down
*/
add_filter( 'tiny_mce_before_init', 'fb_mce_before_init' );
function fb_mce_before_init( $settings ) {
$style_formats = array(
array(
'title' => 'Download Link',
'selector' => 'a',
'classes' => 'download'
),
array(
'title' => 'My Test',
'selector' => 'p',
'classes' => 'mytest',
),
array(
'title' => 'AlertBox',
'block' => 'div',
'classes' => 'alert_box',
'wrapper' => true
),
array(
'title' => 'Red Uppercase Text',
'inline' => 'span',
'styles' => array(
'color' => 'red', // or hex value #ff0000
'fontWeight' => 'bold',
'textTransform' => 'uppercase'
)
)
);
$settings['style_formats'] = json_encode( $style_formats );
return $settings;
}
您还可以使用树形菜单增强下拉菜单。从上面的示例源创建var,并在数组中添加更多结构,例如follow源。
$style_formats = array(
array(
'title' => 'Headers',
'items' => array(
array(
'title' => 'Header 1',
'format' => 'h1',
'icon' => 'bold'
),
array(
'title' => 'Header 2',
'format' => 'h2',
'icon' => 'bold'
)
)
),
array(
'title' => 'Download Link',
'selector' => 'a',
'classes' => 'download'
)
);
有关更多可能性和参数,请参见“样式格式”下拉字段的默认值(使用JavaScript编写)。
var defaultStyleFormats = [
{title: 'Headers', items: [
{title: 'Header 1', format: 'h1'},
{title: 'Header 2', format: 'h2'},
{title: 'Header 3', format: 'h3'},
{title: 'Header 4', format: 'h4'},
{title: 'Header 5', format: 'h5'},
{title: 'Header 6', format: 'h6'}
]},
{title: 'Inline', items: [
{title: 'Bold', icon: 'bold', format: 'bold'},
{title: 'Italic', icon: 'italic', format: 'italic'},
{title: 'Underline', icon: 'underline', format: 'underline'},
{title: 'Strikethrough', icon: 'strikethrough', format: 'strikethrough'},
{title: 'Superscript', icon: 'superscript', format: 'superscript'},
{title: 'Subscript', icon: 'subscript', format: 'subscript'},
{title: 'Code', icon: 'code', format: 'code'}
]},
{title: 'Blocks', items: [
{title: 'Paragraph', format: 'p'},
{title: 'Blockquote', format: 'blockquote'},
{title: 'Div', format: 'div'},
{title: 'Pre', format: 'pre'}
]},
{title: 'Alignment', items: [
{title: 'Left', icon: 'alignleft', format: 'alignleft'},
{title: 'Center', icon: 'aligncenter', format: 'aligncenter'},
{title: 'Right', icon: 'alignright', format: 'alignright'},
{title: 'Justify', icon: 'alignjustify', format: 'alignjustify'}
]}
];
现在是最后一点,您可以通过hook包含此格式的自定义CSS mce_css
。
/**
* Apply styles to the visual editor
*/
add_filter( 'mce_css', 'fb_mcekit_editor_style');
function fb_mcekit_editor_style($url) {
if ( ! empty( $url ) )
$url .= ',';
// Retrieves the plugin directory URL
// Change the path here if using different directories
$url .= trailingslashit( plugin_dir_url( __FILE__ ) ) . '/my-editor-styles.css';
return $url;
}
不要忘记也将此样式表规则添加到前端样式表中。
作为增强功能,您可以formatselect
通过检查按钮数组中的值来删除下拉按钮。将以下源代码添加到fb_mce_editor_buttons
挂钩处的函数中mce_buttons_2
。
$value = array_search( 'formatselect', $buttons );
if ( FALSE !== $value ) {
foreach ( $buttons as $key => $value ) {
if ( 'formatselect' === $value )
unset( $buttons[$key] );
}
}
formatselect
下拉列表。该下拉列表没有构建菜单的功能,是WP的tinymce.js中的静态值。
$settings['style_formats_merge'] = true;
到»fb_mce_before_init()«中来添加到格式下拉列表中。
根据此答案,使样式显示在下拉菜单中的关键是unset($settings['preview_styles']);
add_filter( 'tiny_mce_before_init', 'fb_mce_before_init' );
function fb_mce_before_init( $settings ) {
// customize as desired
// unset the preview styles
unset($settings['preview_styles']);`
return $settings;
}
非常有帮助,感谢您的defaultstyles
指点。我发现在将这些默认选项转换为有效的JSON(无效的JavaScript)之前,合并数组不起作用。下面允许附加 WordPress tinymce的下拉列表,而不是替换
默认选项(JSON):
$defaults = '[{ "title": "Headers", "items": [
{ "title": "Header 1", "format": "h1" },
{ "title": "Header 2", "format": "h2" },
{ "title": "Header 3", "format": "h3" },
{ "title": "Header 4", "format": "h4" },
{ "title": "Header 5", "format": "h5" },
{ "title": "Header 6", "format": "h6" }
] },
{ "title": "Inline", "items": [
{ "title": "Bold", "icon": "bold", "format": "bold" },
{ "title": "Italic", "icon": "italic", "format": "italic" },
{ "title": "Underline", "icon": "underline", "format": "underline" },
{ "title": "Strikethrough", "icon": "strikethrough", "format": "strikethrough" },
{ "title": "Superscript", "icon": "superscript", "format": "superscript" },
{ "title": "Subscript", "icon": "subscript", "format": "subscript" },
{ "title": "Code", "icon": "code", "format": "code" }
] },
{ "title": "Blocks", "items": [
{ "title": "Paragraph", "format": "p" },
{ "title": "Blockquote", "format": "blockquote" },
{ "title": "Div", "format": "div" },
{ "title": "Pre", "format": "pre" }
] },
{ "title": "Alignment", "items": [
{ "title": "Left", "icon": "alignleft", "format": "alignleft" },
{ "title": "Center", "icon": "aligncenter", "format": "aligncenter" },
{ "title": "Right", "icon": "alignright", "format": "alignright" },
{ "title": "Justify", "icon": "alignjustify", "format": "alignjustify" }
] }
]';
在functions.php中,返回较大的选项哈希:
add_filter( 'tiny_mce_before_init', 'fb_mce_before_init' );
function fb_mce_before_init( $settings ) {
$style_formats = array(
//....
$settings['style_formats'] = json_encode( array_merge( json_decode($defaults), $style_formats ) );
return $settings;
}
$settings['style_formats_merge'] = true;
到»fb_mce_before_init()«中来添加到格式下拉列表中。