Wordpress: use = "post" method for multiple language selection

I am creating a bilingual website with two flags as the login page. I plan to use <form method="post"> around the flags so that the user can select the desired language.

Then on the following pages I want to use something like:

 <?php if( $_POST['language']=='uk' ){ echo $uk; }elseif( $_POST['language']=='french' ){ echo $french;} ?> 

So, clicking the flag, they selected the desired language. Will this only work on the next page after they click on the flag or can transfer them to different pages, and still choose which language was selected?

If this does not work, how else can this be done?


UPDATE:

I donโ€™t think I made it clear that I use Wordpress, which $_SESSION does not seem to like.

I have this on a template called region.php to send a language selection:

  <form action="<?php the_permalink(); ?>/home" name="region" method="post"> <div id="uk"> <a href="javascript:document.region.submit()" name="UK"> <img style="margin-bottom:10px;" src="<?php bloginfo('stylesheet_directory'); ?>/images/-uk.png" width="259" height="160" alt="UK" /> </a> <h1 style="color:black!IMPORTANT;">Enter United Kingdom site</h1> </div> <div id="world"> <a href="javascript:document.region.submit()" name="World"> <img style="margin-bottom:10px;" src="<?php bloginfo('stylesheet_directory'); ?>/images/ world.png" width="258" height="160" alt=" Rest of the World" /> </a> <h1 style="color:black!IMPORTANT;">Enter Rest of the World site</h1> </div> </form> 

What do I need to wear every other pattern to check which language has been chosen? To help with an example, if the United Kingdom was chosen, it could simply say โ€œUnited Kingdom,โ€ if the rest of the world was chosen, then it could simply show โ€œPeace.โ€

This is necessary to work on several pages, so if they go to the page where he checks the language, then if they go to the contact page, he checks the language again - everything that should come from the original language choice.

+6
source share
6 answers

The answers

Firstly, to add regional / multilingual support to wordpress, there is no better alternative to this plugin: http://wpml.org/ . It has a cost associated with this, but its not prohibitive (you pay for huge support).

Secondly, you cannot use $ _SESSION by default. WP has no analogues in design. However, there are tons of plugins and tutorials online to get this functionality.

Code property

There was no data entered in the original html form. It was a form that did not represent anything. This form has two views, which are called the same location . Thus, no matter which button is pressed, its value will be fed in the direction of $_POST['location'] .

 <form action="<?php the_permalink(); ?>/home" name="region" method="post"> <div id="uk"> <input type="submit" name="location" value="UK" /> <img style="margin-bottom:10px;" src="<?php bloginfo('stylesheet_directory'); ?>/images/uk.png" width="259" height="160" alt="UK" /> <h1 style="color:black!IMPORTANT;">Enter United Kingdom site</h1> </div> <div id="world"> <input type="submit" name="location" value="World" /> <img style="margin-bottom:10px;" src="<?php bloginfo('stylesheet_directory'); ?>/images/world.png" width="258" height="160" alt="Rest of the World" /> <h1 style="color:black!IMPORTANT;">Enter Rest of the World site</h1> </div> </form> 

Write a Wordpress action to process the message. Add this to your theme function file. look at http://php.net/manual/en/function.setcookie.php for setcookie files.

 function set_location_cookie() { if(isset($_POST['location'])) { setcookie('location', $_POST['location'], time()+1209600); } } add_action('init', 'set_location_cookie'); 

Then, anywhere, you want to know the location:

 <?php echo $_COOKIE["location"]; ?> 

ADDITIONAL INFORMATION

First I clicked on the UK and it set the cookie, then I went back and clicked โ€œWorldโ€, but it did not copy the cookies with the world, it showed the UK again. Can cookies be wiped every time a different choice is made?

So, this is a problem with how cookies work at a technical level:

When the browser does this [requests the site], it will look on your computer at the cookie set by [your site]. If it finds a cookie, your browser will send all name-value pairs to the file on [server] along with the URL. If a cookie is not found, it will not send cookie data.

New cookie data is not initially sent, b / c it inst until the request is sent from your browser to the server so that the data of the new cookie is saved and available for sending with the request.

