Get Wordpress Child Theme Path in Wordpress - php

I have a parent and child theme. In my child theme I declared a variable CHILD_DIR so I can add custom JS and CSS files to the my child theme's folder structure. I did this in my functions.php file in my child theme by doing this:
define( 'CHILD_DIR', get_stylesheet_directory_uri() );
This produces a URL which is what I want since I'm using it like this:
wp_enqueue_script('custom', CHILD_DIR .'/custom.js', array( 'jquery' ));
I also added some additional folders to my child theme which I want to use within a Wordpress page. I tried to do something like this:
require_once CHILD_DIR . '/Zend/Loader.php';
I get an error since it's trying to provide a URL and I want a menu path to include the PHP and not try and make it an HTTP request. How can I define a path for the PHP instead of a URL to default to my child theme?

It's almost the same functions:
Parent theme: get_template_directory()
Parent theme or child theme: get_stylesheet_directory()

For Retrieve Theme stylesheet directory URL:
get_theme_file_uri() oR
get_stylesheet_directory_uri()
For Retrieve Absolute File Paths:
get_stylesheet_directory() oR
get_theme_file_uri()

Main theme path : get_template_directory_uri();
Child Theme path : get_stylesheet_directory_uri(); or get_stylesheet_uri();
NOTE : Irrespective of calling js files or css files, For gettig the path of the theme or the path of the child theme we have to use the above functions.

define( 'CHILD_DIR',TEMPLATEPATH.'-child');
me funciono, is working

Related

Bootstrap files get priority over my child theme style.css file

I am creating custom WordPress theme. I have a child theme and parent theme.
One issue is my child theme function.php file not adding bootstrap files, So I called it in parent theme, but now the issue I am getting is bootstrap CSS taking priority over my child style.css.
I have tried this code in my child theme function.php file but its not working.
Its not showing any custom.css in view source. I have both files in correct path
add_action('wp_enqueue_scripts', 'theme_enqueue_styles', 100);
function theme_enqueue_styles()
{ $parent_style = 'flambird-style';
$child_style = array('flambird-style');
wp_enqueue_style($parent_style, get_template_directory_uri() . '/bootstrap/custom.css', array());
wp_enqueue_style('child-style', get_stylesheet_directory_uri() . '/css/custom.css', $child_style);
}
its not adding custom in both condition. I think my child theme function.php file is not working.
Please help me on this. I want to use child theme css file.
enter image description here
Add !important to your css formats when there is a conflict, or host your own copy of Bootstrap with compatible changes.

Wordpress child theme - Overriding a php file which is not in the root folder of a theme

I would like to modify a php file which is in "/inc/sections" folder of the Shop Isle theme (Wordpress) through a child theme. I tried the classic way by creating a new php file with the same name in my child theme but it s not working. I ve read there is a way to make the child file overriding the parent one using a function.
All what I ve tried didn t work or like this code below create some bugs (scrolling problem, blank space, etc):
<?php
require get_stylesheet_directory() . '/inc/sections/shop_isle_big_title_section.php' ;
?>
Do you know a solution ?
front-page.php code:
/* Wrapper start */
echo '<div class="main">';
$big_title = get_template_directory() . '/inc/sections/shop_isle_big_title_section.php';
load_template( apply_filters( 'shop-isle-subheader', $big_title ) );
Ok it s working now!!!
I just had to create a new front-page.php in my child theme and replace "template" of get_template_directory with "stylesheet" ...
Thanks for your help

How to localize child theme with Loco Translate

