Advanced Ways to Modify Your Theme's Code Advanced Advanced tutorials require advanced knowledge of web design concepts and languages such as HTML, CSS, JavaScript.
The important options (used by most of the users) that will let you customize your theme are built into the admin, like changing the color palette, logo, header type, footer, etc. Other important page changes can be made directly from the visual composer of that page by adding the built in shortcodes.
However there might be more advanced changes that you'll want to make that require code editing. These changes should happen in one of the following ways:
Don't change anything in the parent theme folder, ever. All the modifications are done in the child theme folder, this way all the future updates made by us will not impact your changes. All the modifications done in the parent folder will be lost on the first update of the theme because the files will be over written.
- Use hooks - provided by WordPress to allow your code to 'hook into' the rest of WordPress; that is, to call functions at specific times, and thereby set your code in motion. There are two kinds of hooks: Filters and Actions. More about each and how to use them can be found in this article.
- Rewrite functions in functions.php file from the child theme - 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.)
Add your custom function to functions.php file from child theme. The function will do the exact same job from there too, with the advantage that it will not be affected by future updates of the parent theme. Do not copy the full content of functions.php of the parent theme into functions.php in the child theme.
Functions that are conditionally declared can be replaced in child theme by simply declaring them in functions.php from the child theme.
function declared in parent theme
if ( ! function_exists( ‘fw_theme_featured_posts' ) ) :
function fw_theme_featured_posts() {
// Do something.
}
endif;
can be overridden in functions.php in child theme
function fw_theme_featured_posts() {
// Do something.
}
- Copying a file from parent to child - your child theme can override any file in the parent theme: simply include a file of the same name in the child theme directory (keeping the folder structure), 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. Use this method if you want to modify the entire file or make big changes to it, otherwise we advise you to use one of the two ways above.