1

I'm developing a theme for WordPress. I have a doubt about templates redirect. For example, I have a theme that have the front-page.php template and I don't use the index.php template. What I should do it with index.php file? Delete it or redirect like:

index.php

<?php

wp_safe_redirect('front-page.php');
exit();

The redirect affects the performance of the site? Should I left the template blank If I don't use it?

I always have this doubt about templates. If I don't use archive.php template for example, I should delete it or redirect to the template I use? I don't know what to do with the underscore theme files (the files I don't use).

Template Hierarchy: https://developer.wordpress.org/files/2014/10/Screenshot-2019-01-23-00.20.04.png

Underscores base theme: http://underscores.me/

0

I don't see any reason to redirect index.php. Let's say you only have a Front Page, no blog or post types with archives. You would assign the Front Page in Settings -> Reading and WordPress will do the template redirect for you. At that point, nothing uses index.php and there's no need to redirect it.

Maybe you install a plugin which create a post type and, by default, uses index.php to display its posts. You'll want something to display instead of redirecting it otherwise you may spend time trying to figure out why the archive of this plugin is redirecting. Copy / Paste the page.php template and throw some baseline styles into it.

If you do have a post type archive or blog and want to keep the single posts but do not want an archive you can use the template_redirect hook to redirect the request:

/**
 * Redirect Archive Pages
 *
 * @return void
 */
function prefix_redirects() {

    if( is_post_type_archive( 'foobar' ) ) {
        wp_safe_redirect( home_url() );
        exit();
    }

}
add_action( 'template_redirect', 'prefix_redirects' );

The tools are there if you really need them but since index.php is the default display for post type archives it's not something I would redirect but instead add some baseline styles to. You can always do more specific template overrides later if you need to.

  • Setting has_archive to false when registering the post type would be far simpler. – Jacob Peattie May 23 at 14:48
  • So, index.php is a required theme file as the theme handbook says, so I will put "<?php //silence is golden" like they do in their empty index.php and if Wordpress misredirect some page to index.php for example because of a plugin, I will use the hook you said: 'template_redirect', I think It solves the problem. – Lucas Vendramini May 23 at 14:54
  • If you're trying to use another template, you should be using the template_include hook. My point is that you shouldn't be redirecting index.php at all in most cases. – Howdy_McGee May 23 at 14:56
1

Don't redirect in templates to other templates. That's not how templates work. Notice in your browser that you're never redirected to single.php or front-page.php or anything like that. If you do this then if you load a page that uses index.php your browser is going to redirect to http://website.com/front-page.php and you'll get a 404.

Templates are loaded with include (or require) in PHP by WordPress to render the page as part of a process involving many WordPress files. They are not a loaded directly, and they are not redirected to.

The first thing to note for your specific example is that index.php is a required file for WordPress themes. So you can't delete it, and you shouldn't redirect from it. So if your front-page.php and index.php templates are the same, then you should be deleting front-page.php, not index.php.

You should start your theme with index.php, which should be the template for a generic list of posts. Then you start adding templates based on your design requirements, by referring to the Template Hierarchy and adding the ones you need.

However, let's say I have a custom post type whose archive template (eg. archive-project.php) needs to be the same as the taxonomy archive template for its taxonomy (eg. taxonomy-project_category.php). Both post type archives and taxonomy archives fall back to archive.php. The problem is that if you're already using that for regular posts, you can't create a single template for both types of page.

The simplest solution is to load one template into the other using get_template_part(). So archive-project.php could be your main template, and then taxonomy-project_category.php could look like this:

<?php get_template_part( 'archive-project' ); ?>
  • The problem of delete the front-page.php template is that I use page.php template and I use it to display the pages of WordPress (in my case in specific, pages of WooCommerce). So If i delete it I don't get the front-page in the index.php but the page.php. I agree with you, things should start in index.php but sometimes, the template hierarchy is confusing to handle. – Lucas Vendramini May 23 at 14:59
  • What have you got your front page set to in Settings > Reading? If you want a list of posts you should not have it set to a page. Then index.php will load. – Jacob Peattie May 23 at 15:00
  • If I want to display a list of posts, I should use the home.php. I use the template for front-page.php because is a custom front page, isn't the right template? – Lucas Vendramini May 23 at 15:03
  • Ok, so I’m not sure what index.php has to do with anything then? Since your brought it up in your question. – Jacob Peattie May 23 at 16:17
  • index.php is a obrigatory file you should put in the theme, read the docs: developer.wordpress.org/themes/getting-started/what-is-a-theme – Lucas Vendramini May 24 at 13:27
1

index.php can be <?php //silent here. Doesn’t matter at the theme level really. Unless you want to guarantee something before your functions.php file it’s obsolete in the theme directory. Just like anything else in php/html on apache servers anyway.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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