Wordpress child theme - functions.php copy - cannot "redeclare" - php

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

Related

Adding functions to a a divi theme

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')

Use defined constants from parent theme in child theme in Wordpress

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

How to override a non pluggable and non hookable function in a Wordpress parent theme?

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?

How to override a widget in a child theme in WordPress?

I want to make a simple modification in a PHP file located here in my parent theme:
wp-content\themes\sailing\inc\widgets\gallery\tpl\base.php
So I created the same folder structure in my child theme and did the modification I need in this file. I also copied/pasted all the PHP files needed to declare this widget.
wp-content\themes\sailing\inc\widgets\widgets.php
wp-content\themes\sailing\inc\widgets\gallery\gallery.php
wp-content\themes\sailing-child\inc\widgets\widgets.php
wp-content\themes\sailing-child\inc\widgets\gallery\gallery.php
What am I missing here ?
WordPress child themes are not working this way. The only files that you can override by using the same path in your child themes are the "basic" files: index.php, page.php, style.css... Mostly the template files.
When it comes to overriding functions or classes in a child theme. You've several ways to handle it:
re-declaring the functions/classes
duplicating the functions/classes
But it depends on how your theme is built and if it's "child theme" ready. Let's have a look with your widget issue.
If you open your widget declaration file within your parent theme, you'll see something like:
class Widget_Name extends WP_Widget {
...
CODE OF THE WIDGET
...
See: https://codex.wordpress.org/Widgets_API
The ideal case is you don't see the above lines first but:
if(!class_exists('Widget_Name')) {
class Widget_Name extends WP_Widget {
...
CODE OF THE WIDGET
...
Which means, you can just copy/past your file and that'll work just fine, you widget will override the parent one and no error will be thrown as the parent widget won't be executed. That's the "child theme ready" theme. Note that it's the same with functions (if(!function_exists('function_name')).
Don't forget to call your file from your child-theme/functions.php file as it won't be called by default.
Like:
require_once('path/to/your/widget_class.php');
Other way, if you don't have a class_exists call is to just duplicate the file, call it with the require_once. You should see an error as you're defining 2 times the same class. PHP won't let that happen, fatal error.
Just rename:
class Widget_Name2 extends WP_Widget {
And somewhere (most of the time at the end) of your file, look for register_widget( and edit the class name:
register_widget( 'Widget_Name2' );
That's not the most handy way as you'll have 2 times the same widget but that does work though.
So since #2Fwebd answer is kinda incomplete (as marked in the comment), here is a more complete answer (just to make it clearer than an answer and its comment. ) I've suggested an edit for his answer, but while it isn't accepted, here is a more complete answer :
WordPress child themes are not working this way. The only files that you can override by using the same path in your child themes are the "basic" files: index.php, page.php, style.css... Mostly the template files.
When it comes to overriding functions or classes in a child theme. You've several ways to handle it:
re-declaring the functions/classes
duplicating the functions/classes
But it depends on how your theme is built and if it's "child theme" ready. Let's have a look with your widget issue.
If you open your widget declaration file within your parent theme, you'll see something like:
class Widget_Name extends WP_Widget {
...
CODE OF THE WIDGET
...
See: https://codex.wordpress.org/Widgets_API
The ideal case is you don't see the above lines first but:
if(!class_exists('Widget_Name')) {
class Widget_Name extends WP_Widget {
...
CODE OF THE WIDGET
...
Which mean, you can just copy/past your file and that'll work just fine, you widget will override the parent one and no error will be thrown as the parent widget won't be executed. That's the "child theme ready" theme. Note that it's the same with functions (if(!function_exists('function_name')).
Don't forget to call your file from your child-theme/functions.php file as it won't be called by default.
Like:
require_once('path/to/your/widget_class.php');
Other way, if you don't have a class_exists call is to just duplicate the file, call it with the require_once. You should see an error as you're defining 2 times the same class. PHP won't let that happen, fatal error.
Just rename:
class Widget_Name2 extends WP_Widget {
Then, change your id_base in your parent::_construct to a unique id (like this :
parent::__construct( 'new_uniq_id', 'name of your widget', ...)
And somewhere (most of the time at the end) of your file, look for register_widget( and edit the class name:
register_widget( 'Widget_Name2' );
That's not the most handy way as you'll have 2 times the same widget but that does work though.
Hope it helps someone.

How to override a parent file in a child Wordpress theme when parent file is being required in functions.php

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/

Categories