In my parent Theme I've a function without the initial statement:
if (!function_exists(... etc...
How can I replace it with a function with the same name in my child theme?
If I create the function into my functions.php it gives me an error due to the fact that there are two functions with the same name.
Thank you in advance for your answers.
This seems to be working for me:
function fileExistsInChildTheme($file_path){
$file_directory = get_template_directory();
if(file_exists($file_directory . "-child" . $file_path)){
return $file_directory .= "-child" . $file_path;
}
return $file_directory .= $file_path;
}
require ( fileExistsInChildTheme('/includes/functions.php') );
require ( fileExistsInChildTheme('/includes/theme-options.php') );
require ( fileExistsInChildTheme('/includes/hooks.php') );
require ( fileExistsInChildTheme('/includes/version.php') );
Child theme function.php file is loaded before the parent theme functions file, therefore you shouldn't get fatal error for re-declaring the function in the child theme. That's why your parent theme is using the function_exists check.
Maybe you're declaring the function in the child theme after a hook(e.g. init)?
Here is the codex documentation about this: http://codex.wordpress.org/Child_Themes#Referencing_.2F_Including_Files_in_Your_Child_Theme
i think if you add same name of function then it take from child theme function then it take from parent.
For ex.
Child Theme
if ( ! function_exists( 'function_name' ) ) {
function function_name(){
echo 'This is child theme';
}
}
Parent Theme
if ( ! function_exists( 'function_name' ) ) {
function function_name(){
echo 'This is parent theme';
}
}
Related
I use a paid theme and unfortunately it does not offer the possibility to use own icons from a font. Since I have to observe the GDPR and cannot use Google icons, I would like to use local icons. The only possibility is to import my own CSS file and define the unicodes of the icons.
I already use a child theme and have already successfully defined my local icon font in the parent theme, but I am not sure how to transfer my customisations to the child theme.
Parent: /themes/mytheme/includes/assets.php
I have not made any changes here. I just added the css definition of my font to the existing icons.css. But the goal would be to separate this. So here I have to add another line in the register_scripts function so that my CSS is loaded.
wp_register_style( 'local-icons', c27()->template_uri( 'assets/dist/local-icons'.$suffix.'.css' ), [], \MyTheme\get_assets_version() );
class Assets {
...
...
public function register_scripts() {
...
...
...
wp_register_style( 'mytheme-icons', c27()->template_uri( 'assets/dist/icons'.$suffix.'.css' ), [], \MyTheme\get_assets_version() );
...
...
}
}
...
public function enqueue_scripts() {
global $wp_query;
...
...
wp_enqueue_style( 'mytheme-icons' );
...
}
What is c27?
function c27() {
return mytheme()->helpers();
}
Parent: /themes/mytheme/includes/src/admin/admin.php
*Here I have added the following to the enqueue_scripts() function:
wp_enqueue_style( 'mytheme-local-icons' );
In the function get_icon_packs() in the return of wp_send_json I added the following:
'local-icons' => array_values( \MyTheme\Utils\Icons\Local_Icons::get() ),*
class Admin {
...
...
public function enqueue_scripts() {
...
...
wp_enqueue_style( 'mytheme-icons' );
...
}
...
...
public function get_icon_packs() {
if ( ! is_user_logged_in() ) {
return;
}
return wp_send_json( [
'mytheme-icons' => array_values( \MyTheme\Utils\Icons\mytheme_Icons::get() ),
'font-awesome' => array_values( \MyTheme\Utils\Icons\Font_Awesome::get() ),
] );
}
....
....
}
Parent: /themes/mytheme/includes/utils/icons/theme-icons.php
Here I created a new file and did the mapping for my font.
<?php
namespace MyTheme\Utils\Icons;
if ( ! defined('ABSPATH') ) {
exit;
}
class Theme_Icons {
public static function get() {
return [
'' => 'icon-add-circle-1',
'' => 'icon-airplane',
'' => 'icon-alien',
...
...
];
}
}
I have made my changes in each of the above files and everything was working as expected. But will be cause a problem when an update of the theme will be installed in future. How can I extend the classes in the child theme so that I can use my own local icons?
Hope there is somebody out there who can help me a bit.
PS: I'm not a developer, but understand a bit of programming
Create a child theme and add your file custom-icons.css
Then add following code to your file functions.php
add_action('wp_enqueue_scripts', 'enqueue_custom_icons', 20);
function enqueue_custom_icons(){
wp_enqueue_style('custom-icons', get_stylesheet_directory_uri() . '/custom-icons.css', [], '1.0.0');
}
This will properly add your CSS file to your theme. And having a child theme allows you to update the parent theme (main theme) without having to re-add this code upon update.
I need to override all of child theme templates woocommerce and parent templates in a plugin. Basically, I'm making couple of copies to my site that has some custom functionalities added via the child theme. So, in order to update all of the sites at once I want to use a plugin instead of a child theme. Because I assume it's pretty difficult to make child theme do automatically updates. (maybe I'm wrong in this)
I'm using a parent theme that has different versions of woocommerce templates. For example, child-theme/woocommerce/cart/cart-v2.php is a cart template.
This is what I use to override woocommerce templates, but they override woocommerce plugin templates not parent theme templates.
add_filter( 'woocommerce_locate_template', 'woo_adon_plugin_template', 1, 3 );
function woo_adon_plugin_template( $template, $template_name, $template_path ) {
global $woocommerce;
$_template = $template;
if ( ! $template_path )
$template_path = $woocommerce->template_url;
$plugin_path = RCW2P_PLUGIN_PATH . '/templates/woocommerce/';
if(file_exists( $plugin_path . $template_name ))
$template = $plugin_path . $template_name;
if( ! $template )
$template = locate_template(
array(
$template_path . $template_name,
$template_name
)
);
if( ! $template && file_exists( $plugin_path . $template_name ) )
$template = $plugin_path . $template_name;
if ( ! $template )
$template = $_template;
return $template;
}
With this code I have to have this path for cart page plugin/templates/woocommerce/cart/cart.php
Also, this code doesn't overrides templates files that are in woocommerce folder, such as taxonomy-product-cat.php
Any help is much appreciated.
In my plug-in i'm trying to override part of the theme's template with a template of my own but I'm struggling to get it to work.
add_filter( 'template_include', 'demo_page_template');
function demo_page_template($template) {
if ( is_product() ) {
$template = untrailingslashit( plugin_dir_path( __FILE__ ) ) . 'templates/woocommerce/single-product.php';
}
return $template;
}
I have added the appropriate files and folders but I'm not getting any reaction.
thanks for reading, appreciate any help I can get.
A function in the parent functions.php is requiring an include. I have editted this file its calling, but obviously its still in the parent directory.
require get_template_directory() . '/inc/widgets/team-member.php';
How can i ammend the file url in the child functions.php?
Add this code in parent theme functions.php
add_action( 'wp_loaded', 'parent_prefix_load_classes', 10 );
function parent_prefix_load_classes()
{
define('MEMBER_FILE_URL', get_template_directory(). '/inc/widgets/team-member.php');
}
Use this code in chile theme
// Priority 11 to run after the parent theme's loader.
add_action( 'wp_loaded', 'child_prefix_create_objects', 11 );
function child_prefix_create_objects()
{
echo MEMBER_FILE_URL;
}
I want to use a file conditionally upon user input. I am using WooCommerce templates in my child theme. Of course, the file (content-single-product.php) is customized in the child theme. However, I also wish to give users an option to use the default WC file if they want to. In short, I want to use that file conditionally which user will choose.
I am using the PHP copy() and unlink() functions. I just want your expert opinion if this is the best solution or if you have any suggestions better than mine. Here are the functions in my child theme.
/*...for copying the file from a child theme folder to the woocommerce folder...*/
function ac_wc_files_to_theme()
{
$theme_dir = get_stylesheet_directory() . '/woocommerce/files/content-single-product.php';
$theme_dir_file = get_stylesheet_directory() . '/woocommerce/content-single-product.php';
if (!copy($theme_dir, $theme_dir_file)) {
echo "failed to copy $theme_dir to $theme_dir_file...\n";
}
}
/*...for removing the file from a the woocommerce folder...*/
function ac_wc_delete_wc_file(){
$fileArray = array(
get_stylesheet_directory() . '/woocommerce/content-single-product.php'
);
foreach ($fileArray as $value) {
if (file_exists($value)) {
unlink($value);
} else {
echo 'file not found';
}
}
}
/*...calling the options from the theme settings area....*/
if (get_option('ac_wc_default_single') == 1) {
add_action('init', 'ac_wc_delete_wc_file');
remove_action('init', 'ac_wc_files_to_theme');
}
else {
add_action('init', 'ac_wc_files_to_theme');
remove_action('init', 'ac_wc_delete_wc_file');
}
The codes are working fine. Just I need your view if this will do. Thanks
Here's my best guess at filtering the template without extensive testing.
// change the default template path to `woocommerce/files`
function ac_wc_override_template_path(){
return 'woocommerce/files/';
}
add_filter( 'woocommerce_template_path', 'ac_wc_override_template_path' );
// now filter the woocommerce template
function ac_wc_override_single_product($template, $template_name, $template_path) {
$alt_template = '';
if ($template_name == 'content-single-product.php') {
$alt_template = locate_template( trailingslashit( $template_path ) . 'content-single-product-1.php' );
}
if( $alt_template && get_option( 'use_my_themes_templates' ) ){
return $alt_template;
} else {
return $template;
}
}
add_filter('woocommerce_locate_template', 'ac_wc_override_single_product', 20, 3);