Wordpress: editing child theme - php

I want to edit some stuff on my website. I'm using a child theme and I plan to change some CSS and do some structural changes. I know the CSS is easy to change, as I only need to call the names of the classes or IDs and give the new values.
But, if I want to change things around in html, do I need to copy the whole code? Will I lose every change when the theme updates (as in: i copied and pasted the older version and now I need to copy and paste the new version)?
I don't know much about the names of things and how to describe them properly.

Make copies of the theme files in the parent theme you want to modify - such as index.php, category.php, etc - and move those to the child theme and edit them. The copies in the child theme will be used by WordPress rather than the same-named files in the parent. This includes file in folders, i.e. /css/style.css, so duplicate the file structure in the child theme, if needed.
The child theme will continue to use those files, even if/when the parent theme get updated. And that also means your child theme edits won't disappear, unless there are major structural/functional changes to the parent theme.
One exception to child theme file usage is functions.php:
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.)
See http://codex.wordpress.org/Child_Themes

Taken from Wordpress Doc - Child Themes:
If you want to change more than just the stylesheet, your child theme
can override any file in the parent theme: simply include a file of
the same name in the child theme directory, and it will override the
equivalent file in the parent theme directory when your site loads.
For instance, if you want to change the PHP code for the site header,
you can include a header.php in your child theme's directory, and that
file will be used instead of the parent theme's header.php.

Wordpress Child Theme can't have a child theme itself. It's not supported by Wordpress. I had the same problem before and after some searches I found this article.
It explains about creating a grandchild theme; from the article:
The solution is surprisingly simple. Instead of editing the child
theme, create a grandchild theme. It’s very similar to creating a
child theme, except you do it via a plugin. You add your custom
functions to the plugin, just as you normally would in functions.php
(though remember your plugin will be called much earlier than
functions.php, so you’ll need to make sure that any code in your
plugin only runs when an action is fired). Use the wp_register_style
and wp_enqueue_style functions to add your CSS (by default a
grandchild theme will add your CSS to the CSS of the parent or child
theme, but you can change this behaviour by using the wp_dequeue_style
function). Finally filter the result of the get_query_template
function to control which PHP files are executed when a page is
requested.
It's not really a child theme of a child theme, but a plugin including calls to add_filters or something similar to override the behaviour of the child theme. In this way upgrading child / parent themes will not affect the customisation you've made.

Related

wordpress child theme does not automatically load single-post_type.php from parent

For the first time I am trying to setup a wordpress child theme. The parent has a complicated templates for custom post types and header.php and footer besides stylesheets and javascript files etc.
The child theme only needs to change the css (for now). My child theme only has style.css and functions.php.
I already have a single-product.php for a custom post type product in the parent theme folder. That is not getting loaded automatically from the parent. Please suggest what is required to get this working.
Thanks.
Not really the best solution but can help.
Copy header.php into the child theme and put your css in the <head> section. Remember to enclose your css with <style>custom-css{...}</style>

Child theme doesn't read template part file

I'm building my first child theme. My child theme is my active theme at the moment. So, I have a file under the parent theme which is at wp-content/themes/mytheme/template-parts/ajax-mytick-list.php and I copied it to wp-content/themes/mytheme-child/template-parts/ajax-mytick-list.php and edit it there. But the site is not showing the one that I edited under child theme. Cos if I edit the same file in parent theme, the result is reflected.
What am I doing wrong?
You can override page templates this way, but not other files. In this case, you'll need to override the according function (guide) or hook into it with an action hook or filter.
More details according to your question below:
You'll need to find out where the template-parts file is getting included. Search the parent theme for something like
require get_template_directory() . '/template-parts/ajax-mytick-list.php';
If that call is in a template file, copy it to your child theme and change the call to
require get_stylesheet_directory() . '/template-parts/ajax-mytick-list.php';
If it's not in a page template, you'll need to dig deeper, but as every theme is different, it's impossible to say how exactly you'll get there.

Will the header.php updated in child-theme when the theme is updated?

I have a theme installed and I want to add a header at the top of the existing code. So not to lose it when the theme is updated I'm thinking of creating a child theme and copy the ´header.php` and then edit it in the child theme folder.
So when the theme is updated with changes in the header.php from the main theme developer, will the file that I copied and changed in the child-theme folder have the updates with the code I added? Or it will remain the same whenever the theme is updated?
I mean to have the updates from the developer and my custom header remains there , So for example if he changed the order of the header elements it will be implemented to the child-theme and my custom header will remain?
Is there is a better way for adding that header without creating a child-theme?
Using a child theme is exactly what you need to do. Think of the files in the child theme as an override to what is in the parent theme. If you copy header.php from the parent theme into the child theme and change it, it will load the header.php from the child as opposed to the parent. When you update the parent, the child is untouched. What you're doing is the best way to make your edits without the main theme overwriting your changes.
If the main theme's header.php is altered by a theme update, the child theme's header.php won't be touched.
So if there are useful changes in the main theme's header.php which you want to become effective for your child theme, you'll have to transfer those parts of the code to your child theme's header file by editing it accordingly.

WordPress Child Theme - General Understanding

I am trying to understand how to use a Child Theme in wordpress.
I am afraid that something will go wrong in the process of developpment because I haven't completely understood how to use the child theme or its integration wasn't done properly.
My confusion comes after I create the child theme and by adding the style.css and functions.php.
At this point, can I make any changes that I want to the child theme and it will still work?
For example,
After I modify the front-end of the website with the use of the child theme, I want to add some forms that will insert data into the database.
Do I have to implement any php files from the parent-theme in order to make this work ?
Or do I treat my child-theme as a fresh theme (blank canvas) and code the back-end however I would like ?
Thanks
Think of the child theme as a layer on top of the parent theme. By default (if you add nothing to the child theme other than a style.css file), all the templates from the parent theme will be used. The only thing you have to have is the style.css file. If you wanted to override the page.php template for instance, you would simply just make a page.php file in your child directory and WordPress will use that template instead of the parent template. Same goes for any template file or template part.
You can have a functions.php file in your child theme and it will be included in addition to the parent functions file.
Usually the parent's style.css file would be enqueued before the child's style.css file, but it depends on how those files get enqueued. The system is quite flexible.
Your child theme is not a blank canvas. It inherits style.css, functions.php and all the post/page templates from the parent theme. The style.css file of your child theme will load after that of the parent theme, thus enabling you to add more CSS rules. The functions.php file of your child theme will load before the parent's functions.php file.
The child also inherits all the templates from the parent. However, if a template exists in the parent and you create it in the child the child theme's template will completely override the parent's template. For more information on parent-child theme relationship I highly recommend that you check the official child themes documentation.

Changing classes/id's in Wordpress child theme

I am currently changing a parent theme in wordpress by creating a child theme and I am just checking if this is considered bad form and if it will have any repercussions if/when the theme is updated.
e.g. The parent theme's header.php has an id called #menu. This has a lot of styles in the parent's style.css file. To avoid having to alter everything to do with the #menu I changed the id to #menu-child in the child theme's header.php file and added my own styles, this has made it a lot quicker but I'm unsure if it will screw the styles/theme up if/when it is updated.
Thanks in advance.
Short answer: No.
Your child theme template overwrites the main theme template and still does that after an update. So your changes will stay in place. But if the parent theme uses the #menu for stuff (styling, js...) these changes and current adjustments will not have any effect in your child theme anymore. So if you want your own changes and all functionalities and styling of the parent theme you should not remove the id, but add your own class.
Of cause theme developers can sometimes mess up the structure or make changes themselves which require you to alter your child theme templates as well (like changing the whole structure).

Categories