Skip to content

Instantly share code, notes, and snippets.

@jrobinsonc
Last active May 20, 2020 19:21
Show Gist options
  • Save jrobinsonc/31054f6ec9b1227d60ea0083bf0a2287 to your computer and use it in GitHub Desktop.
Save jrobinsonc/31054f6ec9b1227d60ea0083bf0a2287 to your computer and use it in GitHub Desktop.
WPML WordPress plugin

WPML WordPress plugin

Links

WPML Coding API
http://wpml.org/documentation/support/wpml-coding-api/

Coding reference

SNIPPET COMMENTS
icl_register_string(context, name, value) Registers a string for translation.
icl_unregister_string(context, name) Removes a string from the translation table.
icl_t(context, name, value) Gets the translated value of a string.
icl_makes_duplicates($master_post_id) Duplicate the post $master_post_id to all the site’s languages.
icl_get_home_url() Link to the home page in the active language.
icl_object_id(ID, type, return_original_if_missing, language_code) Used to calculate the IDs of objects (usually categories) in the current language.
ICL_LANGUAGE_CODE Code for the current language (example: fr)
define('ICL_DONT_LOAD_LANGUAGE_SELECTOR_CSS', true); Remove language selector css for front-end.

Snippets

Custom language switcher:

/**
 * get_language_links
 *
 * @see https://wpml.org/documentation/getting-started-guide/language-setup/custom-language-switcher/
 * @return array Returns an array with the links for each available language.
 */
function get_language_links( $link_format = '<a href="%1$s">%2$s</a>' ) {
	if ( ! function_exists( 'icl_get_languages' ) ) {
		trigger_error(
			'The plugin WPML is not installed.',
			E_USER_ERROR
		);
		return array();
	}

	$languages = icl_get_languages( 'skip_missing=N&orderby=KEY&order=DIR&link_empty_to=str' );
	$links     = array();

	foreach ( $languages as $lang ) {
		$links[] = sprintf( $link_format, $lang['url'], $lang['native_name'] );
	}

	return $links;
}

Redirects users from splash-intro page to the default language of your site:

/**
 * Redirects users from splash-intro page to the default language of your site
 * Must configure the ID.
 */
add_action('wp', function () {
    if (!function_exists('icl_get_home_url')) {
        return;
    }

    $introPageId = 0; // ID of your intro page.

    if (get_the_ID() === $introPageId) {
        wp_redirect(icl_get_home_url());
        exit;
    }
});
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment