John Davidson

php - Wordpress Multilanguage with variable and get request

0 comments
Message:


i tried almost all plugins and not giving me what im looking for


im looking to click on a _GET link and convert site / content to another dir / lang and have 2 variable for each Lang as example :


  $is_lang = htmlspecialchars($_SESSION['WPLANG'], ENT_QUOTES);
$is_en = htmlspecialchars('en_US', ENT_QUOTES);
$is_ar = htmlspecialchars('ar', ENT_QUOTES);

if(isset($is_lang) = $is_en):
--
-- show something
--
elseif(isset($is_lang) = $is_ar):
--
-- show something
--
endif;

so i end up with this wp-config :


$language = isset( $_GET['lang'] ) ? htmlspecialchars($_GET['lang'], ENT_QUOTES) : 'en';
switch ( $language ) {
case 'ar':
define( 'WPLANG', 'ar' );
$_SESSION['WPLANG'] = 'ar';
break;

case 'en':
default:
define( 'WPLANG', 'en_US' );
$_SESSION['WPLANG'] = 'en_US';
}


My Answer:
This code snippet looks like it is trying to set the language of the WordPress site based on a GET parameter 'lang' in the URL. It also sets a session variable 'WPLANG' to store the selected language.

However, there are a few issues in the code:

1. The comparison in the if statements should use '==' instead of '='. So it should be `if(isset($is_lang) == $is_en)` instead of `if(isset($is_lang) = $is_en)`.

2. The isset function should be used to check if a variable is set, not to compare values. So instead of `isset($is_lang) == $is_en`, you should use `$is_lang == $is_en`.

3. The switch statement in wp-config.php should have a default case to handle other language options. You can add a default case like this:

php
default:
define( 'WPLANG', 'en_US' );
$_SESSION['WPLANG'] = 'en_US';


4. Make sure that the session_start() function is called at the beginning of the script to start the session.

Overall, the code looks like it should work for setting the language based on the GET parameter and displaying content accordingly. Just make sure to fix the issues mentioned above.

Rate this post

4 of 5 based on 1614 votes

Comments




© 2024 Hayatsk.info - Personal Blogs Platform. All Rights Reserved.
Create blog  |  Privacy Policy  |  Terms & Conditions  |  Contact Us