I'm having issue in overwriting parent theme file via child theme. Normally I copy parent theme file to child theme by following same file structure/path and it works. But this time it is not working. I'm not sure, what is the problem...
The parent file is located at:
wp-content/themes/THEMENAME/assets/js/FILENAME.js
and I put at this path in child theme:
wp-content/themes/CHILDTHEME/assets/js/FILENAME.js
Same problem on this file:
Parent Path:
wp-content/themes/THEMENAME/includes/FILE.php
Child Path:
wp-content/themes/CHILDTHEME/includes/FILE.php
I'm pretty sure, I'm missing something technical but I'm unable to figure out. Looking for help!
Child theme doesn't override css/js files, to achieve that, You will need to dequeue these scripts in the child functions.php file, You need to get the script handle name that parent theme uses to enqueue it.
add_action( 'wp_enqueue_scripts', function(){
wp_dequeue_script( 'script-handle-name' );
}, PHP_INT_MAX );
Child theme only overrides the template files Template Hirarchy
To override a php file, There are two conditions:
1) If the file function you want to override is hooked to action / filter then create a function in the child theme functions.php file with a different name and hook it with a higher priority.
2) If you want to override the function itself, you can recreate the function in the child theme functions.php with the same name only if The function is created in the parent theme with the condition.
if ( ! function_exists ( 'my_function' ) ) {
function my_function() {
}
}
otherwise, It will raise a fatal error.
Related
There's a bunch of css in my parent theme I don't need and instead of just overwriting it all, I'd like to dequeue it completely and put most of the CSS from the parent theme into the child themes css file.
Is this possible?
First you'll need to identify the names/handles that the parent theme's stylesheets were originally enqueued under. You can do this quickly by running a search on your web server in the parent theme directory, e.g. grep wp_enqueue_style /var/www/mysite/wp-content/themes/parent_theme/
Then add a dequeue function to the child theme's functions.php file, and initialize it with a priority higher than the priority level set for the parent theme's enqueue function:
function remove_parent_styles() {
wp_dequeue_style( 'name_of_parent_stylesheet' );
wp_dequeue_style( 'name_of_parent_stylesheet_2' );
}
add_action( 'init', 'remove_parent_styles', 99 );
You should identify your styles/scripts handle name before dequeue it. A easiest way is install Query Monitor plugin and see in Styles tab. Handle name is in second column.
With this plugin, you also see CSS files are required by any dependants.
Dequeue your CSS:
Append this code to the end of your theme's functions.php file:
function tdt_dequeue_styles(){
wp_dequeue_style('your-handle-name');
wp_deregister_style('your-handle-name');
// Another style dequeue
wp_dequeue_style('your-2nd-handle-name');
wp_deregister_style('your-2nd-handle-name');
}
add_action( 'wp_print_styles', 'tdt_dequeue_styles', 9999 );
I'm creating a WordPress child theme based on an existing parent theme, and I'd like to have any same-named file I put in my child theme directory take priority over the file in the parent theme directory. I thought this was how parent/child theming was set up in WP but I have hit a bump.
According to the WordPress codex on Child Themes, it says:
Template Files
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.
In one of my files (header.php), there is an include that looks like this:
include get_parent_theme_file_path("folder/file.php")
Even though I have a duplicate-named-but-modified version of that file.php in my child theme, it still uses the version in my parent theme. According to the same codex, their recommendation for targeting a child theme file specifically is to use get_stylesheet_directory(), so it would look like this:
include (get_stylesheet_directory()."/folder/file.php");
I understand that the purpose of a function called "get_parent_theme_file_path()" is to ignore the parent/child relationship and just get the parent theme version, so without replacing that with a function that explicitly gets a file in my child theme (ie. get_stylesheet_directory), is there a way I can have some sort of universal get_path() function that checks for child first, if it doesn't exist, get parent version?
By the way, I read this Q&A on "get_parent_theme_file_path vs. get_template_directory", but their solution was to use parent_theme_file filters, but that isn't dynamic, and would require me to write a filter function for every child file I want it to use.
Thanks for your help.
Have you tried something like this:
add_filter( 'parent_theme_file_path', function( $path, $file ) {
return get_stylesheet_directory() . '/' . $file;
}, 10, 2 );
This should override the parent theme path without the need to write a function for every file.
If you want to make sure get_parent_theme_file_path() still works for parent theme files that are not overriden in your child theme, you could do a simple check:
add_filter( 'parent_theme_file_path', function( $path, $file ) {
if ( !file_exists( get_stylesheet_directory() . '/' . $file ) ) {
return $path;
}
return get_stylesheet_directory() . '/' . $file;
}, 10, 2 );
Someone on wordpress.stackexchange pointed out the use of locate_template, which retrieves the name of the highest priority template file that exists.
Searches in the STYLESHEETPATH before TEMPLATEPATH and wp-includes/theme-compat so that themes which inherit from a parent theme can just overload one file.
I am currently working on a WordPress website, which has a Parent and Child Theme.
The Parent Theme is outputting the following statement, at the top of the WordPress Dashboard:
This theme requires the following plugins: Plugin A, Plugin B, Plugin C etc
Triggering this output, is the following code, within the Parent Theme's functions.php file:
require_once get_template_directory() . '/inc/plugins/class-tgm-plugin-activation.php';
Is there a way I could disable this, via the Child Theme's functions.php? Sure, I could comment it out within the Parent Theme, but this could get overridden during a future update. I did try to copy the entry, into the Child Theme's functions.php, then comment it out there, but that did not work.
Latest Effort
I went into the WordPress Dashboard, where I copied the notification message prefix:
This theme requires the following plugins:
I then went into the file located within get_template_directory() . '/inc/plugins/class-tgm-plugin-activation.php', where I found the above notification prefix, located within the do_action( 'tgmpa_register' ); function.
I then located the add_action for the do_action( 'tgmpa_register' ); within the /inc/functions/tgm-functions.php directory.
I then headed into the Child Theme, and duplicated the above folder hierarchy.
I then commented out both add_action and do_action, within their respective files as well as entering remove_action( 'tgmpa_register', 'register_required_plugins', 999 ); into the Child's functions.php file.
Unfortunately, neither of the above work. I take it, that this is because the Parent Directory parent_theme/inc has priority over the Child Directory child_theme/inc? If so, is there a way to change this?
How to use functions.php to add all the .css to child themes at one time?
If it is not possible at once, how is right way to do it one by one?
Theme: Generatepress
https://sk.wordpress.org/themes/generatepress/
Child: Hand made by Codex
https://codex.wordpress.org/Child_Themes
Adding one CSS file isn't recommended, it's better if you register each new custom CSS individually.
Keep in mind, a child theme will automatically inherit all of the parent's CSS. You should only register CSS files you custom wrote.
This is an example function of loading a CSS file located in the CSS directory within the child theme. It loads a custom file 'CustomFooter.css' on a page with an ID of 45.
add_action( 'template_redirect', 'footer_css' );
function footer_css() {
if (is_page(45)) {
wp_enqueue_style('CustomFooter-style',
get_stylesheet_directory_uri() .
'/CSS/CustomFooter.css?v=1.1', array( 'parent-style'));
}
}
I am using style.css to "add" styles to theme- so it won't get overwrite when i update the theme.
How can i accomplish the same effect with functions?
Suppose i have a function (in the parent theme),
function interio_gmap_shortcode($atts) {
....
....
$str .= '
<script type="text/javascript" src="http://maps.google.com/maps/api/js?sensor=false**&language=iw**"></script>
This is what i want to add- &language=iw , it's not exist in the parent theme.
this function exist in shortcodes.php
How can I accomplish that?
If you're properly creating a child theme, you should have your own folder with a styles.css and a functions.php in it.
To override a function like you're describing, you'll need to copy the php function from its original location (either your parent theme's functions.php or shortcodes.php) then make the changes you need in your child theme's functions.php.
It depends on whether the parent theme was coded to allow the function to be overridden. If so, it will probably look something like:
if (!function_exists('interio_gmap_shortcode')) {
function interio_gmap_shortcode($atts) {
...
In this case, you can copy the function to your own functions.php file and change it. At the time that the parent functions.php runs, your function will already exist and will be the one used.
If the parent function.php is not coded something like this, then copying the function to your function.php will result in a "Duplicate Function" error.
In that case, you would need to create your own function with a different name and use that instead.