How can I overwrite files in the parent theme with the replacement versions in my child theme? We are using Divi as the parent theme.
We were told, in our functions.php you would create a function with all our modifications, then tell wordpress to hook our function in when et_builder_structure_elements_load runs. Im extremely new to wordpress and php so any help would mean a lot.
You need to first add the child theme and activate it. If you haven't read it already, here is a link to the wordpress documentation to do this: https://developer.wordpress.org/themes/advanced-topics/child-themes/#how-to-create-a-child-theme
Note that you cannot remove any function from the parent theme. The parent functions and child functions will run together.
Then you can add an action hook to your functions.php of the child theme like so:
<?php
add_action('et_builder_structure_elements_load',function(){
echo "Doing my function";
})
This will run when wordpress calls do_action('et_builder_structure_elements_load')
Related
I'm creating a theme and was wondering if the following is possible. I want to define some constants in parent theme to be usable in a child theme. But, since Wordpress loads child themes functions.php file before parents functions.php, my constants always return the literal string. For example:
In parents theme:
define('THEME_VERSION', 1.0);
In child theme:
var_dump(THEME_VERSION);
returns the string 'THEME_VERSION' (with a warning, that a constant is not defined) and not the number.
Is there anything I can do here without defining constants in my child theme?
WordPress loads functions.php file of child-theme just before the functions.php file of parent-theme. So that you are getting the warning while using constants in functions.php file of child-theme from parent-theme.
Here is the explanation of child theme
The constants from child themes will be used instead of parents one. Though, there will be a warning, but that shouldn't create any problem if you disable warning.
You should add constants like this if you don't want any warning.
if ( !defined('CONSTANT') )
define('CONSTANT', 'constant_value');
For more information you can see here.
https://wordpress.stackexchange.com/questions/157505/override-constants-in-child-theme?utm_medium=organic&utm_source=google_rich_qa&utm_campaign=google_rich_qa
My parent theme has some bugs in a function I then want to override in my child theme.
The thing is it is neither pluggable and hookable. It is just defined like so :
// no if(!function_exists())
function parentThemeFunction() {
//some bad coding
}
// no add_action
And that is all. It is not defined in functions.php either, but in "parentTheme/directory/file.php"
So i heard about "runkit_function_redefine" and "runkit_function_rename", but it means i have to implement "runkit" library on my server.
So far, my only option is to edit the original function in the parent theme. With the risk to see my code overrided at the very next theme update.
Any idea?
I'm trying to add customization to a Wordpress theme. Instead of replacing the original code, I'm using a "child theme" technique where I can replace files individually.
Now, that original code invokes the get_footer(), and I like to intercept that call so that I can insert my own html output before the actual get_footer function does its work.
I've read about the PHP 5's __call method but that only applies to classes whereas the WP code is not using classes but global functions. So I cannot use that technique, right?
get_footer() function just find and load proper template for the footer. There is a hook in WP get_footer that rans just before template being loaded.
Codex says that is not good to echo-es from this function, but you can use it to load your own template just before template that get_footer() loads.
You can do something like this in your functions.php:
add_action( 'get_footer', 'my_hook_fn' );
function my_hook_fn() {
load_template('my_template_file.php');
}
Codex
I use that thick in several sites, they do not use child themes, but I think this will do the trick.
I'm rather new to designing and coding Wordpress themes and have recently started a child theme based on the Sydney theme that's offered free in Wordpress. I have hit a snag I can't work out.
There's a PHP file being called through the parent's functions.php that I need to customize and need to be untouched when I update the parent theme. This file is called template-tags.php and is located inside an "inc" folder. Now, supposedly, according to the Wordpress codex (which I have consulted before asking here), any duplicate files on the child theme (except the functions.php) will automatically override those in the parent theme. I don't know if this works as well for files inside folders but I think the fact that this particular file is being called in the parent's functions.php makes it so that it is the parent's file that is being loaded. I am assuming this because the same goes for the style CSS and JS script files that I had to enqueue in the child theme in order to get my own customized versions in.
However, there is nothing in the Wordpress codex that explains how to override a file that is being already called in the parent's functions.php. And I can't find anything anywhere else (not even in these questions: WordPress child theme override a parent theme include, Overiding parent theme functions with a Child theme in WordPress, How to override a not plugable parent theme function from a non function.php file?, Wordpress child theme - functions.php copy - cannot "redeclare") that helps me specifically.
So, to sum it all up:
The parent theme has an "inc" folder with the file template-tags.php in it.
My child theme also has an "inc" folder with the file template-tags.php in it.
The parent theme's functions.php has the following code that calls its file:
require get_template_directory() . '/inc/template-tags.php';
Anything I try to add in my child's functions.php file that calls its template-tags.php file will result in an error because obviously WP can't require the same thing twice.
Short of commenting out the call in the parent's functions.php, which does't seem to me a practical solution but more of a work-around, I have no idea what else to do.
I would appreciate any input at this time. Thanks in advance!
Hello #QuestionerNo27,
It's been a long time since you asked this but I've also been looking for an answer for 2 days as I was facing the same issue. There is the answer, simple like "Hi":
You dont need the inc folder in your child theme nor the template-tags.php file.
You just have to copy the function(s) you want to override from your parent template-tags.php and paste it into your child theme functions.php
In my case, I wanted to override function mytheme_posted_on() from my parent template-tags.php
And it's now working. Thanks to https://wordpress.org/support/topic/inc-folder-in-child-theme (Stephencottontail answer)
I had a similar problem where I wanted to make changes to the layout of the header in my child theme within the template-tags.php.
The solution that worked for me was to use the following line in my functions.php file in my child theme:
require get_stylesheet_directory() . '/inc/template-tags.php';
I was using the original line from the the Parent them in functions.php:
require get_template_directory() . '/inc/template-tags.php';
Using the function get_template_directory() was not working because this function will always return the parent theme directory.
Using the function get_stylesheet_directory() worked because it retrieves the Child Theme directory.
Yes I know the name of the function "get_stylesheet_directory()" is not very intuitive but it it actually will return the Child Theme directory location.
It took a while to work this out by searching online. I found this reference online that helped me in my search: How to override a parent file in a child Wordpress theme when parent file is being required in functions.php
you should use
get_stylesheet_directory() that refers to active theme directory
instead of get_template_directory() that refers to parent theme directory
https://www.csestack.org/overwrite-wordpress-php-file-subfolder-child-theme/
I'm using Starkers with a child theme (starkers-child).
I need to edit the functions.php to declare a menu, so I copied down the functions.php, JS folder and external folder (which were all declared somewhere in the functions file)
However I still have one error:
Cannot redeclare starkers_script_enqueuer()
(previously declared in C:\wamp\www\redlaw\wp-content\themes\starkers-child\functions.php:65) in
C:\wamp\www\redlaw\wp-content\themes\starkers-master\functions.php
I believe it is caused by this line:
function starkers_script_enqueuer() {
I can see why as it already calls the function in the master, so it cannot call it again in the child.
But if I remove this line from the master then doesn't that defeat the purpose of keeping a clean master and having a child theme?
Full functions.php is here (unedited, as it appears in the master theme)
http://jsfiddle.net/8KGcK/
Forgive me, I don't have enough rep to just comment.
A child theme's functions.php is loaded in addition to the parent theme. So you don't need to actually copy and paste the code. What everyone else says about wrapping it in an if(function_exists(function)) is optional, but a best practice because it's less error prone.
Anyways!
This is from the Codex:
Unlike style.css, the functions.php of a child theme does not override its counterpart from the parent. Instead, it is loaded in addition to the parent’s functions.php. (Specifically, it is loaded right before the parent’s file.)
Read more here