WordPress child theme: override functions

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.

Read More

Thank you in advance for your answers.

Related posts

Leave a Reply

3 comments

  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') );
    
  2. 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

  3. 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';
      }
    }