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 am writing a plug-in that creates new front/home page from scratch. Below code works nice and it outputs the text before the themes outputs anything. So the question is how can I remove the theme creating any html code?

Basically I want the plug-i to start with blank home page regardless of any theme that is currently active.

  add_action('wp_head', 'new_front_page');
    function new_front_page(){
     if (is_front_page()){
       echo 'is_front_page():'.is_front_page();
     }
    }
  }

I am testing this on wp 3.6 and twenty thirteen.

Of course anyone is welcomed to suggest better what how to do that.

share|improve this question
add comment

1 Answer

up vote 1 down vote accepted

If you want to replace the theme home page, you cannot use wp_head hook: it is called from a theme file (usually header.php) so you can't replace something that is already included.

I suggest you to create a php file that contain your custom home page, and put it in a plugin folder. This file, e.g. 'my_custom_home_page.php', has to create the entire html for the page.

You can't use function like get_header or get_footer because this function include theme template files...

Following code is just a starting point. Using it, the plugin file 'my_custom_home_page.php' is included when the front page is required.

After return you have to put the full path of the custom home page file. The code I used works if the code is in the main plugin file and 'my_custom_home_page.php' is in the same folder. If you have a different folder structure or the code is elsewhere change the path according to it.

add_filter( 'template_include', 'my_replace_home_page' );

function my_replace_home_page( $template ) {
  if ( is_front_page() ) {
    return plugin_dir_path( __FILE__ ) . 'my_custom_home_page.php';
  }
  return $template;
}
share|improve this answer
    
Works smoothly. Thank you. So now I have to take care of <!DOCTYPE html> , <head> etc by myself? –  Radek Aug 17 '13 at 10:25
1  
Sure. All the page depends on you: if you want your plugin be theme agnostic you can't rely on existing theme. A side note: consider that theme always have template tag like wp_head() and wp_footer() and a lot of plugins assume these template tags are called in every site page. So consider adding these template tags in your custom page for other plugins compatibility. –  G. M. Aug 17 '13 at 10:39
    
If I want to use jQuery, how can I do that? –  Radek Aug 17 '13 at 11:27
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.