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.
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.
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. It glosses over how we get to the theme’s functions.php though. The linked humanshell.net 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.
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. π
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.
Many thanks for your help. I’ve got a lot to chew on and study for a while.