Change language only on theme - wordpress

I want to keep the admin in english but the theme in another language. By adding language files to the theme directory nothing happens, the option to switch language wont come up. Is this possible without adding languages to wp-content?

I found a solution using the locale filter and load theme textdomain
The solution looks like this:
define('LOCALE', 'en');
add_action( 'after_theme_setup', 'site_setup' );
function site_setup() {
//Make the theme available for translation
load_theme_textdomain( 'mytheme', get_template_directory() . '/languages' );
$locale = get_locale(); //Will get what you defined earlier
$locale_file = get_template_directory() . "/languages/$locale.php";
if ( is_readable( $locale_file ) ) require_once( $locale_file );
}

Related

Custom post template in a plugin erase theme's one

i work with sportpress on Wordpress. I have a custom plugin that add a lot of function to it.
I created a template for the custom post-type of sportpress. They are in my plugin but i don't find how to make them prioritary on theme. The theme isn't a custom'one, so i don't want to delete or modify anything in it.
I have tried several things but theme's file is everytime superior.
Things tried :
naming the file 'single-....php',
filter : add_filter( 'template_include', 'toornament_template' ),
shortcode in theme file (works but not what i want...),
Thanks a lot...
EDIT :
here is what i tried exaclty, it works with another type of post that has no theme template, but not for the one that has single-post template in the theme...
add_filter( 'template_include', 'player_template', 1 );
function player_template( $template ) {
if ( is_singular( 'sp_player' ) && file_exists( plugin_dir_path(__FILE__) . 'templates/player-tpl.php' ) ){
$template = plugin_dir_path(__FILE__) . 'templates/player-tpl.php';
}
return $template;
};

Triggering wp_update_plugins cron to automatically update plugins in WordPress

I am trying to force plugin updates with WordPress, but it doesn't seem to be working. I need to force these updates within a custom plugin itself.
I added the following filter into my plugin:
add_filter( 'auto_update_plugin', '__return_true' );
I made sure that nothing in the wp_config file disallows auto-updates. Even though I didn't need to, to be safe I also set:
define( 'FS_METHOD', 'direct' );
and define( 'WP_AUTO_UPDATE_CORE', true );
I then installed the Advanced Cron Manager plugin to trigger the wp_update_plugins event, but this did not update any plugins.
I decided to simply call the wp_maybe_auto_update() function within my plugin on init... and it worked and updated my plugin - but also disabled it!
I am wondering if anyone knows why running the wp_update_plugins cron event wouldn't be updating any plugins? There must be a really simple solution here that I'm missing! Your help would be much appreciated!
You could use the following function to update a plugin programatically:
function upgrade_plugin( $plugin_slug ) {
include_once ABSPATH . 'wp-admin/includes/class-wp-upgrader-skin.php';
include_once ABSPATH . 'wp-admin/includes/class-wp-upgrader.php';
wp_cache_flush();
$upgrader = new Plugin_Upgrader();
$upgraded = $upgrader->upgrade( $plugin_slug );
return $upgraded;
}
& you could use that in conjunction with get_plugins
// Check if get_plugins() function exists. This is required on the front end of the
// site, since it is in a file that is normally only loaded in the admin.
if ( ! function_exists( 'get_plugins' ) ) {
require_once ABSPATH . 'wp-admin/includes/plugin.php';
}
$all_plugins = get_plugins();
and then loop over it to update the plugins
foreach ( $all_plugins as $key => $value ) {
upgrade_plugin( $key );
}
I found this article useful when putting together this answer: https://wpreset.com/programmatically-automatically-download-install-activate-wordpress-plugins/
From the answer above I got this to work, you just need to remove the extra / from include_once ABSPATH . '/wp-admin/includes/class-wp-upgrader-skin.php';
to make it: include_once ABSPATH . 'wp-admin/includes/class-wp-upgrader-skin.php';
When I am relying on a cron event (Testing with WP Crontrol plugin) to take care of plugin update, following code seems to be working with given required files:
include_once ABSPATH . 'wp-admin/includes/file.php';
include_once ABSPATH . 'wp-admin/includes/class-wp-upgrader.php';
include_once ABSPATH . 'wp-admin/includes/misc.php';
$upgrader = new Plugin_Upgrader();
$result = $upgrader->upgrade($plugin_slug);
if (is_wp_error($result) || !$result) {
// Check if error, else updated successfully
}

WordPress replace header.php from a plugin

