As a proof of concept, I'd like to create a simple plugin that loads some content, say "hello world" just after the_content on the home page only. How can I do this from a plugin?
1 Answer
You mean a filter and a check for is_home()
?
add_filter( 'the_content', 'wpse6034_the_content' );
function wpse6034_the_content( $content )
{
if ( is_home() ) {
$content .= '<p>Hello World!</p>';
}
return $content;
}
-
Jan, can you tell me how you might alter your example, if I only want my plugin to filter content when the home page is viewed? This example works, but it runs the filter on every page.– Scott BCommented Jan 6, 2011 at 12:53
-
@Scott: Then you want to run
add_filter()
only whenis_home()
is true. Remember that you can't check foris_home()
only after the query has run, so probably in theinit
hook? Commented Jan 6, 2011 at 13:39 -
Any idea if I want to do something similar? Simple plug-in that creates
new custom
home page. Nothing comes from active theme. wordpress.stackexchange.com/questions/110118/…– RadekCommented Aug 17, 2013 at 7:11 -
1Perhaps useful to the next guy, it took me awhile to realize you don't want to
$content = get_the_content()
and then append to that (which is not what this example shows either). Doing so will mess things like auto-embeds up.– NathanCommented Nov 24, 2020 at 20:31