我有一个插件,它不是我创建的,它从网络上获取网络内容并显示在wordpress帖子中。
问题是应用程序描述显示的文本没有<br '>
。插件将所有内容转换为'n
。
我有一个想法,构建一个单行插件来拦截在视图时编写的帖子,并用<br />
替换'n
。
我从未为wordpress创建过插件。阅读网页,我得出了这个:
<?php
/*
Plugin Name: Convert 'n in HTML BR
Plugin URI:
Description: Convert 'n in HTML BR
Author: Me
Version: 1.0
Author URI:
*/
function my_function($id) {
$the_post = get_post($id);
$content = str_replace("'n", "<br />", $the_post->post_content);
return $content;
}
add_action('the_post', 'my_function');
?>
但这没有任何效果。
我也试过这个:
add_filter('the_content', 'modify_content');
function modify_content($content) {
global $post;
if ($post->ID != $desired_id)
return $content;
$modified_content = str_replace("'n", "<br />", $the_post->post_content);
return $modified_content;
}
怎么了?我基本上是按照网上帖子中的食谱来做的。
我会尝试直接对post对象进行更改:
add_action( 'the_post', 'replace_newline' );
function replace_newline( $post ) {
$post->content = str_replace( "'n", "<br>", $post->post_content );
}
使用the_content
对象上的过滤器,您可以使用以下代码动态替换任何HTML字符串:
function replace_text($text) {
$text = str_replace('look-for-this-string', 'replace-with-this-string', $text);
$text = str_replace(''n', '<br>', $text);
return $text;
}
add_filter('the_content', 'replace_text');