Inserting code after each post in WordPress

强颜欢笑 提交于 2019-12-02 11:35:40
edelwater

Here is an example of a filter on the content and the function:

    function the_content_replacer($content) 
    {
//global $post, $posts;
       //$content = str_replace(']]>', ']]>', $content);

 $content .= "\n<div style=\"display:none;\">text here</div>"; 

//$content = preg_replace('/="http:\/\/cnn/i', 
// '="http://example.com?http://cnn', $content, -1); 
       return $content;
    }
    add_filter('the_content', 'the_content_replacer', 1);
  1. much more examples on this filter on http://wordpress.stackexchange.com ..........
  2. You can just copy and paste the piece of content in the file "functions.php" in your theme and it will work.
  3. You can also just drop it in the directory wp-content/mu-plugins if your run multisite so it works on all the blogs in your multisite environment.
  4. the third parameters determines the importance of when applying the filter, see: https://wordpress.stackexchange.com/questions/2126/at-what-priority-does-add-filter-overwrite-core-functions

--> it is better to post all WordPress questions in http://wordpress.stackexchange.com !!!

--> if you use e.g.

$content .= "\n<div style=\"display:none;\">text here</div>";

it will not remove a closing paragraph tag (note the linebreak at the beginning of the string)

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!