Title: exactly how does WordPress start?
Last modified: February 9, 2017

---

# exactly how does WordPress start?

 *  Resolved [hyacinthus](https://wordpress.org/support/users/hyacinthus/)
 * (@hyacinthus)
 * [9 years, 4 months ago](https://wordpress.org/support/topic/exactly-how-does-wordpress-start/)
 * This probably sounds like a dumb question, but here goes. I understand basically
   how the themes work and I’m working with one called portfolio-press and am either
   going to make a child theme of it or else write my own. Where I am having trouble
   is understanding how WordPress initializes and which files get called prior to
   index.php because I get a different page when things start up compared to when
   I select HOME from my navigation menu. My question, I think, revolves around 
   when the theme gets loaded and initialized. I can find the init routine for portfolio-
   press in functions.php, but when does it get called? This is something that the
   tutorials on writing a theme don’t mention or at least I haven’t found it. Anybody
   have a link to initialization? Thanks.

Viewing 7 replies - 1 through 7 (of 7 total)

 *  Moderator [Steven Stern (sterndata)](https://wordpress.org/support/users/sterndata/)
 * (@sterndata)
 * Volunteer Forum Moderator
 * [9 years, 4 months ago](https://wordpress.org/support/topic/exactly-how-does-wordpress-start/#post-8786636)
 * This may help you:
 * [https://codex.wordpress.org/Plugin_API/Action_Reference](https://codex.wordpress.org/Plugin_API/Action_Reference)
 *  Thread Starter [hyacinthus](https://wordpress.org/support/users/hyacinthus/)
 * (@hyacinthus)
 * [9 years, 4 months ago](https://wordpress.org/support/topic/exactly-how-does-wordpress-start/#post-8788117)
 * well, yes and no. Now I understand a bit better what “hooks” are. Some of the
   terminology is baffling. But I’ve tracked the beginning of portfolio-press to
   template_loader.php and it seems it includes the portfolio-press index.php directly
   by the following.
 *     ```
       if ( $template = apply_filters( 'template_include', $template ) ) {
       		include( $template );
       	} elseif ( current_user_can( 'switch_themes' ) ) {
       		$theme = wp_get_theme();
       		if ( $theme->errors() ) {
       			wp_die( $theme->errors() );
       		}
       	}
       ```
   
 * Why is there an assignment statement within the if() clause? This is a WordPress
   file, not part of the theme itself. At this point the $template variable is set
   to portfolio-press/index.php and things go from there.
 *  Thread Starter [hyacinthus](https://wordpress.org/support/users/hyacinthus/)
 * (@hyacinthus)
 * [9 years, 4 months ago](https://wordpress.org/support/topic/exactly-how-does-wordpress-start/#post-8788168)
 * Does it matter where the following occurs?
    `add_action( 'after_setup_theme','
   portfoliopress_setup' );` Reason I ask is that it currently follows the function
   inside function.php and I haven’t yet discovered when function.php is loaded.
 *  Moderator [bcworkz](https://wordpress.org/support/users/bcworkz/)
 * (@bcworkz)
 * [9 years, 4 months ago](https://wordpress.org/support/topic/exactly-how-does-wordpress-start/#post-8788487)
 * As long as you add the action before “after_theme_setup” fires and your function
   call is actually executed (anywhere on functions.php outside of function declarations
   will work), it doesn’t matter where in the sequence it occurs. Once you fully
   understand hooks, you will understand why this is so.
 * The entire WP initialization process is explained in [Query Overview](https://codex.wordpress.org/Query_Overview).
   It glosses over how we get to the theme’s functions.php though. The linked [humanshell.net](http://humanshell.net/2011/08/wordpress-initialization/)
   article goes over the same in excruciating detail. If you are adept at reading
   PHP code, going through wp-settings.php concisely covers the same terrain. The
   theme functions.php is loaded at line 420 in the 4.7.2 version.
 * The assignment inside an IF conditional is just an expedient shortcut. We could
   assign $template first on its own line, then do `if ( $template ) {`. The end
   result is the same.
    -  This reply was modified 9 years, 4 months ago by [bcworkz](https://wordpress.org/support/users/bcworkz/).
 *  Thread Starter [hyacinthus](https://wordpress.org/support/users/hyacinthus/)
 * (@hyacinthus)
 * [9 years, 4 months ago](https://wordpress.org/support/topic/exactly-how-does-wordpress-start/#post-8791182)
 * Thanks for the direction. I would have looked a long time before finding that
   info. The article at humanshell.net was extremely helpful.
    What I understand
   about hooks is that it is a way to connect events within WP to functions. I’ve
   worked a lot with event driven programming but the way these things are connected
   in WP is new to me. How does one know when certain events will fire? I won’t 
   belabor the point about the assignment statement in the if clause. My goal is
   to understand WP, not to be a troll. But if I had done that at my old day job,
   my boss would have given me a very stern lecture and made me fix it. 😉
 *  Moderator [bcworkz](https://wordpress.org/support/users/bcworkz/)
 * (@bcworkz)
 * [9 years, 4 months ago](https://wordpress.org/support/topic/exactly-how-does-wordpress-start/#post-8791824)
 * Heh, you don’t have to convince me about the place for assignments. It also violates
   current core coding standards. There’s nothing specific per se, but it goes against
   the “No tricky shortcuts” guideline IMO. However, some code has been in place
   for a long time and cleaning up old code is a very low priority.
 * It’s often difficult to know when some hooks fire. All you can do is locate where
   it is applied in source, then deduce when by the surrounding context. For low
   level code that typically runs several levels down the call stack, this can be
   very ambiguous. The “Used by” section of the code reference is useful for climbing
   back out of the stack to get an understandable context.
 * I usually attack the problem from the other direction. I need to alter ‘X’, so
   I locate where ‘X’ is applied, then try to find any hook before then that will
   allow me to do what I need. Sometimes this means drilling down several levels
   of function calls. It seems there’s always something. By approaching the problem
   this way, the context of when the hook fires is self apparent.
 *  Thread Starter [hyacinthus](https://wordpress.org/support/users/hyacinthus/)
 * (@hyacinthus)
 * [9 years, 4 months ago](https://wordpress.org/support/topic/exactly-how-does-wordpress-start/#post-8792472)
 * Many thanks for your help. I’ve got a lot to chew on and study for a while.

Viewing 7 replies - 1 through 7 (of 7 total)

The topic ‘exactly how does WordPress start?’ is closed to new replies.

 * In: [Developing with WordPress](https://wordpress.org/support/forum/wp-advanced/)
 * 7 replies
 * 3 participants
 * Last reply from: [hyacinthus](https://wordpress.org/support/users/hyacinthus/)
 * Last activity: [9 years, 4 months ago](https://wordpress.org/support/topic/exactly-how-does-wordpress-start/#post-8792472)
 * Status: resolved

## Topics

### Topics with no replies

### Non-support topics

### Resolved topics

### Unresolved topics

### All topics
