2

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.

1
  • This is not a parent theme, it is a standalone theme. Contact the theme author to fix it. Commented Feb 28, 2012 at 16:01

1 Answer 1

6

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.

7
  • 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. Commented Feb 28, 2012 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. Commented Feb 28, 2012 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. Commented Feb 28, 2012 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? Commented Feb 28, 2012 at 16:21
  • See updated answer. It might work. :) Commented Feb 28, 2012 at 16:37

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.