1

In my parent Theme I've a function without the initial statement:

if (!function_exists(... etc...

How can I replace it with a function with the same name in my child theme? If I create the function into my functions.php it gives me an error due to the fact that there are two functions with the same name.

Thank you in advance for your answers.

Avionicom
  • 191
  • 2
  • 5
  • 19

3 Answers3

1

This seems to be working for me:

function fileExistsInChildTheme($file_path){
    $file_directory = get_template_directory();
    if(file_exists($file_directory . "-child" . $file_path)){
        return $file_directory .= "-child" . $file_path;
    }
    return $file_directory .= $file_path;
}

require ( fileExistsInChildTheme('/includes/functions.php') );
require ( fileExistsInChildTheme('/includes/theme-options.php') );
require ( fileExistsInChildTheme('/includes/hooks.php') );
require ( fileExistsInChildTheme('/includes/version.php') );
John Magnolia
  • 16,769
  • 36
  • 159
  • 270
0

Child theme function.php file is loaded before the parent theme functions file, therefore you shouldn't get fatal error for re-declaring the function in the child theme. That's why your parent theme is using the function_exists check.

Maybe you're declaring the function in the child theme after a hook(e.g. init)?

Here is the codex documentation about this: http://codex.wordpress.org/Child_Themes#Referencing_.2F_Including_Files_in_Your_Child_Theme

Emil M
  • 1,082
  • 12
  • 18
  • The problem is that for this function, the parent theme is not using function_exists check – Avionicom May 26 '12 at 16:55
  • Oh, I misread the "without" word -- sorry about the bad comment. The only other idea I have is to perform `remove_action` or `remove_filter` for that function and then hook another function on it's place. Not sure if this is applicable for your case though. – Emil M May 26 '12 at 17:15
  • I know, thank you the same. I think I've to go for another way by creating a new function. – Avionicom May 26 '12 at 17:36
0

i think if you add same name of function then it take from child theme function then it take from parent.

For ex.

Child Theme

if ( ! function_exists( 'function_name' ) ) {
  function function_name(){
    echo 'This is child theme';
  }
}

Parent Theme

if ( ! function_exists( 'function_name' ) ) {
  function function_name(){
    echo 'This is parent theme';
  }
}
Mukesh Panchal
  • 1,956
  • 2
  • 18
  • 31