Actually i’m building a Website using WordPress, everything work, but now I need to add some change inside of the functions.php, the original one in the parent folder, so how should I do ? Is it possible to copy everything inside of the functions.php child theme and then add changes ?
I don’t want to add changes in the parent folder because of future theme updates. But tell me if this is the only one option.
EDIT: “Do not copy the full content of functions.php of the parent theme into functions.php in the child theme.” is an advice from WordPress Codex https://codex.wordpress.org/Child_Themes
EDIT 2: [SOLVED] Watch answer below.
Advertisement
Answer
A child theme is a theme that inherits the functionality and styling of another theme, called the parent theme. Child themes are the recommended way of modifying an existing theme.
How to Create a Child Theme
- Create child theme directory (common approach to name child theme as the parent theme plus -child added on the end.)
- Create style.css
- Create functions.php
The stylesheet must begin with the following (the stylesheet header):
/* Theme Name: XYZ Child Theme Theme URI: http://example.com/themes/spacious/ Description: XYZ Child theme Author: ABC Author URI: http://example.com Template: XYZ Version: 1.0 */ /* =Theme customization starts here ------------------------------------------------------- */
A couple things to note:
- You will need to replace the example text with the details relevant to your theme.
- The Template line corresponds to the directory name of the parent theme, so adjust accordingly.
The final step is to enqueue the parent and child theme stylesheets inside functions.php like this:
add_action( 'wp_enqueue_scripts', 'enqueue_parent_styles' ); function enqueue_parent_styles() { wp_enqueue_style( 'parent-style', get_template_directory_uri().'/style.css' ); }
Lastly, to activate the child theme, in the dashboard go to Appearance->Themes. Look for the child theme you created and activate it. That’s it. Also, make sure the parent theme is also present in the installed themes for the child theme to work. If you now visit your site, it should look all same just like when the parent theme was activated.
NOW, you can make changes in your child theme without any problem!
If you want to modify a parent theme function then you can simply enclose it in a conditional tag to check if a function that name has already been run:
<?php if ( ! function_exists ( 'my_function' ) ) { function my_function() { // Contents of your function here. } } ?>
So if you enclose the functions in your parent theme in a conditional tag like this, WordPress will check if there is a function with the same name in your child theme that’s already been run, and if that’s the case it won’t run the function from the parent theme.
Then when you come to write a function in your child theme which you want to override the one in the parent theme, you just give it the same name as the one in the parent theme:
<?php function my_function() { // Contents for your function override here. } ?>
WordPress will run the function in the child theme first, and when it comes to the one in the parent theme, it’ll check if it already exists and because it does, it won’t run it.
And if you do not wanted to touch parent functions.php then you can:
Assign child theme function higher priority:
add_action( 'init', 'child_function', 15 );
Remove parent theme functions from hooks:
remove_action( 'init', 'parent_function' );