I've been trying to translate a WordPress theme for days with no success, no matter what I try, the _e translate function doesn't work and the strings just stay the same as they are in the .POT file.
What I've done so far:
Loaded the text domain from the languages folder from inside the theme inside functions.php
function theme_setup() {
load_theme_textdomain( 'domain', get_template_directory() . '/languages' );
}
add_action( 'after_setup_theme', 'theme_setup' );
Wrapped the strings I want to translate in the _e() function, including the text domain.
<?php _e( 'Află mai multe', "domain" ); ?>
The languages folder includes a .POT file generated with EazyPo that includes all strings that are inside _e() functions together with an English translation of my strings with .PO and .MO files called en.PO and en.MO respectively.
Switched the language of the website in WordPress General Settings to English.
What I see from your code is that you action is calling the wrong function.
You should change this: add_action( 'after_setup_theme', 'optimrent_setup' );
To this: add_action( 'after_setup_theme', 'theme_setup' );
Because the second parameter to add_action is to what function the callback should be. So in that code you have above, should display some sort of error if you don't have the function optimrent_setup
Related
I am trying to add translation to my WordPress plugin using .po files, but I can't display the translation.
My .po and .mo files are named as follows:
plugin-name-fr_FR.mo
plugin-name-fr_FR.po
and are located in the ./languages/ folder.
In my code, I load the translations like this:
add_action( 'init', 'load_translation');
function load_translation() {
load_plugin_textdomain( 'plugin-name', false, dirname(plugin_basename(__FILE__ )) . '/languages/' );
}
I have also tried using:
add_action( 'plugins_loaded', 'load_translation' );
but it didn't work either.
The header of my plugin contains :
* Text Domain: plugin-name
* Domain Path: /languages
When I check the response value of the load_plugin_textdomain() function, it returns true so the files are being found, but the translations are not displayed.
For displaying my content, I use :
<?= __( 'My text' ); ?>
I don't know what to do, can someone help me please ?
You have to assign the custom textdomain, provided that your pot files are correct as well.
<?= __( 'My text' , 'plugin-name'); ?>
I am trying to enqueue an external plain JavaScript file from within my plugin php, to add within my footer of the WP theme. Is this possible?
I've tried the below, but test is failing as no alert is firing...
<?php
/**
Plugin Name: Add Footer JS
*/
add_action( 'wp_footer', 'my_footer_scripts' );
function my_footer_scripts(){
?>
<script>alert( 'Hi Roy' ); </script> // just a test
<?php
}
So basically; the JS would be at:
http://dev.website.com/wp-content/plugins/mykewlplugin/js/script-support.js
And this PHP, at:
http://dev.website.com/wp-content/plugins/mykewlplugin/support.php
And core plugin PHP/functionality at:
http://dev.website.com/wp-content/plugins/mykewlplugin/mykewlplugin.php
Make sure that your currently active theme is making use of the wp_footer() and wp_head() functions, as those are required to utilize the appropriate action hooks. With that said, you should really properly enqueue your assets. Anybody else working on the project (including your future self) will be appreciative of that!
This involves utilizing the wp_enqueue_scripts hook and the wp_enqueue_script() function (which relies on both the wp_head() and wp_footer functions).
The proper way to do this would be like the following:
add_action( 'wp_enqueue_scripts', 'no_spex_footer_js' );
function no_spex_footer_js(){
wp_enqueue_script( 'no-spex-footer-js', '/path/to/your/file.js', array(), null, true );
}
Note all the passed arguments, in order:
a unique handle
the path to your actual file
an array of dependencies by handle (if you rely on jQuery, change this to array('jquery'), etc.
version of the file. I use filemtime( plugin_dir_path( __FILE__ ) . 'assets/file-name.js' typically.
in_footer - This is the important one, and determines if the file should be loaded into the header or footer.
If you're sure your theme is making use of wp_head() and wp_footer(), and the file isn't working - it means your plugin isn't configured/activated properly, at which point I'd recommend another cursory glance at the Writing A Plugin documentation and the File Header Requirements.
For your specific case, something like this should get you started:
mykewlplugin.php:
<?php
/**
* Plugin Name: My Kewl Plugin
* Description: Does Kewl Things
* Version: 0.1
*/
add_action( 'wp_enqueue_scripts', 'my_kewl_plugin_footer_js' );
function my_kewl_plugin_footer_js(){
wp_enqueue_script( 'my-kewl-plugin-footer', plugins_url( '/js/script-support.js', __FILE__ ), array(), filemtime( plugin_dir_path( __FILE__ ) . 'js/script-support.js', true );
}
?>
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!
Ok, so im working on a theme that I want to have different styles and php files depending on which design the user has chosen. How would I do this? The best example I can give is having a get_template_part function that changes directory based on the users selection of design. Heres my idea of how the code would kinda look.
<?php /*custom_function*/(/*Global Path variable which changes*/, '/filename') ?>
The x theme has like stacks I think which might be a similar idea. Thanks.
Maybe something like this in the function.php: (just an example)
<?php
define("DESIGN", "flat");
if(DESIGN == "flat"){
add_action( 'wp_enqueue_scripts', 'asd_scripts' );
}elseif(DESIGN == "notflat"){
// other add_action( 'wp_enqueue_scripts', '...' );
}
function asd_scripts() {
// load bootstrap js
wp_enqueue_script('asd-bootstrapjs', get_template_directory_uri().'/includes/resources/bootstrap/js/bootstrap.js', array('jquery') );
}
?>
You need to use the theme customization api to achieve this. For example you would use the customize_register hook:
function mytheme_customize_register( $wp_customize )
{
//All your sections, settings, and controls will be added here
}
add_action( 'customize_register', 'mytheme_customize_register' );
This hook allows you access to the $wp_customize object.
There are existing examples in the WordPress docs, this should get you started with what you would like to do.
I recently started working on a wordpress site.
I have the woo commerce plugin working and he mystile theme. SO far so good!
However the gallery sucks!
I am trying to fiddle aroundwith it, but do not know my way around wordpress too well.
Question:
How do I add a line into the header?
Say for the single product, I would want to include a Javascript file. Do you know how I would do that?
What file do I augment?
Please let me know if you need more information.
Bo
To add a javascript file to the wordpress theme, you have 2 alternative:
Suppose we have a jquery.js inside the directory root of the theme.
Method A)
Open header.php file in wordpress theme. Add this inside <head> tag:
<script src="<?php echo get_template_directory_uri(); ?>/jquery.js"></script>
Method B)
Open functions.php file in wordpress theme:
Search something like add_action( 'wp_enqueue_scripts', 'blahblah' );,
If exist, find a function with blahblah name and add this code inside it:
wp_enqueue_script(
'jquery',
get_template_directory_uri() . '/jquery.js', array(), '1.8.3', false );
Otherwise: You should add a function and add_action method:
function scripts_styles() {
wp_enqueue_script(
'jquery',
get_template_directory_uri() . '/jquery.js', array(), '1.8.3', false );
}
add_action( 'wp_enqueue_scripts', 'scripts_styles' );
Note: Replace jquery string with your file name so.