I have searched and cant find any answer.
How can I replace the themes header.php template from a custom plugin?
Everywhere I look it seems like it cant be done, and you can only change template parts from a theme/child theme.
But how does plugins like Elementor page builder do it then in their theme builder?
Thanks,
Daniel
I also needed to include a custom header.php from my plugin. Because you can register templates like page.php, post.php or archive.php from a plugin via the ${type}_template hook, I thought it shouldn't be that difficult to do the same for the header.php, but you can't load a custom header.php from directories other than parent or child theme's one.
When traversing down the get_header function you'll see the locate_template function being invoked to load the header template. But there is no way to hook into that function and to replace the path for the loaded header template:
function locate_template( $template_names, $load = false, $require_once = true, $args = array() ) {
$located = '';
foreach ( (array) $template_names as $template_name ) {
if ( ! $template_name ) {
continue;
}
if ( file_exists( STYLESHEETPATH . '/' . $template_name ) ) {
$located = STYLESHEETPATH . '/' . $template_name;
break;
} elseif ( file_exists( TEMPLATEPATH . '/' . $template_name ) ) {
$located = TEMPLATEPATH . '/' . $template_name;
break;
} elseif ( file_exists( ABSPATH . WPINC . '/theme-compat/' . $template_name ) ) {
$located = ABSPATH . WPINC . '/theme-compat/' . $template_name;
break;
}
}
if ( $load && '' !== $located ) {
load_template( $located, $require_once, $args );
}
return $located;
}
One solution to bypass this limitation would be to get rid of the get_header function within your plugin's templates and to include the plugin's header.php via require or require_once statement from within the plugin's templates. But than you will have to implement your own get_header hook and locate_template functionality to enable replacement from the parent or child theme. See this answer for more information on limitations when omitting the get_header function:
https://wordpress.stackexchange.com/a/5195/80177
I have experience using a similar page-builder to elementor called site origin.
To the best of my knowledge wordpress page-builders don't manipulate or replace the themes template files. Instead, a lot of the page-builder content is displayed in the front end with the_content() found in your template file. In addition, it appends a bunch of css and js scripts to the web page in order to handle the styling of the elements and interactivity.
As for your original question... If I understand correctly, there's no need for this plugin because the header.php file can be freely edited/replaced in the theme editor!
In the admin section, go to 'appearance' > 'editor'. Then under "theme files", select the template for header.php.
I used this solution
add_action('get_header', 'wpbet_replace_theme_header');
function wpbet_replace_theme_header(){
require plugin_dir_path( __FILE__ ) . 'templates/headers/header-padrao.php';
$templates = [];
$templates[] = 'header.php';
remove_all_actions( 'wp_head' );
ob_start();
locate_template( $templates, true );
ob_get_clean();
}
Elementor does not replace the header.php. It adopts whatever header.php the active theme has. It simply takes over the_content() part.
header.php is essential part & asset of a theme.
You may build a page template with a different header (not different header.php)

'poedit' translation activation

I'm trying to make my wordpress site translation ready, using 'poedit'. I have problems with activating the translation. I want to translate the site only, not wordpress system. I have my .pot, .po and .mo files in a 'languages' folder in my theme directory, I have this code in my functions.php file:
load_theme_textdomain( 'example', get_template_directory() . '/languages' );
well, after everything is saved and translated in the poedit program, how do I activate it in my site? I am working with a localhost for now.
First, you need to connect the method to an action hook in your theme functions.php:
function load_mytheme_textdomain() {
load_theme_textdomain( 'example', get_template_directory() . '/languages' );
}
add_action( 'after_setup_theme', 'load_mytheme_textdomain' );
Then you have to use translation instead of echo for output:
_e( 'My Text', 'example' );
Read more here: https://developer.wordpress.org/themes/functionality/internationalization/

Applying translation to custom WordPress plugin

I'm trying to apply a translation to my cusom plugin. I've alredy created brau-ru_RU.mo and brau-ru_RU.po files. My translation domain is 'brau'.
What I've tried is to put files in wp-content/languages/plugins/ and execute this code in my plugin.
$domain = 'brau';
$mo_file = WP_LANG_DIR.'/plugins/'.$domain.'-'.get_locale(). '.mo';
var_dump(load_textdomain( $domain, $mo_file ));
var_dump(load_plugin_textdomain( $domain ));
var_dump(__('This is the test', 'brau'));
Result is:
bool(true) bool(true) string(16) "This is the test"
I also got this code in my config
define ('WPLANG', 'ru_RU');
The text shoud be translated from English to Russian, but it's not. What am I missing?
This is a link to test version of the plugin: https://github.com/Brezgalov/brezgalovauth
If you want to add language files to your plugin then you can create a folder called languages inside your plugin folder and in the plugin main file you can define like this
// Localiztion with language files
function custom_langs_i18n() {
load_plugin_textdomain( 'textdomain', false, dirname( plugin_basename( __FILE__ ) ) . '/languages' );
}
add_action( 'init', 'custom_langs_i18n' );
Please remember to use textdomain where you are using string.

Resources