How to add multiple custom PHP files in WordPress

0

How to call multiple functions from multiple files into a WordPress page template.

Just add your functions to your theme's functions.php file. They will be available to all of the templates within the theme.

function add_cars_specifications() {
    if ( in_category( 'cars' ) ) {
        echo types_render_field( 'cars-specs', array() );
    }
}

In your page template file and other templates in the theme, add_cars_specifications() will be in scope, so call it like this:

<h3>Car Specifications</h3>
<?php add_cars_specifications(); ?>
<p>More text...</p>

You can also include other files from functions.php which themselves contain functions. This makes it easier to organize things. Inside functions.php:

require_once( get_template_directory() . '/includes/template-tags.php' ); 

If you really want to make an include only available inside of a particular page template or templates, you can include the external file from the top of your page template like so:

require_once( get_template_directory() . '/includes/my-functions.php' );

Keep in mind that if you do an include like this, you won't be able to trigger actions and hooks that occur earlier in the execution flow, so for example trying to fire a function attached to the init hook would not work.

Note that get_template_directory() is for parent themes and get_stylesheet_directory() is for child themes.

We will be happy to hear your thoughts

Leave a reply

CodeROG
Logo
Reset Password
Compare items
  • Total (0)
Compare
0
Shopping cart