Wordpress “PHP inside of PHP“ – Integrate qTranslate with Meta Box - php

For a Website (Wordpress) I want to display dates with the well-known plugin Meta Box and the Website should be provided in English as well as in German, with the plugin qTranslate.
German and English date formats are different: German: dd.mm.y (31.12.16), English: mm/dd/y (12/31/16). So to display the right format according to the language chosen by/for the visitor, the two plugins need to work together.
This is how I would display the date without any language option:
<?php echo rwmb_meta( 'exhibition_meta_beginning' ); ?>
This is how I would display translating text on the website outside of the post- and page-contents (like the site navigation):
<?php _e("[:en]Current[:de]Aktuell[:]"); ?>
My question now is, how I can join the two functions. If it helps, I would be fine with splitting the exhibition_meta_beginning in exhibition_meta_beginning_DE and exhibition_meta_beginning_EN, which would mean that I had to enter every date twice when creating a new instance/post, one in German and once in English format.
I haven't found anything useful yet – yes, there are mentions of that and there is some kind of way to use Custom Fields (which is the core basis of Meta Box) with qTranslate, but nothing seems to work and my knowledge in PHP is the bare minimum.
The input fields for the meta data being dates (and times) are defined in a separate PHP file like this (standard Meta Box procedure):
array(
'name' => 'Beginn der Ausstellung',
'id' => $prefix . 'beginn',
'type' => 'date',
'format' => 'dd.mm.y'
),
Neither did it work to implement the bilinguality into the format value right there (it thinks that it is a date's format not a language tag when [:en]... appears there), nor something like that in the part where the data is displayed (sorry for the amateurish attempt):
<?php __("[:en] echo rwmb_meta( 'exhibition_meta_beginn_EN' )[:de]echo rwmb_meta( 'exhibition_meta_beginn_DE' )[:]"); ?>
Thanks!

You can do it like this,
$en = rwmb_meta( 'exhibition_meta_beginn_EN' );
$de = rwmb_meta( 'exhibition_meta_beginn_DE' );
echo __('[:en]'.$en.'[:de]'.$de.'[:]');
or create a function that return the value based on language
function __show_based_on_lang($en, $de) {
return ( qtranxf_getLanguage() == 'en') ? $en : $de ;
}
then to use the function you can just have it like this,
echo __show_based_on_lang( rwmb_meta( 'exhibition_meta_beginn_EN' ), rwmb_meta( 'exhibition_meta_beginn_DE' ) );

Related

Wordpress page title fxn in echo p

This is probably super simple but I just cant seem to figure it out.
How can I pass the current Wordpress page title into this code?
Below is a snippet from Formidable Forms WP plug-in which basically prints statistics from forms within my website.
In this case, the # of entries for a specific form (55jqi) and a specific field(50) are displayed on the pages, showing how many other people also submitted that form.
Im trying to skip needing to update each page (4,380 pages) with the stats output snippet.. and instead call the current page into the stats display code, to show stats for that particular page being viewed.. using an elementor custom post type template.
I need this:
echo FrmProStatisticsController::stats_shortcode(array('id' => '55jqi', 'type' => 'count', 50 => 'runtz'));
To work like this:
echo FrmProStatisticsController::stats_shortcode(array('id' => '55jqi', 'type' => 'count', 50 => 'single_post_title();'));
Replace the input area ‘Runts’ with the current page title, using
single_post_title()
Or similar.
Any help would be amazing!!
There is also a short code available which works the similarly.
[frm-stats id=55jqi type=count 50="Runtz"]
Formidable Forms Plugin shortcode and php page for reference: https://formidableforms.com/knowledgebase/add-field-totals-and-statistics/#kb-field-filters
You are using single_post_title function in a wrong way. By default, this function will echo the output but in the shortcode, you need to return the output.
This function accepts 2 param: 1: $prefix 2: $display
You need to pass $display as false, which will tell the function to return the value.
so you'll have to make a call like this `single_post_title( '', false )``
and your shortcode call will be like this:
echo FrmProStatisticsController::stats_shortcode(
array(
'id' => '55jqi',
'type' => 'count',
'50' => single_post_title( '', false ),
)
);

Multilingual PHP Inline Replacement in HTML ([en]...[/en] etc.)

I am a coding beginner and have my PHP/HTML web project in German. Now I want to make it available in English in the easiest way. I don't want to add other languages in the future, so I want to try it in the easiest (and maybe not the most proper) way.
I have PHP files with HTML content and the selected language available in a var, i.e.:
<?php
$lang = "en";
?>
<h1>Title in German</h1>
So all the German words are inline HTML. My idea was to create something like:
<h1>[de]Title in German[/de][en]Title in English[/en]</h1>
But I have no idea how to replace it on every load in a smart way. So it is more a topic on "live replacement".
Working with constants in an external language file is of course also an option, like all the other options to make a multilingual site I found on Stackoverflow.
But maybe there is a "quick and dirty" possibility option like the one I mentioned?
Thank you for every hint!
You could try and do this will almost only HTML and CSS. You would need to add this at the top of your page:
<?php
$pageLanguage = "en";
function getLanguageStyle($showLanguage)
{
global $pageLanguage;
$display = ($showLanguage == $pageLanguage ? 'inline' : 'none');
return " span.$showLanguage { display: $display }\n";
}
echo "<style>\n".
getLanguageStyle('en').
getLanguageStyle('de').
"</style>\n";
?>
It sets up a style for each language, which you can then use like this:
<h1><span class="de">Title in German</span><span class="en">Title in English</span></h1>
The advantage here is that you don't need to mix HTML and PHP. This is not a normal way of doing this, but it will work. On very complex pages, where these styles are applied after the first render, this might not be pleasant for your visitors.
Usually translations are made that way:
You have key to translation map for each language, then you request some function that takes proper map for that language and returns translation:
function translate(string $lang, string $key) {
/*
* This usually sits in some file in dir like `/src/i18n/en.json`
* And you do then `$translations = json_decode(require "/src/i18n/{$lang}.json")`
*/
$translations = [
'en' => [
'page.title' => 'Page Title',
...
],
'de' => [
'page.title' => 'Page Title In German',
...
],
];
return $translations[$lang][$key] ?? $key;
}
<h1><?= translate($lang, 'page.title'); ?></h1>

Facebook language codes table

If I'm adding like-box or page-plugin so I have to choose the language in which it will be displayed.
But what if I have multilanguage-site system in PHP which remembers only country codes (en,es...). So I probably need a table like this, but with all languages:
en => en_US
es => es_ES ...
Do you know about some?
Facebook is using country codes, but this codes are a bit more specific so they are mostly called Locale ID (language and country code).
You could just grab the XML from facebook with all locale ids used by them:
<locale>
<englishName>English (UK)</englishName>
<codes>
<code>
<standard>
<name>FB</name>
<representation>en_GB</representation>
</standard>
</code>
</codes>
</locale>
You can find the whole file here (https://www.facebook.com/translations/FacebookLocales.xml)
BUT:
The Problem here is, that you cannot refer from en to en_US or en_GB. So the mapping from 2 to 4 letters won't work. You need to have a table with the language and country code like en_US.
Most modern browser send an "accept language" header, but beware It's not 100% reliable and it's just the primary language of the users browser and not really the country code.
try: var_dump( $_SERVER['HTTP_ACCEPT_LANGUAGE'] );
to get: fr-FR,fr;q=0.8,en-US;q=0.6,en;q=0.4
You can also try to use an API i.e. http://www.hostip.info/
An example would be:
$theFirstPart = "en";// you said you already got this
$userIP = "12.215.42.19";// from $_SERVER var
$languageCode = "en_US";// your standard
//you should use curl for this
// and it's really slow, so please cache this for at least a day or two ;)
$content = file_get_contents( "http://api.hostip.info/get_json.php?ip=$userIP" );
if( $content ) {
$json = json_decode( $content );
if( $json && isset( $json->country_code ) ) {
// before you assign the value, you should check if it's in the facebook xml
$languageCode = $theFirstPart.'_'.$json->country_code;
}
echo "<pre>";
var_dump( $languageCode );
}
Finally I created this table from all supported facebok lang-locales then I picked one using stripos() and because it few-times picked up wrong code, I re-ordered the array by putting prefered to top of the array.
Maybe it's not the best solution, but it solved my problem.
function fb_lang($lang_code){
$fb_locales = [
'es_ES', 'en_US', 'fr_FR', 'tr_TR', 'sv_SE', // prefered codes are moved to line
'af_ZA', 'sq_AL', 'ar_AR', 'hy_AM', 'ay_BO', 'az_AZ', 'eu_ES', 'be_BY', 'bn_IN', 'bs_BA', 'bg_BG', 'ca_ES', 'ck_US',
'hr_HR', 'cs_CZ', 'da_DK', 'nl_NL', 'nl_BE', 'en_PI', 'en_GB', 'en_UD', 'eo_EO', 'et_EE', 'fo_FO', 'tl_PH', 'fi_FI',
'fb_FI', 'fr_CA', 'gl_ES', 'ka_GE', 'de_DE', 'el_GR', 'gn_PY', 'gu_IN', 'he_IL', 'hi_IN', 'hu_HU', 'is_IS', 'id_ID',
'ga_IE', 'it_IT', 'ja_JP', 'jv_ID', 'kn_IN', 'kk_KZ', 'km_KH', 'tl_ST', 'ko_KR', 'ku_TR', 'la_VA', 'lv_LV', 'fb_LT', 'li_NL',
'lt_LT', 'mk_MK', 'mg_MG', 'ms_MY', 'ml_IN', 'mt_MT', 'mr_IN', 'mn_MN', 'ne_NP', 'se_NO', 'nb_NO', 'nn_NO', 'ps_AF', 'fa_IR',
'pl_PL', 'pt_BR', 'pt_PT', 'pa_IN', 'qu_PE', 'ro_RO', 'rm_CH', 'ru_RU', 'sa_IN', 'sr_RS', 'zh_CN', 'sk_SK', 'sl_SI', 'so_SO',
'es_LA', 'es_CL', 'es_CO', 'es_MX', 'es_VE', 'sw_KE', 'sy_SY', 'tg_TJ', 'ta_IN', 'tt_RU', 'te_IN', 'th_TH',
'zh_HK', 'zh_TW', 'uk_UA', 'ur_PK', 'uz_UZ', 'vi_VN', 'cy_GB', 'xh_ZA', 'yi_DE', 'zu_ZA'
];
foreach($fb_locales as $fbl){
if(stripos($fbl,$lang_code)!==false){
return $fbl;
}
}
trigger_error('Fb_lang() couldn\'t find equvalent for language code "'.$lang_code.'"');
return 'en_US';
}
Hope it will help someone else.

PHP variable not returned in array

I am trying to place a variable inside a PHP array which will work with a Wordpress plugin. Here is the code I have:
function custom_list( $lists ) {
$fil = "Dump";
$new_lists = array(
'ddl-list-block' => array(
'name' => __( 'Download Block', 'delightful-downloads' ),
'format' => "
<article class=\"ddl-list-block ddl-list-item\" id=\"ddl-%id%\">
<div class=\"download-wrap\" style=\"background-image: url('fi');\">
<div class=\"download-item\">
<div class=\"download-details\">
<h2>%title%</h2>
".$fil." /* VARIABLE SHOULD APPEAR HERE */
<div class=\"download-meta\">
<div class=\"download-meta-data\"></div>
Download
</div>
</div>
</div>
</div>
</article>
"
),
'ddl-list-plain' => array(
'name' => 'Flain List',
'format' => '<i class="fa fa-download"></i>%title% - %date%'
)
);
return $new_lists;
}
add_filter( 'dedo_get_lists', 'custom_list' );
I have tried using ., {, single quotes and double quotes, but I can not get the word "Dump" to display where it is supposed to. The reason I am trying to get it to show the word "Dump" is just to make sure that variables can be passed through into an array, because I am using another Wordpress plugin (Simple Fields) to manage extra fields in my post types and I want to pull out one particular aspect of the field (the URL of an uploaded image), so I will be inserting something like $my_field['url']. But none of the options I am aware of have even inserted a simple string into my array. Is there something I am missing?
I am posting this solely because it answers my problem. Not necessarily because it answers the question. I guess that's because of the plugin that I used which answers to that function.
The variable I was using called a featured image, but it could not be fed into the function. As a solution, I used their dedo_search_replace_wildcards filter and created another wildcard (I think I used %fi% or something like that), which gave me the outcome I wanted. Shame it didn't feed native $ variables into it.

MediaWiki: Show user name in skin

This should be fairly simple for anyone familiar with MediaWiki, but it's stumping me for me because being me.
I'm working on a skin, and I need to show the currently logged in user's name in a top bar - let's assume in plain text, for simplicity's sake, with changes via CSS.
Initially, I was planning on using the automatically generated one used in the personal tools bar, but since the generating line in the skin is
<?php $this->renderNavigation( 'PERSONAL' ); ?>
, it's inseparable from there. I looked in User.php and found its generation line:
public function getUserPage() {
return Title::makeTitle( NS_USER, $this->getName() );
}
So, I figure I might be able to use this function somehow, but I have very little knowledge of PHP, and am unsure how.
EDIT: It appears that this is used for the generation in the personal tools line itself, but again, I'm not sure how to adapt this.
$personal_urls['userpage'] = array(
'text' => $this->username,
'href' => &$this->userpageUrlDetails['href'],
'class' => $this->userpageUrlDetails['exists'] ? false : 'new',
'active' => ( $this->userpageUrlDetails['href'] == $pageurl )
);
Could I duplicate this into a separate function, and make something like the following?
<?php $this->renderNavigation( 'USERNAME' ); ?>
You can use this code:
<?php echo htmlspecialchars($this->getSkin()->getUser()->getName()); ?>
Or, as the User class has a __ToString() magic method:
<?php echo htmlspecialchars($this->getSkin()->getUser()); ?>
Sources :
The SkinTemplate class in MediaWiki code documentation
The User class in the same documentation
CurrentUsers
http://www.mediawiki.org/wiki/Extension:CurrentUsers
GetUserName
http://www.mediawiki.org/wiki/Extension:GetUserName
Modify these extension for your needs
If you indeed just want the username inserted somewhere into the skin HTML, this should do it:
<?php echo htmlspecialchars( $this->username ); ?>

Categories