2016-05-12 10 views
3

私は自分で作成したものではなく、ウェブからウェブコンテンツを取得し、ワードプレスのポストに表示するプラグインを持っています。Wordpress - 閲覧時に投稿コンテンツを検索して置換する

問題は、アプリの説明には、<br \>のテキストが表示されています。プラグインはすべてを\nに変換します。

私は、閲覧時に書き込まれた投稿を傍受し、\n<br />に置き換える1行のプラグインを構築する考えがありました。

私は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; 
} 

間違っていますか?私は基本的にウェブ上の投稿からのレシピに従っています。

答えて

1

私はポストオブジェクトに直接変更を行う試してみた:

add_action('the_post', 'replace_newline'); 

function replace_newline($post) { 
    $post->content = str_replace("\n", "<br>", $post->post_content); 
} 
+0

BRILLIANT!感謝 – SpaceDog

関連する問題