WordPress Plugin: Search <! - more & # 8594; in the_content

I am writing a WordPress plugin that filters the_content and I would like to use the tag <!--more-->

, but it seems like it was removed before it reaches me. This is not a filter, but a WordPress feature.

I could of course resort to reloading the already loaded content from the database, but it looks like this could cause other problems. Is there a good way to get the original content without removing it <!--more-->

?

0


a source to share


3 answers


Most likely, by the time your plugin <!--more-->

was launched, it was converted to<span id="more-1"></span>

This is what I use in my plugin, which injects some markup right after the tag <!--more-->

:



add_filter('the_content', 'inject_content_filter', 999);

function inject_content_filter($content) {
  $myMarkup = "my markup here<br>";
  $content = preg_replace('/<span id\=\"(more\-\d+)"><\/span>/', '<span id="\1"></span>'."\n\n". $myMarkup ."\n\n", $content);
  return $content;
}

      

+6


a source


You can use the following code:

.! is_single () will no longer display the link on the View Post page.



add_filter('the_content', 'filter_post_content');
function filter_post_content($content,$post_id='') {

        if ($post_id=='') {
            global $post;
            $post_id = $post->ID;
        }

        // Check for the "more" tags
        $more_pos = strpos($filtered_content, '<!--more-->');
        if ($more_pos && !is_single()) {
            $filtered_content = substr($filtered_content, 0, $more_pos);

            $replace_by = '<a href="' . get_permalink($post_id) . '#more-' . $post_id 
                    . '" class="more-link">Read More <span class="meta-nav">→</span></a>';

            $filtered_content = $filtered_content . $replace_by;
        }

        return $filtered_content;
    }

      

+1


a source


Building on Frank Farmer's answer , I decided to add a thumbnail photo after the more generated ( <span id="more-...

) tag in the single.php file like this:

// change more tag to post thumbnail in single.php
add_filter('the_content', function($content)
{
    if(has_post_thumbnail())
    {
        $post_thumbnail = get_the_post_thumbnail(get_the_ID(), 'thumbnail', array('class'=>'img img-responsive img-thumbnail', 'style'=>'margin-top:5px;'));
        $content = preg_replace('/<span id\=\"(more\-\d+)"><\/span>/', '<span id="\1"></span>'.$post_thumbnail, $content);
    }
    return $content;
}, 999);

      

0


a source







All Articles