2

我创建了一个wp_editor在管理员上具有的自定义插件,现在当我将一些 html 标签放入编辑器中的Text视图选项卡中<br>并单击Visual选项卡时,当我返回选项卡时<br>转换为。<p>&nbsp;</p>Text

这是我的 php 代码:

$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>按标签放置标签Text在此处输入图像描述

我单击Visual以显示结果。 在此处输入图像描述

我单击返回Text选项卡,然后<br>消失并替换为<p>&nbsp;</p> 在此处输入图像描述

有没有办法在放置<br>时保留它<br>

4

2 回答 2

1

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

<?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 );
}
于 2015-10-07T06:14:30.793 回答
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)

于 2015-10-07T02:48:56.567 回答