Take the 2-minute tour ×
WordPress Development Stack Exchange is a question and answer site for WordPress developers and administrators. It's 100% free, no registration required.

I'm looking for an efficient way to group a series of separate but related functions for a child theme I'm designing using theThematic Theme Framework. Thematic does a great job of providing plenty of action hooks and filters for child theme development, which I'm trying properly utilize. I'm making a lot of changes to the home page, so I have created a series of functions which alter specific content areas but I only want to execute these functions on my site's index page. Previously my functions.php file has looked a lot like this:

<?php
     function home_function_a() {
          if ( is_front_page() ) {
               //Something Near the top of the Home Page
          }
     }
     add_action('thematic_belowheader', 'home_function_a');

     function home_function_b() {
          if ( is_front_page() ) {
               //Something in in the sidebar
          }
     }
     add_filter('thematic_sidebar', 'home_function_b');        

     function home_function_c() {
          if ( is_front_page() ) {
               //Something near the bottom of the page
          }
     }
     add_action('thematic_abovecomments', 'home_function_c');

     // etc, etc, etc

 ?>

The above technique works but strikes me as somewhat inefficient. I was thinking it might be nice to use a nested function approach as shown below:

<?php
    function everything_on_the_home_page() {
         if ( is_front_page() ) {

              function home_function_a() {
                   //Something near the top of the Home Page
              }
              add_action('thematic_belowheader', 'home_function_a');

              function home_function_b() {
                   //Something in the sidebar of the Home Page
              }
              add_filter('thematic_sidebar', 'home_function_b');

              function home_function_c() {
                   //Something near the bottom of the Home Page
              }
              add_action('thematic_abovecomments', 'home_function_c');

              // etc, etc, etc              
         }
    }
    add_action('thematic_aboveheader', 'everything_on_the_home_page'); 
 ?>

This code looks a little nicer and seems to work properly, but I wanted to double check to make sure this technique is recommended and Kosher. I tried a couple google searches for articles/forum posts about this approach, but I couldn't find anything helpful. Please let me know if you think my nested function tree is a good way to solve my problems or if you have any other suggestions.

Thanks for your time! Best, Jonathan

share|improve this question

1 Answer 1

It might look more elegant, but you don't want to nest hooks in this way. You're now ending up with actions added inside a logic check inside another action hook.

Instead, keep your functions as granular and atomic as possible and only hook them in where needed. Your first technique isn't inefficient at all, it's the way you should be doing things.

One major consequence of changing things up is the order of operations.

In your first example:

When firing the themeatic_sidebar hook, call the home_function_b() function. Inside the function, check to make sure you're on the front page before doing something.

In your second example

When firing the thematic_above_header hook, check to see if you're on the front page. Then, wait for the thematic_sidebar hook. Then call the home_function_b() function.

The logic between each example is very different. Let's say home_function_b() needed to do one thing if is_front_page() is true and something else if it's false ... you second example wouldn't allow that at all.

share|improve this answer
    
Thanks a lot! Your explanation was really helpful. –  jfudem Aug 6 '12 at 23:19

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

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