Support » Plugins » Hacks » serve another language from custom field

  • Hi,

    I want to get rid of the language switcher plugin that I’ve been using for years now and serve an alternate language (content and title) from a custom field. I found this code on stackoverflow but was wondering how to set up the “front end query” to serve this alternate content.

    add_filter( 'the_title', 'english_title', 10, 2 );
    function english_title( $title, $post_id )
        if( $new_title = get_post_meta( $post_id, 'custom_field_name', true ) )
            return $new_title;
        return $title;
    add_filter( 'the_content', 'english_content', 6);
    function english_content($content,$post_id) {
       if( $english_text = get_post_meta( $post_id, 'custom_field_name', true ) )
            return $english_text;
        return $content;

    I would verry much like to use custom post urls instead of cookies but I don’t know how.
    Users must click on a flag or text in the menu to trigger the alternate text. Does anyone know a way to acheave this?

    Thanks a lot in advance!

    Kindly, M van Dijk

Viewing 5 replies - 1 through 5 (of 5 total)
  • Moderator bcworkz


    I’m sorry, I really doubt this code will work for your needs. First off, the English title and content are coming from the same meta value. This is easy to fix, but the larger problem is the user preference is not considered. If the English variant is available, it is used, no matter what.

    There’s a few general approaches to multilingual blogs. Take a look at Multilingual WordPress.

    Thanks bcworkz,

    I am aware of the things I have to fix in my code above, it’s just a draft.

    What I’m looking for is a way to set up the front end part, let the user choose the language. I think that this must be done with a cookie after all but I’m not quite sure how.

    I found some snippets about setting and retrieving cookies from the functions.php file:

    add_action('init', function() {
        if (!isset($_COOKIE['my_cookie'])) {
            setcookie('my_cookie', 'some default value', strtotime('+1 day'));

    retrieving cookie:

    $cookieValue = $_COOKIE['cookie_name'];

    Unsetting a cookie requires setting one with an expiration date in the past, something like:

    setcookie('cookie_name', null, strtotime('-1 day'));

    (cookie name still has to be set right!)
    Now I have to make this “set cookie action” conditional to a link click. How do I do that?
    And secondly, how do I check the cookie before the custom field (code of post above) is triggered.

    Any help apreciated!

    Moderator bcworkz


    Yes, saving the user preference in a cookie is a good option because it will persist between visits. Another alternative is save the preference in user meta. There’s a few options for setting the cookie when the language preference is clicked, I think in the cookie case a javascript solution may be the best because it is set immediately and thus available for the next page request. Capture the onclick action on whatever text and/or flag graphic represents the option and store the language preference in a cookie. Then if the language differs from the current one, the script should reload the page so the proper language can be shown.

    Now, to check the cookie for a page request to determine which language to serve, you grab the value out of the $_COOKIE array somewhere before any content is sent to the client, the theme’s header.php template should work well. You could save the value in your own global, which seems redundant since the $_COOKIE value is available. I tend to use my own global anyway because I don’t trust how long the $_COOKIE array will have the correct value.

    Then any time you serve content that is available in different languages, such as in “the loop”, you serve one language or the other based on this value, in a process vaguely similar to your initial code example.

    Incidentally, if you would like the WP interface to be a different language as well, first of all, you’ll need the proper language files installed with your WP installation. Do NOT change the WPLANG variable (you can’t do so on the fly anyway). Also do not set_locale() or other such techniques because it will apply to ALL current threads of all visitors. To change WP interface language of the current thread, hook the ‘locale’ filter and return the desired language code that would normally be used when setting WPLANG.

    Hi bcworkz,

    Thanks for the help!
    I was first trying to go with GET requests but think your argument for a consistent user experience with cookies ways out the advantages of this other method. So I’m going with cookies.
    It might take me a wile but i’ll keep you updated!

    Hi, Still havn’t finished this code but I found a thread on the Advanced Custom Fields (ACF) forum about this:

    Anyone who knows how to solve this?

Viewing 5 replies - 1 through 5 (of 5 total)
  • The topic ‘serve another language from custom field’ is closed to new replies.