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.
Related
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.
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 have a function on parent theme called function ce_seller_bar( $seller )
I want to overwrite it in child theme without deleting it from parent
When I delete it in parent theme it works, when I don't delete it the browser output is:
Fatal error: Cannot redeclare ce_seller_bar()
(previously declared in
C:\wamp\www\CampusAdsList2\wp-content\themes\classifiedengine-child
\includes\template.php:12)
That means that child function had been loaded before parent one
so I want to write the function in child theme so after update I dont lose it
ty
thanks for replay
I took a nap when I woke up,I figured out an easy solution
my solution is to overwirte file /template/ad_detail.php that calls this function and this file doesnt contain functions like template.php so I copy and past it in child theme directory
as I created my new function with defferent name in fuctions.php in child theme as well as I call it new file in /template/ad_detail.php inside child theme and it works fine for me
How to override function inside plugin and put in theme functions.php file? I've try using remove_filter and add_filter, apply_filter, add_action etc, but still not worked.
https://github.com/mikejolley/WP-Job-Manager/blob/master/wp-job-manager-template.php#L72
I want to override function on line 72. How can I done this? I don't want to edit plugin files because I just want this function replaced only when this theme activated. If not activated, function will be as the original function.
This function used by plugin. And no any filters or actions inside. So, my sorry, you can't solve this issue.
I'm trying to alter the comment links in my Drupal output, and I think I have found the function I want to influence, which is function comment_node_view($node, $view_mode).
It is in the Comment module. The problem is I can't seem to effect it, when I try to override it by putting it in my Template.php file and add my theme_ to the function name? In my template.php it looks like this now:
function themename_comment_node_view($node, $view_mode)
if I take off the themename_ it causes an error saying I can't redeclare it. I can copy the comment module and edit it directly but I thought this was how I theme something?
Drupal themes can only implement theme functions (which include template preprocess and process functions) or alter hooks.
comment_node_view() is a hook, but it's not an alter hook (differently the hook name would end with "_alter").
Why cannot themes implement hook_node_view()?
Because hook_node_view() is invoked in comment_build_comment() using the following code:
// Allow modules to make their own additions to the comment.
module_invoke_all('comment_view', $comment, $view_mode, $langcode);
module_invoke_all('entity_view', $comment, 'comment', $view_mode, $langcode);
As it is also highlighted from the comment, module_invoke_all() invokes the hooks implemented in modules, not themes.
If you want to change how a comment is rendered, from a theme, you should create the comment.tpl.php template file for your theme.
The name spacing is 'hook_node_view' so you need to replace 'comment' (the name spacing used by the comment module) with your theme name:
function mytheme_node_view($node, $view_node)
Hooks:
http://api.drupal.org/api/drupal/includes--module.inc/group/hooks/7
hook_node_view:
http://api.drupal.org/api/drupal/modules--node--node.api.php/function/hook_node_view/7
Hope that helps :)