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.

Is there a way to replace a function by another function in a child theme? In other words, in a theme there is a function named "theme_function" and this function is called by several files inside the theme.
I would like to find, if exist, an action that replace all occurrences of "theme_function" with another function "child_function" inside the child theme. Please note that "theme_function" that I want to replace do not have the following block:

if ( !function_exists('theme_function') ) :  

because if it that case is easy to replace the theme_function with another function having the same name in functions.php

share|improve this question
add comment

1 Answer 1

up vote 4 down vote accepted

If the parent-Theme function in question isn't either pluggable (i.e. wrapped in a if( ! function_exists( $function ) ) conditional) or filterable (i.e. returns its output wrapped in a apply_filters( $filtername, $output )), then there's no easy way to override the function.

You'll have to do one of the following:

  1. Replace all template files that contain instances of the function call, and replace those calls with your own child-Theme function, in all such templates
  2. Modify the parent Theme, to delete (or make pluggable) the function in question
share|improve this answer
    
Thank you, I was hoping that there was a method to do it, but the only way is to contaminate the parent theme or to replace a lot of files containing the function call in the child theme. –  Avionicom Nov 20 '12 at 20:52
    
If the parent Theme developer is amenable to patches, you could always modify the parent Theme to make the file pluggable, and then submit the change back to the developer, as a patch. –  Chip Bennett Nov 20 '12 at 20:54
    
Infact! Because the best way, always, is to make the functions pluggable. –  Avionicom Nov 20 '12 at 20:58
2  
Actually, I prefer making my functions filterable, in almost all cases. :) –  Chip Bennett Nov 20 '12 at 20:59
add comment

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.