0

I'm developing my first WordPress theme and had a question about best practice when it comes to a few things.

Including templates: when I create a front-page.php file and want to include a header.php file, is it best practice to have:

  1. front-page.php in the /theme/ directory and header.php in /theme/core/ directory, then using <?php include 'core/header.php; ?> to include it

Or

  1. front-page.php and header.php in the same /theme/ directory and then using <?php get_header(); ?> to include it

Same for get_footer, get_sidebar etc.

4

Using include makes it impossible to child theme that file. You will also find that some things that depend on global variables will not work. This is because the function that loads templates also globalizes a bunch of variables. You will also not trigger the actions attached to get_header, get_footer, and get_sidebar, which some plugin out there might depend on. Always using the API when possible will maximize the compatibility of your theme with the rest of the WP ecosystem.

0

What defines which files would be "core" and which files wouldn't? Sidebar and a Front Page template sound like "core" to me. Unless there's some kind of framework involved I don't see why most template files wouldn't be in the root. You could if you wanted to separate things like Page Templates or Template Parts into folders.

WordPress uses their "Twenty 20xx" themes as an example of "good development" so for most things you could use that as a basis for best practices. It suggests something like the following:

assets (dir)
      - css (dir)
      - images (dir)
      - js (dir)
inc (dir)
template-parts (dir)
      - footer (dir)
      - header (dir)
      - navigation (dir)
      - page (dir)
      - post (dir)
404.php
archive.php
comments.php
footer.php
front-page.php
functions.php
header.php
index.php
page.php
README.txt
rtl.css
screenshot.png
search.php
searchform.php
sidebar.php
single.php
style.css

That being said, if you find that something is easier to organize in your theme then I say go for it, nothing is really set in stone.

0

If WordPress has a specific function to do something (i.e., get_header()), then use that function whenever possible, rather than straight PHP. There's generally a reason for those WP functions.

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.