WordPress.org

Ready to get started?Download WordPress

Forums

Where should I hook to get the more tag recognized? (6 posts)

  1. gallantfish
    Member
    Posted 6 months ago #

    I've been banging my head literally the whole week and I just hit a wall.

    Before asking why get_the_content() makes ANOTHER call to the DB ( as my humble understanding and honest 1 week reading of the wp includes tell me) I'll start asking:

    Where should I inject the filtered $post->post_content so following behaviors that take decisions based on whether the <!--more--> tag is found or not can read it?

    The function works, and it's currently returning the modified post hooked to "the_post" action, retrieving the right post_content… but for no following function to see it!

    I honestly believed a) reading the DB the fewer times was one of the main goals and b) functions were designed to take what previous functions output, so most default filters can be applied to it too.

    As I see it right now I have to replicate the whole "the_content()" behavior and "the_excerpt" too, because it also depends on the more tag.

  2. bcworkz
    Member
    Posted 6 months ago #

    To filter the content, do not hook 'the_post', hook 'the_content'. This way the more tag gets handled without you needing to worry about it.

    get_the_content() hits the DB because that's where the content is stored. It will get the data from the cache instead if it's available. It has no way of knowing if the_content is available outside of it's scope. If it is available, you should be using that instead of get_the_content().

    Filters can only return values to where apply_filters() was called. However, filter callbacks are public functions like any other. You could call that function directly, passing appropriate values, and get the filtered value in return.

  3. gallantfish
    Member
    Posted 6 months ago #

    It will get the data from the cache instead if it's available. It has no way of knowing if the_content is available outside of it's scope

    Can you help me figure out why it's NOT being used?
    It shows available as WP_POST object, but not used.

    This is the test.

    function my_pre_getthecontent_function($post = false) {
    	global $post;// deleting this makes no effect
    	$content = "CHANGED POST";
    	$post->post_content = $content;
    
        return $post; //returning array( "0" => $post) doesn't work either
     }
    add_action('the_post', 'my_pre_getthecontent_function', 10);
    
    function test_post_content ( $content ) {
    	global $post;
    	echo '<pre>'; print_r( $post ); echo '</pre>';
    	return $content;
    }
    add_filter('the_content', 'test_post_content', 10);

    If you test this code (in functions.php) in a single post page, you'll see the "CHANGED" content is not showing with with the_content().

    I'm sure I'm missing something, but what?
    I tried setting blank $post->filter, and to "raw", just in case, no success.

    I can't filter "the_content" because at that stage the more tag has been already not found and I can't replicate the behavior as you said (without creating a whole new get_the_content() with who know which default things come with it)

    To prevent other plugins fail with my mods, I prefer to pass the more tag the earlier possible, so everything else founds it and work as expected.

    So I NEED to find a way to make get_the_content() to get my modified $post.

  4. gallantfish
    Member
    Posted 6 months ago #

    Oh, I just noticed Inside post-template.php line 183, $get_post() is used…
    $post = get_post();
    but $post not globalized for it to get it :(

    I'll try changing the global $pages, and see if get_the_content() takes it from there.

    (I was confused with the codex page for $pages, which said it was an integer, and I was blacking out that part of the code in my brain, not seeing it IS using the content stored there)

  5. gallantfish
    Member
    Posted 6 months ago #

    Ok, it works.
    (above I meant the Codex page for Globals*, where says $pages is an integer :( )

    get_the_content() DOES use the already-retrieved info, not loading "again" from the as I thought. It just does it through the global "$pages".

    There's no filter for that, but I can change the global before it gets to get_the_content()

    I learned a lot :)

  6. bcworkz
    Member
    Posted 6 months ago #

    Good work there tracking down $pages. I did confirm that, because the way 'the_post' works, the post object used by setup_postdata() is essentially read-only which is why your initial efforts did not work. You alluded to this by observing it was not globalized.

    Fortunately, $pages is! I'm going to fix the Codex entry, it is clearly wrong. Thanks for noticing the error, sorry it caused confusion.

Reply

You must log in to post.

About this Topic