mardi 28 juin 2016

WordPress+WPML: Creating a multi-country website with fallback languages

Good evening!

This is more of a general question of how to approach a problem, rather than asking for specific code (but of course, anything is welcome that could help out!).

I'm developing a site where the intro is what volvocars.com is: a portal where you can select a country and in most cases it's the native tongue of that country, but it usually fallbacks to English if there's no language support.

I'm aiming to build something similar, where WPML is thought of to be the base, handling the languages (right now, around 6-7). There are some requirements though:

  • I don't want to use WPML's permalink structure. Instead I want to build my own, to achieve URL's like http://ift.tt/290U86W if we're entering the Moroccan version of the contact page on the website. If no Moroccan version is available (controlled by WPML), then fallback to what ever language is specified for that country (controlled through a custom option setting with ACF).
  • The languages should be managed in WPML, but the countries are being added on an option page I've created through ACF (see code down below).
  • I want to be able to change some other content based the country chosen.

Is there an easy way to do this? Should I go with rewriting the permalinks through WordPress? Or should I change something in .htaccess?

class ACF_populate_language {

    /**
     * Initialize the class
     */
    public function __construct() {

        // Make an array of the field ID's for the different parts of the world (defined in the ACF settings)
        $fields = array(
            '5756c03647974', // Asia & The Pacific
            '5756c36c54753', // Europe
            '5756c38754757', // North & South America
            '5756c3db5475b', // Middle East & Africa
        );

        foreach ($fields as $f) {
            add_filter( 'acf/load_field/key=field_' . $f, array( $this, 'acf_populate_language' ) );
        }
    }

    /**
     * Populate the input checkboxes in "Settings for countries" with languages from WPML
     *
     * @since  1.0.0
     */
    public function acf_populate_language( $field ) {

        $field['choices'] = array();

        // Get all the WPML languages, ordered by ID
        $languages = apply_filters( 'wpml_active_languages', NULL, 'orderby=id&order=desc' );

        if ( !empty( $languages ) ) {

            foreach( $languages as $l ) {

                $field['choices'][ $l['language_code'] ] = $l['translated_name'];

            }
        }

        return $field;

    }
}



via Chebli Mohamed

Aucun commentaire:

Enregistrer un commentaire