4

I have a php file located at:

.../wp-content/themes/kallyas-child/pagebuilder/elements/TH_TeamBox/TH_TeamBox.php

I'm trying to use the above file to overwrite the parent's TH_TeamBox.php file which is found here:

.../wp-content/themes/kallyas/pagebuilder/elements/TH_TeamBox/TH_TeamBox.php

My child theme is active and I have created the file path in my child theme to mimic my parent theme. I was under the impression that if I did that I would be able to edit PHP files to my liking in my child theme without losing my changes when I update my parent theme.

I've been developing for a while, but as you can see I'm new(er) to Wordpress. Any insight on why my child's .php file isn't overwriting it's parent's .php file would be greatly appreciated. I'd be glad to give anymore info that you might need to assist me, thanks! : )

  • Hard to know without knowing what's in the file. My guess is that that file is or is wrapped by a php class. If that's the case, you can try to extend the class. If that doesn't work, you can play with add_action, remove_action and priorities but it much depends on how it's arranged within the parent theme. – Luis Sanz Apr 6 '16 at 22:47
  • I couldn't imagine class extension working. If class C (child) extends P (parent), then an object of Type C would have everything from P but P would have nothing from C and the theme would be using the P by default even when the Child theme is active. I will look into the action hooks. Thanks for responding to my thread, I appreciate it. – domdambrogia Apr 19 '16 at 15:41
12

Child themes are allowed to override templates, not simply arbitrary PHP files.

In WordPress, a theme consists of a bunch of PHP files which are used as Templates. You can find a list of those files in the Template Hierarchy.

Those specific template files can be overridden with new ones, but unless the parent theme has some special means for you to override other files, then files simply included by the parent as part of a support structure or as libraries for a pagebuilder piece of code cannot simply be overridden in that manner.

  • Not what I wanted to hear, but thanks for the info. I appreciate it – domdambrogia Apr 19 '16 at 15:34
1

The solution is to move the required changes to your child theme functions.php file.

If it is a function, usually there were will be an if statement that checks if it is already declared, in this case you can copy the function to functions.php with the same name and make the required changes, it will have priority over the rest of the theme files. In case the function was declared without an if statement, then you have to change its name in functions.php, find the template file the functions is called, copy it to your child theme and change the function call in it.

If it is a widget, then copy the whole class declaration to functions.php, change its name and make the other changes. Make sure you also register the new widget and you will find it available in the widgets area with the new name and changes.

  • Thanks for your input. At the time of the question I was not looking for how to overwrite a function but rather overwrite a file. Also, the correct answer has been supplied by Otto. Unless there is a custom template function you can filter (like WooCommerce's wc_get_template) only main template files (header.pho, footer.php, page.php, etc) can be overwritten. – domdambrogia Apr 1 '18 at 18:17
0

I have a workaround for you that may be better than just writing modifications in parent theme files which is of course not desirable for reasons Otto mentioned.

If you add these two lines of PHP the top of any file you want to use a modified copy of in your in your child theme directory structure:

<?php
    $filepath = str_replace(get_option('template'), get_option('stylesheet'), __FILE__);
    if (file_exists($filepath)) {include($filepath); return;}
?>

You would of course still need to do this again to each file you do this to after a theme upgrade, so recommendation would be to take note of these files, and probably no longer update the theme directly but instead download it, add these lines back in locally and upload the updated copy by FTP.

It may seem to be not worth that hassle, but what it would do is allow you to keep your custom modified files together in your child theme structure and not be overwritten by any theme updates. Again, you'd just re-add those lines to the top of any parent file you want to keep overriding.

It also means you could remove the modified child functions file copy without breaking your site (ie. to test against an updated theme file for example) - as this will fallback to the original file if a modified copy is not found.

(CEM Typo Edit: replaced the first get_options call with get_option.)

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.