WordPress only apply funtion on front page

Viewed 15

I have put the below minimal PHP code in the functions.php and it works.

add_action( 'init', 'test' );

function test() {
   echo 'Testing...123';
}

But if I use is_front_page() to specify only apply this on the front page ( front-page.php ), then it won't work, why is that so?

if ( is_front_page() ) {
   add_action( 'init', 'test' );

   function test() {
      echo 'Testing...123';
   }
}

1 Answers

Try by changing it to the following

add_action( 'init', 'test' );

function test() {
    if ( is_front_page() ) {
      echo 'Testing...123';
   }
}
Related