0

I am using this code in functions.php to conditionally load some shortcodes but it doesn't work. The shortcodes are still loaded on template-flat.php. Any ideas what's wrong here?

if( is_page_template( 'template-flat.php' ) ) {

// other code

}else{

require_once(functions . '/shortcodes.php');

}

I think it's because the page call happens after the functions call in the wordpress actions order?

1

You need to run this code in an action hook rather than directly in the functions.php file. Take a look at the Action Reference page in the Codex and determine at which point you want this script to run. Whatever action you choose will end up looking something like this:

add_filter( 'template_include', 'check_page_template', 99 );

function check_page_template( $template ) {
  if( is_page_template( 'template-flat.php' ) ) {
    // other code
  } else {
    require_once(functions . '/shortcodes.php');
  }
  return $template;
}
0

Solution

As I alluded to in the comments on your earlier question, the Codex entry for conditional tags details,

You can only use conditional query tags after the posts_selection action hook in WordPress (the wp action hook is the first one through which you can use these conditionals). For themes, this means the conditional tag will never work properly if you are using it in the body of functions.php, i.e. outside of a function.

That in mind, you need to refer to the Action Reference to find a hook after posts_selection suitable for your purposes, then use conditionals in a function attached to that hook. I recommend brushing up on the WordPress Plugin API to get a better understanding of how hooks function. Smashing Magazine also provides a decent less-technical intro to hooks, though it is slightly outdated.

In functions.php:

add_action( 'wp', 'wpse177227_load_shortcodes' );

function wpse177227_load_shortcodes() {
  if( ! is_page_template( 'template-flat.php' ) )
    require_once( 'path/to/shortcodes.php' );
}

Mechanism

I think it may be beneficial to break down this code's mechanism somewhat:

  1. WordPress loads the functions.php file
    • function wpse177227_load_shortcodes() {} is a function definition, so PHP simply takes note of the fact that a chunk of code identified by 'wpse177227_load_shortcodes' is available for future reference. The function's body is not evaluated at this point.
    • add_action(); is a function call - the function's logic is evaluated when the call is evaluated - since the call to add_action() is not contained in a function, this means that it is executed as soon as WordPress loads the functions.php file. Called with the arguments wp and wpse177227_load_shortcodes, this instructs WordPress to try and evaluate a function named wpse177227_load_shortcodes() if and when the action wp occurs.
  2. WordPress calls do_action( 'after_setup_theme' ), signifying that the active theme's functions.php file has been evaluated and that any functions contained within the file is are now available for use.
  3. WordPress continues it's loading routine, calling do_action('{action_name}') at specific milestones to provide points at which other code may be evaluated. The order that WordPress executes action hooks is roughly equivelant to the order in which they are listed in the Action Reference for most requests.
  4. After determining and acquiring the data that's needed to produce a response and setting up the WP object, WordPress calls do_action( 'wp' );, signifying that the WordPress environment is established (i.e. all plugins and themes loaded, the WP object is aware of what the end-user is asking for and contains the data needed to build a response).
    1. WordPress sees that a function named 'wpse177227_load_shortcodes' was previously attached to the wp action (via the add_action() call that was evaluated when functions.php was loaded) and attempts to call wpse177227_load_shortcodes();
    2. Since PHP previously encountered the function definition for 'wpse177227_load_shortcodes' when functions.php was loaded, the call succeeds and the function's body is evaluated.
      • WordPress encounters the conditional tag is_page_template(). Since this code was not evaluated until the wp action, WordPress is aware of the nature of the content being processed and can properly answer conditional questions regarding the content.

That's something of a dense (but hopefully not overly-technical) overview, but I hope it might aide you in grasping the nature of hooks. In general, they allow you to defer execution of your code to a specific moment in WordPress's execution. The trick is using the Codex, Action Reference, and other resources to determine the proper hook for your needs.

  • boscho, the explanation is great but your code causes my site to crash. In contrast, the code provided by the other user works fine. Thanks a lot though for clarifying - this is as useful as the answer. – Joanna Mikalai Feb 6 '15 at 0:32
  • Returning rather late, here - but the crash was a missing bracket /shrug – bosco Feb 23 '17 at 0:29

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.