我有一个WP的问题,需要你的帮助。我的一些帖子已经应用了“wp\\u autop”过滤器。此筛选器将所有双断开线转换为<p>
标签我想做相反的事:全力以赴<p>
标记为双分隔线。
你有什么建议吗?非常感谢。
我有一个WP的问题,需要你的帮助。我的一些帖子已经应用了“wp\\u autop”过滤器。此筛选器将所有双断开线转换为<p>
标签我想做相反的事:全力以赴<p>
标记为双分隔线。
你有什么建议吗?非常感谢。
我只是遇到了这种情况。这里有一个我用来撤消wpautop的函数。我可能错过了什么,但这是一个好的开始:
function reverse_wpautop($s)
{
//remove any new lines already in there
$s = str_replace("\\n", "", $s);
//remove all <p>
$s = str_replace("<p>", "", $s);
//replace <br /> with \\n
$s = str_replace(array("<br />", "<br>", "<br/>"), "\\n", $s);
//replace </p> with \\n\\n
$s = str_replace("</p>", "\\n\\n", $s);
return $s;
}
我也需要一种方法来做到这一点,但对现有的任何解决方案都不满意,所以决定做一个。希望它能帮助别人。
<?php
/**
* Replaces paragraph elements with double line-breaks.
*
* This is the inverse behavior of the wpautop() function
* found in WordPress which converts double line-breaks to
* paragraphs. Handy when you want to undo whatever it did.
*
* @see wpautop()
*
* @param string $pee
* @param bool $br (optional)
*
* @return string
*/
function fjarrett_unautop( $pee, $br = true ) {
// Match plain <p> tags and their contents (ignore <p> tags with attributes)
$matches = preg_match_all( \'/<(p+)*(?:>(.*)<\\/\\1>|\\s+\\/>)/m\', $pee, $pees );
if ( ! $matches ) {
return $pee;
}
$replace = array( "\\n" => \'\', "\\r" => \'\' );
if ( $br ) {
$replace[\'<br>\'] = "\\r\\n";
$replace[\'<br/>\'] = "\\r\\n";
$replace[\'<br />\'] = "\\r\\n";
}
foreach ( $pees[2] as $i => $tinkle ) {
$replace[ $pees[0][ $i ] ] = $tinkle . "\\r\\n\\r\\n";
}
return rtrim(
str_replace(
array_keys( $replace ),
array_values( $replace ),
$pee
)
);
}
https://gist.github.com/fjarrett/ecddd0ed419bb853e390
Bonus: 您还可以使用此选项来确定内容是否已被wpautop
.$is_wpautop = ( $content !== fjarrett_unautop( $content ) );
查看帖子或其他内容时,筛选器不在数据库中写入,在前端进行筛选。您可以停用过滤器并为设置自定义标记编写自己的过滤器。
WordPress中有一个内置函数,但只有在post编辑器中从Visual切换到HTML时,才会触发JavaScript(但如果实际使用HTML,则会出现错误)。如果只是普通的帖子内容(不是html代码),那么您可以编辑每篇帖子,来回切换编辑器,然后保存。
这可能是最劳动密集但最安全的方式。
我写了一个插件,可以修改一些评论的内容。它使用pre\\u comment\\u内容过滤器。使用标准WP调节似乎可以正常工作,但是,当我打开IntenseDebate增强调节时就不行了。以下是一些代码:function my_plugin($orig_comment){ $orig_comment = some_func($orig_comment); return $orig_comment; } 编辑:插件基本上只应用一些格式,比如说所有字母