Inserting code after each post in WordPress

本秂侑毒 提交于 2019-12-02 15:05:03


I am wanting to insert some code after each post in WordPress... I know you can do it after this for example, in single.php

<?php the_content(); ?>

However if I do that it puts it in the wrong place.. an example post is here: -- if I put it AFTER the code example above it will be placed AFTER the sociable & facebook links..... I want to put it BEFORE those, so it's RIGHT AFTER the post.

I did some checking & testing.. this code here from post-template.php

function the_content($more_link_text = null, $stripteaser = 0) {
    $content = get_the_content($more_link_text, $stripteaser);
    $content = apply_filters('the_content', $content);
    $content = str_replace(']]>', ']]&gt;', $content);
    echo $content;

It seems the facebook & sociable code is inserted into the output within the apply_filters() function.... though I can't work out where.

Any help on what I am trying to do?


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

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

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

//$content = preg_replace('/="http:\/\/cnn/i', 
// '="', $content, -1); 
       return $content;
    add_filter('the_content', 'the_content_replacer', 1);
  1. much more examples on this filter on ..........
  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:

--> it is better to post all WordPress questions in !!!

--> 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)