Reputation: 170
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?
Upvotes: 1
Views: 341
Reputation: 5031
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' );
}
Upvotes: 1
Reputation: 643
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.
Upvotes: 0