wp_editor始终将<br/>转换为<p> </p>

时间:2015-10-07 01:58:19

标签: html wordpress editor wp-editor

我在管理员上创建了一个wp_editor的自定义插件,现在当我在Text视图标签中的<br>选项卡中添加一些html标签时,点击{{ 1}}标签..当我回到Visual标签时,<br>转换为<p>&nbsp;</p>

这是我的PHP代码:

Text

这是发生的事情: 我将$html_value = '<h1>test</h1><br> ....'; $settings = array( 'textarea_name' => 'al_srms_fileform_content', 'media_buttons' => true, 'wpautop' => false ); wp_editor($html_value, 'mycustomeditor0pdf', $settings ); 标记放在<br>标签上。 enter image description here

点击Text显示结果。 enter image description here

我点击了Visual标签,Text消失了,取而代之的是<br> enter image description here

有没有办法让<p>&nbsp;</p>保持<br>

2 个答案:

答案 0 :(得分:0)

您遇到的问题是您的Themes functions.php文件中的wpautop过滤器功能的结果。

要禁用此功能,请将以下内容添加到主题目录中的functions.php文件的行中:

remove_filter( 'the_content', 'wpautop' );
remove_filter( 'the_excerpt', 'wpautop' );

参考:https://codex.wordpress.org/Function_Reference/wpautop(Wordpress Codex)

答案 1 :(得分:0)

我希望这会对你有所帮助。但是,您不需要安装建议的插件。只需添加此迷你插件即可设置:

<?php
defined( 'ABSPATH' ) OR exit;
/* Plugin Name: TinyMCE break instead of paragraph */
function mytheme_tinymce_settings( $tinymce_init_settings ) {
    $tinymce_init_settings['forced_root_block'] = false;
    return $tinymce_init_settings;
}
add_filter( 'tiny_mce_before_init', 'mytheme_tinymce_settings' );

现在当您按Enter键时,将插入<br>标记而不是创建新段落。但请注意,如果您创建两个连续的换行符,则由于将wpautop过滤器应用于您的帖子内容,文本仍将拆分为段落。您需要先删除此过滤器,然后创建一个新的过滤器,将使用<br>标记替换所有换行符。将这样的内容添加到您的functions.php中,以在模板中显示<br>标记:

remove_filter ( 'the_content', 'wpautop' );
add_filter ( 'the_content', 'add_newlines_to_post_content' );
function add_newlines_to_post_content( $content ) {
    return nl2br( $content );
}
相关问题