Take the 2-minute tour ×
WordPress Development Stack Exchange is a question and answer site for WordPress developers and administrators. It's 100% free, no registration required.

I have a plugin that I only want to execute when the home page is being viewed. How would I stub out my plugin in order to make this happen?

This is does not work...

 <?php
 /*
 Plugin Name: My Test Plugin
 */


 if ( is_home() OR is_sticky() )
 {
  add_filter( 'the_content', 'my_function' );
 }

 function my_function( $content )
 { 
  echo "hello world".$content;
 }

 ?>

This is my original code, but it executes the filter on every page. Which seems overkill to me, since I only want the plugin to run on the home page anyway.

 add_filter( 'the_content', 'wpse6034_the_content' );
 function wpse6034_the_content( $content )
 {
  if ( is_home() ) {
   $content .= '<p>Hello World!</p>';
  }
  return $content;
 }
share|improve this question
add comment

1 Answer

up vote 4 down vote accepted

You need to add the filter later:

function _add_my_filter() {
 if ( is_home() OR is_sticky() )
 {
  add_filter( 'the_content', 'my_function' );
 }
}
add_action('template_redirect', '_add_my_filter');
share|improve this answer
    
What if I want to display only output from the plugin on home page. Nothing from active theme should be displayed. Any idea? wordpress.stackexchange.com/questions/110118/… –  Radek Aug 17 '13 at 7:09
add comment

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

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