I'm working on a custom Wordpress plugin but I cannot get it multi-lang ready.
It does load the .mo file of the main language properly, but when switching languages (using WPML), it always shows the translation of the main language (in this case German). So when I am on English, it still shows the German translations.
Here's my code:
in the header:
/*
Plugin Name: MM Jobs
Plugin URI: http://example.com/
Description: Custom Jobs Plugin to create new Jobs
Version: 1.3.84
Author: Jekey
Author URI: http://example.com/
Text Domain: mm-jobs
Domain Path: /languages
*/
then:
function mm_jobs_plugins_loaded() {
load_plugin_textdomain( 'mm-jobs', false, dirname( plugin_basename( __FILE__ ) ) . '/languages/' );
}
add_action( 'plugins_loaded', 'mm_jobs_plugins_loaded', 0 );
.mo files are correct, as it already loads the German translation.
Named: mm-jobs-en_US.mo or mm-jobs-de_DE.mo under /plugins/mm-jobs/languages/
You have any idea what's causing the problem?
In case someone having the same problem. I had
get_plugin_data( __FILE__ );
in my code. This caused to run a wp_core function where it loads the textdomain, so my en_US.mo was overriden by de_DE.mo
I don't know why get_plugin_data() took the wrong lang-file. It seems to have picked the right one for different plugins that use the function.
Related
i have created a plugin "translation_test" to test the translation capabilities of wordpress.
Translation files
in the languages directory there is a file translation_test.pot that contains:
msgid "Testing"
msgstr ""
and a file translation_test-de_DE.po:
msgid "Testing"
msgstr "Das ist ein test"
using gettext i have created the corresponding .mo file with this command:
msgfmt translation_test-de_DE.po -o translation_test-de_DE.mo
i have uploaded all 3 files to the plugin's languages directory: ./translation_test/languages/
Plugin php file:
there is also an php file called "translation_test" where the textdomain is loaded and an admin page created to display the translated text:
<?php
/*
Plugin Name: translation_test
Plugin URI:
Description: translation_test
Version: 1.0
Author:
Author URI:
Text Domain: translation_test
*/
function translation_test_init_textdomain()
{
load_plugin_textdomain( 'translation_test', false, dirname( plugin_basename( '__FILE__' ) ) . '/languages/' );
}
add_action( 'init', 'translation_test_init_textdomain' );
/**
* Register a custom menu page.
*/
function wpdocs_register_my_custom_menu_page(){
add_menu_page( 'title','custom menu','manage_options','custompage','my_custom_menu_page','',6 );
}
add_action( 'admin_menu', 'wpdocs_register_my_custom_menu_page' );
function my_custom_menu_page()
{
echo __( 'Testing', 'translation_test' );
}
?>
Language setting
in the admin area of wordpress i set the language to german and additionally i added the following line to the wp-config.php file:
define ('WPLANG', 'de_DE');
sadly the translation does not work and it always shows "Testing". does anyone know where the error is? is there some bare-bones plugin template that has a working translation that i could use a starting point?
if you're not making a plugin or theme, you're just loading wp-load.php into your application, try doing it this way. Try this:
//get User Locale
$user_locale = get_user_locale();
// load text domain and .mo file
load_textdomain('your_text_domain', $_SERVER['DOCUMENT_ROOT'].'/wp-content/plugins/your_plugin/your_text_domain-'.$user_locale.'.mo');
// function to use with locale hook
function wpsx_redefine_locale($locale){
global $user_locale;
if ($user_locale == ''){
return "en_US";
} else {
return $user_locale;
}
}
// define locale hook
add_filter('locale','wpsx_redefine_locale',10);
// test translation
echo __('test','your_text_domain');
Obs:
I use LOCO translate to manage my text domains and .mo and .po files. https://br.wordpress.org/plugins/loco-translate/
What am I doing wrong....this doesnt seem to work... I keep getting the following error
Warning: Cannot modify header information - headers already sent by (output started at /home/puretige/public_html/wp-content/plugins/Pure Tiger/puretiger.php:13) in /home/puretige/public_html/wp-includes/option.php on line 568
Warning: Cannot modify header information - headers already sent by (output started at /home/puretige/public_html/wp-content/plugins/Pure Tiger/puretiger.php:13) in /home/puretige/public_html/wp-includes/option.php on line 569
The plugin generated 2 characters of unexpected output during activation. If you notice “headers already sent” messages, problems with syndication feeds or other issues, try deactivating or removing this plugin.
Please note I am very new to this....
<?php
/*
Plugin Name: Pure Tiger Hosting
Plugin URI: http://www.puretigerhosting.com/wordpressplugin
Description: Themes, Plugins and Support from within your admin panel.
Version: 1.00
Author: Pure Tiger Hosting
Author URI: http://www.puretigerhosting.com
License: GPL2
*/
?>
<?php
add_action('admin_menu','register_custom_menu_page');
add_action('admin_menu','register_my_custom_submenu_page');
add_action('admin_menu','register_my_custom_submenu_page2');
add_action('admin_menu','register_my_custom_submenu_page3');
Function register_custom_menu_page() {
add_menu_page('Pure Tiger', 'Pure Tiger','pure-tiger','add_users','','', 6);
}
/*-----------------------------Sub Pages--------------------------------------------*/
function register_my_custom_submenu_page() {
add_submenu_page( 'pure-tiger','Themes','Themes','PTthemes','','','my_custom_submenu_page_themes');
}
function register_my_custom_submenu_page2() {
add_submenu_page( 'pure-tiger','Plugins','Plugins','PTPlugins','','','my_custom_submenu_page_plugins');
}
function register_my_custom_submenu_page3() {
add_submenu_page( 'pure-tiger','Ask a Question','Ask a Question','question','','','my_custom_submenu_page_question');
}
/*-----------------------------Themes--------------------------------------------*/
function my_custom_submenu_page_themes() {
echo '<h3>Pure Tiger Themes</h3>';
}
/*-----------------------------Plugins--------------------------------------------*/
function my_custom_submenu_page_plugins() {
echo '<h3>Pure Tiger Plugins</h3>';
}
/*-----------------------------Support--------------------------------------------*/
function my_custom_submenu_page_question() {
echo '<h3>Pure Tiger Support</h3>';
}
?>
Try removing the open close php tags before your plugin declaration.
/*
Plugin Name: Pure Tiger Hosting
Plugin URI: http://www.puretigerhosting.com/wordpressplugin
Description: Themes, Plugins and Support from within your admin panel.
Version: 1.00
Author: Pure Tiger Hosting
Author URI: http://www.puretigerhosting.com
License: GPL2
*/
<?php
add_action('admin_menu','register_custom_menu_page');
//remaining code to follow....
Remove close ?> and open <?php tags after copyright notice. 2 newline prevent wordpress from sending headers.
<?php
/*
Plugin Name: Pure Tiger Hosting
Plugin URI: http://www.puretigerhosting.com/wordpressplugin
Description: Themes, Plugins and Support from within your admin panel.
Version: 1.00
Author: Pure Tiger Hosting
Author URI: http://www.puretigerhosting.com
License: GPL2
*/
add_action('admin_menu','register_custom_menu_page');
add_action('admin_menu','register_my_custom_submenu_page');
Check if this plugin trys to write a cookie.
If this is the case, then your plugin has to be initiated BEFORE any html code processing.
So you have to initiate the plugin in the top of your index.php, before the <!doctype ... >
Some reading about cookies : http://php.net/manual/en/features.cookies.php
I am building a full design into WordPress for the first time and I am trying to load in stylesheets and script files but all I seem to be getting is the text output of the location.
What I have is below..
wp_enqueue_style('reset', bloginfo('template_url') . '/reset.css');
wp_enqueue_style('style', bloginfo('stylesheet_url'), array('reset'));
wp_enqueue_style('rhino', bloginfo('template_url') . '/rhinoslider-1.05.css', array('reset','style'));
Do I need to put this inside the link tags or something? I thought it would do it all itself; as what's the point loading it that way if it doesn't do it itself? I know it makes sure the same file isn't included twice or something, but if you have to include the link tags yourself and then WP decides not to include the file then you are left with blank link tags!?
Lastly, should I set these up beforehand so I can just call them via their handles? If so, where? functions.php?
Edit: I also tried putting the below in my themes functions.php file but got the same results.
add_action( 'after_setup_theme', 'mmw_new_theme_setup' );
function mmw_new_theme_setup() {
/* Add theme support for post formats. */
add_theme_support( 'post-formats' );
/* Add theme support for post thumbnails. */
add_theme_support( 'post-thumbnails' );
/* Add theme support for automatic feed links. */
add_theme_support( 'automatic-feed-links' );
/* Add theme support for menus. */
add_theme_support( 'menus' );
/* Load style files on the 'wp_enqueue_scripts' action hook. */
add_action( 'wp_enqueue_scripts', 'mmw_new_load_styles' );
}
function mmw_new_load_styles() {
$foo = bloginfo('template_url') . '/reset.css';
$bar = bloginfo('template_url') . '/rhinoslider-1.05.css';
wp_enqueue_style('reset', $foo);
wp_enqueue_style('style', bloginfo('stylesheet_url'), array('reset'));
wp_enqueue_style('rhino', $bar, array('reset','style'));
}
When storing values in a variable via PHP use:
get_bloginfo()
So your new function would now look like:
function mmw_new_load_styles() {
$foo = get_bloginfo('template_url') . '/reset.css';
$bar = get_bloginfo('template_url') . '/rhinoslider-1.05.css';
wp_enqueue_style('reset', $foo);
wp_enqueue_style('style', bloginfo('stylesheet_url'), array('reset'));
wp_enqueue_style('rhino', $bar, array('reset','style'));
}
And be more semantic! It makes code for beginners easier to look at. ($foo could be $resetCssUrl)
I was having similar issues.
The register / enqueue scripts are so that you can globally assign your functions to load in the correct order. You can call them from the page that your working in but it is considered better practice do it this way.
My template has a functions.php but its nealry empty! It sepreates the scripts into 7 subchapters, theme-options, theme-functions, themes-js, etc. Here is my themes.js.php file but this could quite easily placed in your file inside your wp-content/themes/functions.php My themes-js.php file
I do not know if I can ask word press templated questions here. I was trying to add a line to functions.php in order to display images in RSS feeds. However, I missed to add a "/ "at the end of my comment line and my website crashed.
I am getting this error:
Fatal error: Call to undefined function load_theme_textdomain() in /home/content/31/6570531/html/wp-includes/functions.php on line 33
the 33 line of functions.php has this line of code:
load_theme_textdomain( 'btp_theme', get_template_directory().'/languages' );
is there anyone who knows what should I do? I reupload template files but still getting the same error.
this is the part of error I think
/* Initialize translation mechanism */
load_theme_textdomain( 'btp_theme', get_template_directory().'/languages' );
$locale = get_locale();
$locale_file = TEMPLATEPATH . "/languages/$locale.php";
if ( is_readable($locale_file) )
require_once($locale_file);
/* EnableWP Auto Feed Links */
add_theme_support('automatic-feed-links');
/* Enable post thumbnails support */
add_theme_support( 'post-thumbnails' );
/* Enable custom backgrounds support */
add_custom_background();
if ( ! isset( $content_width ) ) $content_width = 593;
I solved my problem and want to share it with you.
You should download a fresh copy of WordPress. Than delete
wp-config.php
and
wp-content directory
from your wordpress copy on your harddrive. These are vital steps in order you to not loose your settings and content.
Than upload the Wordpress files via FTP to your server.
I am writing a plugin that will take advantage of other plugin's features (think about a plugin for a plugin).
My file lies in /plugins/new-plugin/new-plugin.php
and I need to make a
include(/plugins/OLD_plugin/old-plugin.php)
so I can use a couple of functions from the old-plugin.php file.
What is the correct way to do this? I could maybe make the functions in old-plugin.php available globally, but I don't want to change the old-plugin.php file.
I've already tried several ways to do this, but none worked. The new-plugin will only show some info in an options page, not viewable for the general public and does not interact with any public page or post in my site.
I've already tried $_SERVER, WP_PLUGIN_DIR, WP_CONTENT_DIR, the absolute server path, relative paths and even some black magic, but nothing seems to work good.
With some of this solutions the plugin's options page shows good but the blog's pages do not render. With other solutions the inverse happens, and with some other solutions nothing even render, be it admin pages or blog's pages, all with errors regarding to file not found.
The new-plugin.php is as simple as
<?php
/*
WP Common Headers
*/
global $wpdb;
if ( ! defined( 'WP_CONTENT_DIR' ) )
define( 'WP_CONTENT_DIR', ABSPATH . 'wp-content' );
if ( ! defined( 'WP_PLUGIN_DIR' ) )
define( 'WP_PLUGIN_DIR', WP_CONTENT_DIR . '/plugins' );
include '/server-absolute-path/public_html/gameblogs/wp-content/plugins/old-plugin/old-plugin.php';
add_action('admin_menu', 'new_plugin_menu');
function new_plugin_menu() {
$page_title = 'New Plugin';
$menu_title = 'New Plugin';
$function = 'new_plugin_admin_page';
$menu_slug = 'new_plugin';
add_menu_page($page_title, $menu_title, 0, __FILE__, $function);
}
function new_plugin_admin_page() {
$result = old_plugin_link_data(" WHERE link_destination NOT LIKE '/%' AND link_destination NOT LIKE '%gameblogs%'");
$total = count($result);
old_plugin_list_links($result, $total, FALSE, FALSE);
*/
}
?>
thanks for any ideas!
check the old plugin files and see if there are any do_actions or apply_filters in it. If there are then you can hook into the old plugin script with your new plugin using add_action and apply_filters and execute other things you want to do.
see http://codex.wordpress.org/Function_Reference/do_action
and http://codex.wordpress.org/Function_Reference/apply_filters
For example (very basic example):
If in old plugin you find a:
do_action('some_type_of_reference);`
In your new plugin you can hook into it by doing:
`add_action('some_type_of_reference', 'name_of_my_function');
function name_of_my_function() {
//executed code here
}`
If in old plugin you find a:
apply_filters('some_type_of_reference', $variable);
Then in your new plugin you can hook into the filter by doing:
apply_filter('some_type_of_reference', 'my_function');
function my_function( $variable ) {
//act on the variable from the filter.
return $variable;
}
Have you looked at the plugins_url function? I haven't had an in-depth read through your code, but it might help.
The plugins_url template tag retrieves the url to the plugins directory or to a specific file within that directory. You can hardcode the plugin slug in $path or pass FILE as a second argument to get the correct folder name.
Hope this helps!