WordPress.org

Ready to get started?Download WordPress

Forums

Overriding parent theme sub-files with a child theme. HOW? (10 posts)

  1. dorianj
    Member
    Posted 10 months ago #

    This link -- http://codex.wordpress.org/Child_Themes -- says, "your child theme can overwrite any file in the parent theme..."

    This is simple if the parent theme's file is at the root-level of the theme (ie: single.php, search.php, etc).

    How do you handle files that are in the parent's sub-file structure?

    Example: twentytwelve/page-templates/front-page.php.
    How would you include front-page.php in the child theme?

  2. WPMU DEV
    Member
    Posted 10 months ago #

    Hi @dorianj,

    I hope you are well today and thank you for your question.

    Basically, to overwrite parent them template file, simply make sure it has the same name in child theme so if you want to overwrite this file twentytwelve/page-templates/front-page.php then just create a file front-page.php in root directory of your child theme.

    Best Regards,

  3. dorianj
    Member
    Posted 10 months ago #

    I have tried this and it doesn't work. I have also tried the following format and it does not work either...

    twentytwelve-child/page-templates/front-page.php

  4. alchymyth
    Forum Moderator
    Posted 10 months ago #

    I have also tried the following format and it does not work either...

    twentytwelve-child/page-templates/front-page.php

    should have worked, and is working in my local test site.

    http://nacin.com/2012/03/29/page-templates-in-subdirectories-new-in-wordpress-3-4/

  5. dorianj
    Member
    Posted 10 months ago #

    Perhaps this is the reason it's not working for me...

    "And yes, we’re only looking one level down."

    My parent theme includes a file at...
    parent-theme/includes/javascript/counter.js

    I modified this file and placed it in my child theme here.
    child-theme/includes/javascript/counter.js

    If I understand the article correctly, this counter.js file is two levels down which means it's not being loaded. Is that a correct assumption?

  6. alchymyth
    Forum Moderator
    Posted 10 months ago #

    the automatic 'sub directory' inclusion also only applies to the templates recognized by WordPress ( http://codex.wordpress.org/Template_Hierarchy );

    with your javascript file, you will likely need to find out how the file was enqueued or otherwise called by the parent theme; then dequeue or remove that call, and call the one from the child theme...

    please ask in your theme's forum or contact the theme's developer.

  7. WPMU DEV
    Member
    Posted 10 months ago #

    Hi @dorianj,

    Thanks for reply.

    The parent them template file should overwrite in a child theme, if you declare it with same template file name in root directory of your child theme or in a sub directory but if you declare it in a sub directory then it should match the parent theme directory structure for that template.

    Check the answer posted on this thread http://wordpress.stackexchange.com/a/26824/14347 which help you to understand How to override javascript files in child theme in your case the counter.js file.

    Best Regards,

  8. jonfold
    Member
    Posted 9 months ago #

    I was having the same problem with some php functions. I guess my solution would work for javascript functions too, but I haven't tried it. However, here is the layout:

    In my case, it was a matter of modifying some functions that the parent theme (http://www.graphene-theme.com/) used by calling files from an "includes/" dir. For example modifying a function that adds a "Continue reading" link button to search results.

    Thus, if the included files only contain functions, an easy fix is then just to overwrite the functions in the parent theme by requiring a file with the functions you want to override in the child theme's function.php and making sure that all the if(!function_exists('some_random_function')) nestings are removed (so that you force an override).

    Example:

    Let's say your parent theme has a "theme_name/includes/function_script_to_override.php" file that you want to override.

    1. Create a copy of the file you want to override (theme_name-child/includes/function_script_to_override.php) in your child theme.
    2. Then make sure that all the functions called in your child theme's modified function_script_to_override.php are not nested in if(!function_exists('some_random_function')) cases.

      For example, the function:

      if (!function_exists('graphene_continue_reading_link')) {
          function graphene_continue_reading_link() {
              Do something here...
          }
      }

      becomes just

      function graphene_continue_reading_link() {
          Do something here...
      }
    3. Then insert this line in your child theme's function.php:

      require_once( get_stylesheet_directory() . '/includes/function_script_to_override.php' );

    Because the functions.php in your child theme is loaded right before the parent’s file, your modified function_script_to_override.php will be included after the original (which still should have the if(!function_exists cases) thus overwriting these functions.

    I hope it helps someone.

  9. Vishu7
    Member
    Posted 3 months ago #

    Hi @jonfold

    In my case i've done as instructed by you but having fatal error of redeclaration :(

    "Fatal error: Cannot redeclare class PageShowcaseWidget in D:\wamp\www\myfirstwpproject\wp-content\themes\itek\inc\itek-page-showcase.php on line 283"

    the file contains a class and it's functions.

  10. Andrew
    Forum Moderator
    Posted 3 months ago #

    @Vishu7, can you create a new thread here http://wordpress.org/support/theme/itek#postform

Reply

You must log in to post.

About this Topic