Wordpress child theme - modifying file of includes folder - php

I know there are many others who are trying to deal with that problem, but I didn't find an answer.
I'd like to modify a file which is located in the includes folder of a theme.
I need to change the following line:
$html .= '<span class="profile-widget-info-item"><i class="fa fa-star"></i> '.__('Channel','mars').'</span>';
of this file: http://pastebin.com/Qm5r9NeW
How can I do this in my child theme?
Thank you all in advance,
Jake

Copy the file you have pasted to your child theme.
At the very top you will see an if statement that is checking if a function already exists. By placing this file in your child theme, the file will run first and prevent the parent theme from adding the function. You will need to change the name of the widget class so you don't cause a conflict. I've changed the class name from Mars_LoginForm_Widget_Class to Jake_LoginForm_Widget_Class.
if( !defined('ABSPATH') ) exit;
if( !function_exists('Mars_LoginForm_Widget') ){
function Mars_LoginForm_Widget() {
register_widget('Jake_LoginForm_Widget_Class');
}
add_action('widgets_init', 'Mars_LoginForm_Widget');
}
Now you also need to update the class name in the class definition to match the name you've given the class above.
class Jake_LoginForm_Widget_Class extends WP_Widget{
After that just make sure you are including this file in your child theme and then you can freely edit the widget class.

Related

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 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/

override in child theme a function that is included in functions.php

I am trying to override a function included in parent's theme functions.php.
The function I am trying to override lives in functions/widget-fblikebox.php
What I have in my parent's functions.php:
include("functions/widget-fblikebox.php");
functions/widget-fblikebox.php begins with this:
add_action('widgets_init', 'facebook_like_load_widgets');
function facebook_like_load_widgets()
{
register_widget('Facebook_Like_Widget');
}
class Facebook_Like_Widget extends WP_Widget {
I tried following this answer and this is what I did:
I created functions.php in my child theme and I entered there this:
<?php
// Add Facebook Like box Widget
include("functions/widget-fblikebox.php");
?>
I created a file in my child theme folder functions/widget-fblikebox.php that beginnis with this:
add_action( 'init', 'remove_facebook_like_load_widgets' );
function remove_facebook_like_load_widgets() {
remove_action('widgets_init', 'facebook_like_load_widgets' );
add_action( 'init', 'custom_facebook_like_load_widgets' );
}
function custom_facebook_like_load_widgets()
{
register_widget('Facebook_Like_Widget');
}
class Facebook_Like_Widget extends WP_Widget {
but I get an error
Fatal error: Cannot redeclare class Facebook_Like_Widget in /home/electronhe/over9000/www/wp-content/themes/truepixel/functions/widget-fblikebox.php on line 125
I googled this extensively and also tried everything that came to my mind to resolve this. I tried renaming the class in my child theme, using include_once in my child theme functions.php, etc. but to no avail. I you could please shed some light on this that would be greatly, greatly appreciated. The reason I am messing with this in the first place is that I would like to change the localization of the Facebook like box that comes with my theme (from mythemeshop, TruePixel) but it seems that there are no settings for the locale in the DB and the only idea of changing this that came to my mind was to redefine the class and hardcode proper locale in the URL of the iframe talking to FB.
Technically you can't override function declarations. Once it is declared, that's it.
To get around this, you can ask the parent theme author to support function overriding (they would wrap their function declaration in an if statement that checks to see if the function has already been declared) or you can just modify their file manually (but be careful of updating the parent theme.

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

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

Categories