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!
Related
I don't know how to manually add a text-domain to my child theme.
I installed LocoTranslate to find out what the text-domain was, and it shows the same as the main theme.
This is a problem, but I need to add a file that has been made custom and it must call this child theme in various parts of the code, now it calls the main theme and this is the problem.
Searching I found [WordPress documentation on child themes,][1] but I must say that I don't know how to interpret it, my experience is not enough to know how to read this documentation.
I don't even keep looking and I can't find a site that describes how to do this by someone inexperienced.
Is there a document outlining the steps to create a child theme text-domain for beginners?
Go into the style.css file in your child theme. In the comment header, add a text domain field under the theme name field.
/*
Theme Name: My Child Theme
Text Domain: mychildtheme
*/
Then, go to your functions.php file in your child theme and add the following code.
function mychildtheme_load_textdomain() {
load_child_theme_textdomain( 'mychildtheme', get_stylesheet_directory() . '/languages' );
}
add_action( 'after_setup_theme', 'mychildtheme_load_textdomain' );
Change 'mychildtheme' to the text domain you gave in your style.css file.
For some reason my child theme templates are not being recognized.
I believe I have followed the correct procedure (and checked for caching etc)
/wp-content/themes/divi-child/includes/builder/module/Blog.php
should replace
/wp-content/themes/Divi/includes/builder/module/Blog.php
(same path and same file with a slight update)
The child theme module template is not recognized (changes to the child theme template have no effect)
I have tested editing the main file and this works immediately every
time.
Any advice greatly appreciated.
Cheers
EDIT
The below should work according to Divi however it breaks the site when I try.
Apparently it is not enough to just copy the module into the child theme. The file needs to be duplicated. Then copied file to child-theme/custom-modules/Blog.php. After adding following code to the bottom of the functions.php file:
function divi_custom_blog_module() {
get_template_part( '/custom-modules/Blog' );
$myblog = new custom_ET_Builder_Module_Blog();
remove_shortcode( 'et_pb_blog' );
add_shortcode( 'et_pb_blog', array( $myblog, '_render' ) );
}
add_action( 'et_builder_ready', 'divi_custom_blog_module' );
There are a few other steps https://intercom.help/elegantthemes/en/articles/4532734-moving-blog-module-in-child-theme
Create a new folder in the child theme folder, for example, includes folder.
Now copy the Divi/includes/builder/module/Blog.php file from the parent theme into the child-theme/includes/ folder.
Open up the Blog.php file of your child theme and replace this line (at the very top):
require_once 'helpers/Overlay.php';
class ET_Builder_Module_Blog extends ET_Builder_Module_Type_PostBased {
with:
get_template_part( '/includes/builder/module/helpers/Overlay.php' );
class custom_ET_Builder_Module_Blog extends ET_Builder_Module_Type_PostBased {
Replace: $this->vb_support = 'on'; with $this->vb_support = 'off';
Remove this line from the bottom: new ET_Builder_Module_Blog();
Finally, add the following code to the functions.php file in your child theme folder:
/*================================================
#Load custom Blog Module
================================================*/
function divi_custom_blog_module() {
get_template_part( '/includes/Blog' );
$myblog = new custom_ET_Builder_Module_Blog();
remove_shortcode( 'et_pb_blog' );
add_shortcode( 'et_pb_blog', array( $myblog, '_render' ) );
}
add_action( 'et_builder_ready', 'divi_custom_blog_module' );
I have got this issue similar to this before. I think - maybe the blog template is called by another file in the "father theme" folder some thing like:
$url = dirname(__FILE__)."/includes/builder/module/Blog.php";
this will cause the issue. to fix this there are two way:
#1: find and edit the file wich is call the blog template (not recommend)
#2: find and copy the file wich is call the blog template to your child theme (you can try with index.php, content.php or single.php first) (recommend)
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
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
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