Wordpress: Use method="post" for multiple language selection Wordpress: Use method="post" for multiple language selection wordpress wordpress

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


I would drop the selection into a $_SESSION variable. That will stay with them until they leave. You could also use a cookie quite nicely. Actually a combination of the two would be great, it ties up users who don't allow cookies to run on a visit to visit basis, and folks who have cookies enables will only have to pick once.

Edit: Example of 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.

page that the form redirects 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.


If you don't want to use cookies you could do it this way.

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

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

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

you could use this check to varify what language the page should be.

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


Answers

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

Secondly, You cannot use $_SESSION by default. WP is stateless by design. That said there are tons of plugins and tutorials online for getting this functionality.

Code stuff

Your original form html had no inputs. It was a form that submitted nothing. This form has two submits named the same thing location. So whichever button is clicked will submit its value against the $_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 handle the post. Add this to your theme's functions file.look at http://php.net/manual/en/function.setcookie.php for setcookie docs.

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 INFO

I initially clicked UK to start with and this set the cookie, I then went back and clicked World but that didn't copy over the cookie with world, it just showed UK again. Is it possible to wipe over the cookie each time a different selection is made?

So this is an issue regarding how cookies work at a technical level:

When the browser does this [requests a site], it will look on your machine for a cookie file that [your site] has set. If it finds a cookie file, your browser will send all of the name-value pairs in the file to [the] server along with the URL. If it finds no cookie file, it will send no cookie data.

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

How do you make this work then?redirect after a successful set 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');

Also is it possible to use the images of the flags as the submit buttons?Yes, use CSS to style your input buttons.

Add an id 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;}