Support » Plugins and Hacks » Detect more link when filtering the_content

  • Resolved jakemc

    (@jakemc)


    I’m trying to write a plugin that will display the content of a post up to the <!–more–> tag for all users (on the archive page) but that will only display the full post (on the single page) for registered users.

    I’m trying to do this by showing the archive page as normal, but on the single page showing the content up to the <!–more–> tag and replacing the rest if the user isn’t registered/logged in.

    However, it appears the <!–more–> link is processed before the ‘the_content’ filter is applied. Is there a way to catch the content first? Or is there another way to determine (on the single page) where the break occurs? Or is there a better way to achieve what I want?

    Thanks in advance.

Viewing 6 replies - 1 through 6 (of 6 total)
  • I guess you’d have to catch the $post->post_content, then tests which display you want and replace the <!–more> tag if needed with a str_replace. Something like:

    $myposts = get_posts();
    foreach ($mypost as $post):
    if ($test_register_user)
         str_replace ("<!--more-->","",$post->post_content);
    the_content();
    endforeach;

    Thanks alakhnor, but that doesn’t quite work

    If I replace the_content() in my theme with alt_the_content() and define:

    function alt_the_content () {
    	global $post;
    	str_replace("<!--more-->","THIS IS WHERE THE MORE LINK IS",$post->post_content);
    	the_content();
    }

    … the_content() still carries on as normal and strips out the <!–more–>. I can replace <!–more–> if I use $post->post_content but then have to echo $post->post_content and lose all HTML formatting.

    Any more ideas (and I’d still rather use a filter so I can leave the_content() be in my theme…)?

    Sorry that should be:

    function alt_the_content () {
    	global $post;
    	$post->post_content = str_replace("<!--more-->","MORE LINK",$post->post_content);
    	echo $post->post_content;
    }

    This replaces the <!–more–> but removes all formatting.

    function alt_the_content () {
    	global $post;
    	$post->post_content = str_replace("<!--more-->","MORE LINK",$post->post_content);
    	the_content();
    }

    This displays the_content() as normal and doesn’t seem to replace the <!–more–>

    Incidentally, I want to replace <!–more–> *and all the text that comes after*. Should I use preg_replace or is there a more elegant way to do this?

    Thanks again.

    Try using $page instead of $post->post_content in the str_replace.

    Thanks alakhnor.

    Do you mean:

    function alt_the_content () {
    	global $post;
    	$page = str_replace("<!--more-->","MORE LINK",$page);
    	the_content();
    }

    ?

    Same result 🙁

    Any more suggestions? Even some documentation about how the_content() is processed might be helpful…

    I’ve found this which seems to do exactly what I need:

    MarketingTrick release 1.0

    Thanks anyway!

Viewing 6 replies - 1 through 6 (of 6 total)
  • The topic ‘Detect more link when filtering the_content’ is closed to new replies.