Proper way to include PHP files in WordPress theme

I am trying to develop a WordPress theme which requires a lot of PHP functions and custom classes.

I am wondering what the best practice is for including additional PHP files within WordPress. Example: Do I list every file to include using:

Read More
  • include();
  • require_once()
  • etc

When I review other developers themes I never stumble across a batch of “include() statements, so I believe I must be missing some standard method of performing these includes.

Related posts

Leave a Reply

5 comments

  1. You can follow any method. But I suggest you to use require to include any file.

    _s ( aka underscores ) which is theme starter kit and default themes of WordPress such as TwentyFourteen, TwentyThirteen, etc are using require to include files.

    See sample code used in TweentyFourteen theme.

    require get_template_directory() . '/inc/template-tags.php';
    
  2. Include will include the file content

    Require will throw an error if file not found.

    Include_once .. check if included if no then include

    Require_once same as include_once but with error if file not found

    So if you are the one who wrote the file you should include .. not include_once.

    We usually, use include for unnecessary files. Require for important files. For example footer.php will not make script stop .. but core.php is important.

  3. Why not put all the included files into a single file (library.php) and include that in your theme? By doing this if you need to change something you can change just one file. Also you can use conditional tags like if..else within the library.php to include specific files for certain pages.

    I would also suggest that you use require_once() for important files so you get an error if the file is missing. If there is no error when a file is missing then functions that use that file will through multiple errors.

    Also as @Othman has suggested you can use include for less important files.

  4. Another method would be assigning a
    file and include it into your functions.php to avoid redundancy

    For Example my functions.php in my root folder
    where i get my files from the /inc dir and the functions dir/

    include('inc/assets.php'); //Where i register all css and js
    include('functions/index.php'); // Where are all functions are registered
    

    There is also another method on the wordpress codex
    https://codex.wordpress.org/Theme_Development#Including_Template_Files

    <?php get_template_part('template-parts/blog', 'content'); ?>
    

    Also checkout the wordpress organizing theme files docs
    https://developer.wordpress.org/themes/basics/organizing-theme-files/

    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