1

I was looking in my wordpress files and code. There is a file called functions.php. But how it is possible that in every file there are called functions without including the functions.php ?

I learned in school that you have to include files when you want to use something from another page.

I can't find something useful on the Internet. Can somebody explain this to me?

2 Answers 2

1

Take a look at @rdlowrey's answer to How Wordpress Generates HTML for Blog Posts

What Wordpress does -- and indeed many of the popular MVC frameworks -- is route ALL traffic to A Front Controller [wiki] script using an apache .htaccess file.

So when you are accessing any page served by a WP installation, certain functions become available to you, since it's all passing through the same "start point".

To start following the trail, open up index.php in the root directory, and see this:

/** Loads the WordPress Environment and Template */
require( dirname( __FILE__ ) . '/wp-blog-header.php' );

From there, take a look at wp-blog-header.php. It's loading wp-load.php. If there's no wp-config.php, it prompts you to install. If you do have it in your directory, notice it requires wp-settings.php.

In the settings file, you'll see explicitly where your theme's functions.php file gets added into the mix around line 318 (of WP 3.8.1 at least)

// Load the functions for the active theme, for both parent and child theme if applicable.
if ( ! defined( 'WP_INSTALLING' ) || 'wp-activate.php' === $pagenow ) {
    if ( TEMPLATEPATH !== STYLESHEETPATH && file_exists( STYLESHEETPATH . '/functions.php' ) )
        include( STYLESHEETPATH . '/functions.php' );
    if ( file_exists( TEMPLATEPATH . '/functions.php' ) )
        include( TEMPLATEPATH . '/functions.php' );
}
Sign up to request clarification or add additional context in comments.

1 Comment

Thanks, very useful! I'll keep your post in a text file on my pc:D
0

The functions.php file of the active theme is loaded everytime your wordpress site serves a page.

So any function you add there will be loaded and available to use in your template files.

Say your functions.php file looks like this:

<?php
function my_example() {
    echo "<h1>My example</h1>";
}

Then, in your header.php you can call that function like this:

<html>
<head></head>
<body>
    <?php my_example(); ?>
</body>
</html>

You don't need to specifically include (nor include_once) the functions.php file in order to use your just declared function.

2 Comments

An example: I have the file functions.php and the file page.php, i have a function on functions.php and i call it on page.php, how does page.php know that the function is made in functions.php?
It is not page.php that is in control of the situation, but the WordPress app itself, that is bootstrapped and load a ton of things to make your life easier. One of the things it loads is the theme engine, that eventually load functions.php and after that loads any template files such as index.php, header.php, page.php, archive.php and so on.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.