How to do this job? redirect after a successful cookie event.

 function set_location_cookie() { if(isset($_POST['location'])) { // Set Cookie setcookie('location', $_POST['location'], time()+1209600); // Reload the current page so that the cookie is sent with the request header('Location: '.$_SERVER['REQUEST_URI']); } } add_action('init', 'set_location_cookie'); 

Can I also use flag images as submit buttons? Yes, use CSS to style your input buttons.

Add an identifier to each input: id="uk-button" and id="world-button"

CSS

 #uk-button { background-image: url(uk-flag.png); background-size: 100%; background-repeat:no-repeat; } #world-button { background-image: url(world-flag.png); background-size: 100%; background-repeat:no-repeat; } 
+2
source

I would drop the selection into the $_SESSION variable. It will stay with them until they leave. You can also use cookies very well. In fact, a combination of the two would be great, it would connect users who do not allow cookies to be launched on the basis of visits, and people who have cookies will only need to choose one.

Edit: Example working code:

 <form action="<?php the_permalink(); ?>/home" name="region" method="post"> <div id="uk"> <a href="javascript:document.region.submit()" name="UK"> <img style="margin-bottom:10px;" src="<?php bloginfo('stylesheet_directory'); ?>/images/=uk.png" width="259" height="160" alt="UK" /> </a> <h1 style="color:black!IMPORTANT;">Enter United Kingdom site</h1> </div> <div id="world"> <a href="javascript:document.region.submit()" name="World"> <img style="margin-bottom:10px;" src="<?php bloginfo('stylesheet_directory'); ?>/images/world.png" width="258" height="160" alt="Rest of the World" /> </a> <h1 style="color:black!IMPORTANT;">Enter Rest of the World site</h1> </div> </form> // I assume that this form sends a POST request to the following page URL. 

that the form is redirected to:

 <?php session_start(); if(isset($_POST['country'])) // assuming that your form returns a field called 'country'... { $_SESSION['myCountry'] = htmlspecialchars($_POST['country']); // Assumes that myCountry is now 'UK' or 'World'... } header('Location: http://www.example.com/yourIndexPage.php'); // You want to use a quick snippet to process the form and // then redirect them away. This makes for less stupid BACK actions // in the browser as well as data being resent to the code. ?> 

yourIndexpage.php

 <?php // As the $_SESSION is now set, we can use it in the page as follows: session_start(); switch($_SESSION['myCountry']) { case 'UK': echo $UK; // And anything else you want to do with the UK language. break; case 'World': echo $world; // etc etc etc... break; default: // echo out default stuff, probably 'World' at a guess. break; } ?> 

If you are using wordpress, you should probably read this .

+15
source

If you do not want to use cookies, you can do it this way.

 session_start(); if(!isset($_SESSION['language'])){ $_SESSION['language'] = 'English'; //default language } 

then when you have two buttons, where one is English and the other is German or whatever you want.

 <a href="?language=English">English</a> <a href="?language=German">German</a> 

you can use this check to indicate what language the page should be in.

 if(isset($_GET['language'])){ if($_GET['language'] == 'English'){ $_SESSION['language'] = 'English'; } if($_GET['language'] == 'German'){ $_SESSION['language'] = 'German'; } } 
+2
source

The $ _POST variable contains data contained in only one request (see POST (HTTP) ) so that they see the relevant content after "click" or send some form, so after the request only to your page.

To store user preferences more permanently, you need to save these parameters for the "sessions" data, which are available in the $ _SESSION variable and are permanent, therefore they will be available until your visitors look at your pages. A more permanent, but slightly less clean solution is to save the settings for cookies, for which you can determine the validity period, so that they are also used during the next visit.

See an example:

 <?php session_start(); $language = $_POST['language']; $_SESSION['language'] = $language; 

... and in another place:

 <?php session_start(); $language = $_SESSION['language']; 

Or you can use cookies:

 <?php session_start(); $language = $_POST['language']; set_cookie('language', $language, 365 * 24 * 60 * 60); // will live for one year 

... and in another place:

 $language = $_COOKIE['language']; 
+1
source

It will not work on other pages. POST variables are not sent all the time. It only submits when the form is submitted. You can use cookies for your task. Save the variable there and check it on each page.

0
source

You really need sessions / cookies if you want the selection to be saved (or keep passing the value via GET like ?lang=uk , but it's not very elegant). By the way, you do not need an image in a hyperlink, by which the behavior is blocked by default, and its parent form is submitted using JavaScript. You could just use something like this:

 <input alt="en-UK" name="lang" src="uk-flag.png" type="image" value="en-UK"> 

This will work as an image, which is also a form submit button.

0
source

Source: https://habr.com/ru/post/922049/


All Articles