For a plugin, I need to build my own very early post content and comment content filter. Post content/text changing works, i.e. the modifications end up in the client's browser. But my comment content/text modifications somehow are not persistent, i.e. the client receives the original comment text.
The point in time I "hook in" is defined by the template_redirect hook. I then execute something like this:
global $wp_query;
// Iterate over all posts in this query.
foreach ($wp_query->posts as $post) {
// Edit post text
$post->post_content = "foo"; // works: ends up at the client
// Iterate over all approved comments belonging to this post
$comments = get_approved_comments($post->ID);
foreach ($comments as $comment) {
// Edit comment text
$comment->comment_content = "bar"; // this one is lost
}
}
As indicated via the source comments above, $post->post_content = "foo";
has effect in this context, but $comment->comment_content = "bar";
doesn't.
To track this down at least a bit more, I applied a debug filter to the post content and to the comment content:
add_filter('the_content', 'var_dump');
add_filter('comment_text', 'var_dump');
After the content modification routine above, these filters print "foo" in case of post content, but the comment content is printed unchanged (the original content is printed).
Hence, $comment->comment_content = "bar";
seems to be a local modification, while $post->post_content = "foo";
works as desired: globally.
Or is the database even queried two times for comments so that my modification somehow gets overwritten at some point?
I've tried to work with $wp_query->comments
, too. But this variable is NULL
at the point in time I want and need to hook in.
The final and primary question is:
In my loop above, what do I have to do, to modify the comment content persistently?