I want to put a function in the functions.php file so that when the user activates the template, all the files with .mo and .po extensions are copied to the wp-content\languages\themes
path.
For this purpose I have used the following code which did not work:
function load_the_languages() {
load_theme_textdomain( 'themePro', get_template_directory() . '/languages' );
}
add_action( 'after_setup_theme', 'load_the_languages' );
The source I used to write the above function:
Please help me to create a function that can copy all files with .mo and .po extension to wp-content\languages\themes
path.
I came up with the above code after a lot of searching and I hope there is a better way to do this.
Thanks in advance for any help.
CodePudding user response:
The function load_theme_textdomain
() is used to load the translation files for the theme, but it doesn't actually copy the files to a different location. If you want to copy the .mo and .po files from your theme's languages
directory to the wp-content/languages/themes directory
when the theme is activated, you can use the after_switch_theme
hook, which fires after the theme iss switched.
function copy_language_files_to_global_directory() {
$source_directory = get_template_directory() . '/languages';
$destination_directory = WP_CONTENT_DIR . '/languages/themes';
if (!is_dir($destination_directory)) {
wp_mkdir_p($destination_directory);
}
// Copy .mo and .po files
foreach (glob($source_directory . '/*.{mo,po}', GLOB_BRACE) as $file) {
$destination_file = $destination_directory . '/' . basename($file);
if (!file_exists($destination_file)) {
copy($file, $destination_file);
}
}
}
add_action('after_switch_theme', 'copy_language_files_to_global_directory');
Hope that helps!