WordPress.org

Ready to get started?Download WordPress

Forums

[resolved] Detect more link when filtering the_content (7 posts)

  1. jakemc
    Member
    Posted 7 years ago #

    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.

  2. alakhnor
    Member
    Posted 7 years ago #

    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;
  3. jakemc
    Member
    Posted 7 years ago #

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

  4. jakemc
    Member
    Posted 7 years ago #

    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.

  5. alakhnor
    Member
    Posted 7 years ago #

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

  6. jakemc
    Member
    Posted 7 years ago #

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

  7. jakemc
    Member
    Posted 7 years ago #

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

    http://www.ayanev.com/marketingtrick-release-10-887/

    Thanks anyway!

Topic Closed

This topic has been closed to new replies.

About this Topic

Tags