3

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 1

7

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;
}
4
  • 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 B
    Commented Jan 6, 2011 at 12:53
  • @Scott: Then you want to run add_filter() only when is_home() is true. Remember that you can't check for is_home() only after the query has run, so probably in the init hook?
    – Jan Fabry
    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/…
    – Radek
    Commented Aug 17, 2013 at 7:11
  • 1
    Perhaps 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.
    – Nathan
    Commented Nov 24, 2020 at 20:31

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.