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've been searching for a proper solution to override functions and hooks in my parent theme's functions.php. My parent theme has:

    define('TS_INC', TEMPLATEPATH .'/inc');
    require_once( TS_INC .'/file1.php');
    require_once( TS_INC .'/file2.php');

I want to have in my child theme an /inc folder with file2.php changed according to my needs. This means that I want every call to file1.php to reach the parent theme /inc folder and calls to file2.php to go to my child/inc folder.

Thanks in advance.

share|improve this question
    
This is not a parent theme, it is a standalone theme. Contact the theme author to fix it. –  toscho Feb 28 '12 at 16:01
add comment

1 Answer 1

You can only override functions in a Parent Theme if the Parent Theme makes functions pluggable (by wrapping them in an if ( ! function_exists( 'function_name' ) ) conditional), or if the output of a function is passed through a filter (e.g. return apply_filters( $filter_name, $function_output );

  1. Pluggable functions: in the Child Theme's functions.php file, simply define a function with the same name as the pluggable function you want to override. This is often the case with, for example, a Theme's setup function, e.g. twentyeleven_setup().
  2. Filterable functions: in the Child Theme's functions.php file, define a callback, and then call add_filter( $filter_name, $callback );

If the Parent Theme doesn't provide either of these two methods, then the Parent Theme's functions cannot be overridden in a Child Theme.

Edit

Just for kicks and grins, since WordPress loads the Child Theme's functions.php file immediately before the Parent Theme's functions.php file, try adding the following to your Child Theme's functions.php:

<?php
define( 'TS_INC', get_stylesheet_directory() .'/inc' );
?>

It's worth a shot, anyway.

share|improve this answer
    
I think these are not relevant in my case, as my parent functions.php has a "define" which I can't see how to override the way you proposed. –  Nona Man Feb 28 '12 at 15:23
    
Please see the 3 line code bit I put in my question - it defines a TS_INC to shortcut to the /inc folder. –  Nona Man Feb 28 '12 at 16:16
1  
Actually, they are relevant. In PHP, the defined value of a constant can't be changed during execution, so you can't simply redefine the include path. Thus, you're left with overriding the functions in the included files. –  Chip Bennett Feb 28 '12 at 16:16
    
OK - discussion concluded on that. Is there a way for to send it first looking in my child theme for the include and then back to the parent theme if that certain file is not available? –  Nona Man Feb 28 '12 at 16:21
    
See updated answer. It might work. :) –  Chip Bennett Feb 28 '12 at 16:37
show 2 more comments

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.