I am getting my head around adding localizable strings to a wordpress child theme, and have not been able to do this successfully.
I have a child theme with a .php page to which I want to add a localizable string. In my child theme's functions.php, I have added the following line:
load_theme_textdomain( 'i-craft-child', get_template_directory() . '/languages' );
Next, using Loco Translate, I uploaded the files de_DE.po and de_DE.mo to the directory /languages within the child theme directory.
Finally, I added the following line to my html page:
<span><small>><?php _e( 'Your email address is also your username and it cannot be changed', 'i-craft-child' ); ?></small></span>
However, the span above is displayed in English (instead of German). I am not sure where in the localization process am I failing and would appreciate any pointers to solve this problem.
Make sure that your theme text-domain is included, you can use something like this (if file doesn't exists, it will break your site, so use it on test environment).
$loaded = load_child_theme_textdomain( 'i-craft-child', get_template_directory() . '/languages' );
if( ! $loaded ) {
echo 'Unable to load files';
die;
}
Also, did you specify WP_LANG in wp-config.php, like this?
define('WP_LANG', 'de_DE'); // for example
Following up on the pointer from unixarmy above, the problem was that the function load_child_theme_textdomain was not able to read the files, due to me using the function get_template_directory() as a parameter. get_template_directory() will return the path of the parent theme, not the child. Substituting that for get_stylesheet_directory() solved the problem.
I achieved that problem going to the Advanced configuration inside Loco:
Themes -> Child Theme Name -> Advanced.
Inside this area you can define the 'text domain' you will use when translating your theme, for example:
<?php _e('My string','generatepress-child'); ?>
Where generatepress-child is the text domain.
In my case the child theme and parent theme where merging on the same path (on the parent path) and loco wasn't able to 'fetch / scan' the strings inside the child theme, so I added manually the new child path inside this advanced section and now I can translate the strings inside my child theme.
After this I have had to add this lines on my functions.php parent theme:
function wpdocs_child_theme_setup() {
load_child_theme_textdomain( 'generatepress-child', get_stylesheet_directory() );
}
add_action( 'after_setup_theme', 'wpdocs_child_theme_setup' );
Hope it helps!

How to load up a CSS file in a Wordpress child theme that's nested in a different folder and name?

This is probably easy but I just want to make sure I'm doing it correctly. Basically I've been making a bunch of Wordpress sites using child themes and usually the style.css is in the root folder of the theme and I've been using this in my functions.php file
<?php
add_action( 'wp_enqueue_scripts', 'theme_enqueue_styles' );
function theme_enqueue_styles() {
wp_enqueue_style( 'parent-style', get_template_directory_uri() . '/style.css' );
}
While this file exists in the parent theme I'm using, it's blank and I realized the CSS file being used was a different file name and nested in
\THEME-NAME\framework\css\site\stacks\THEME-CSS.css as opposed to just
THEME-NAME\style.css
So for the child theme do I have to recreate that folder structure and place a same named THEME-CSS.css in the same folder or do I just call it style.css and put it in the child themes root folder like it normally would be? Any other advice would be helpful. Thanks.
To add a stylesheet when in a child theme, these are the two options/behaviors:
// This will point to style.css in child theme
wp_enqueue_style( 'my_child_styles', get_stylesheet_directory_uri().'/style.css' );
// This will point to style.css in the parent theme
wp_enqueue_style( 'my_parent_styles', get_template_directory_uri().'/style.css' );
As you said, when using get_template_directory_uri() the path will always point to the parent theme folder. Use get_stylesheet_directory_uri() to point to the current / child theme folder, if applicable.

Wordpress Child Theme Path

I'm working on my first child theme and I'm running into a lot of confusion with directories and child themes.
I won't use my real website but I have made a directory in my cPanel dedicated to working on my theme which is at www.wp.example.com
Lets call the template testTemplate. I made a child template called testTemplate-child following the Wordpress codex meaning I registered the parent theme in the child theme's function.php
So wp.example.com loads the style from both the parent theme and the child theme which is desired. What it does not load is the javascript files I enqueued in my child theme's functions.php file.
The confusing part is this, if I navigate to www.wp.example.com/testTemplate-child/ my javascript loads up and works.
I'm wondering if anyone can clear this up for me, why does my child's function.php only work in wp.example.com/testTemplate-child.
It's because your child's theme function.php is added before parent's function.php, that's why you can't override some options, also keep in mind that you'll need to call get_stylesheet_directory() to get child's theme path, because if you do another function, it will load parent's path.
Example to load a javascript file located a /js folder:
$jsPath = get_stylesheet_directory() . '/js/yourScript.js';
wp_enqueue_script( 'child-js', $jsPath, array(), '1.0', true )
More info at: https://codex.wordpress.org/Child_Themes

Categories