Skip to content

Instantly share code, notes, and snippets.

@mcaskill
Created April 16, 2015 18:47
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save mcaskill/2429e7e8c1f5dac62759 to your computer and use it in GitHub Desktop.
Save mcaskill/2429e7e8c1f5dac62759 to your computer and use it in GitHub Desktop.
WordPress \ WPML : Various utility functions I made but don't remember why or if they work.
<?php
/**
* WPML Utility Functions
*/
if ( ! function_exists('icl_get_object') )
{
/**
* Retrieve an associated translated object from another object's ID.
*
* @uses $wpdb
*
* @param int $element_id The ID of the post, page, tag or category.
* @param string $element_type Optional. The object type. Either a post type or a taxonomy.
* @param string $ulanguage_code If set, forces the language of the returned object and can
* be different than the displayed language.
* @param bool $return_original `true` if WPML should return the ID of the original
* language element if the translation is missing or
* `false` if WPML should return a NULL if translation
* is missing.
* @return mixed Returns a `WP_Post` object, `false` if WPML isn't present,
* `null` if the post doesnt exist, or an error occurred.
*/
function icl_get_object ( $element_id, $element_type = 'post', $ulanguage_code = null, $return_original = false )
{
if ( class_exists('SitePress') ) {
# if ( ICL_LANGUAGE_CODE !== ICL_DEFAULT_LANGUAGE ) {
$translated_id = icl_object_id( $element_id, $element_type, $return_original, $ulanguage_code );
if ( (int) $translated_id > 0 ) {
return get_post( (int) $translated_id );
}
# }
}
return false;
}
}
if ( ! function_exists('icl_get_translation') ) {
/**
* Retrieve an associated translated object from another object's ID.
*
* @uses $wpdb
*
* @param int $element_id The ID of the post, page, tag or category
* @return mixed Returns a `WP_Post` object, `false` if WPML isn't present,
* `null` if the post doesnt exist, or an error occurred.
*/
function icl_get_translation( $element_id )
{
global $wpdb;
if ( ICL_LANGUAGE_CODE == ICL_DEFAULT_LANGUAGE ) {
$querystr = "SELECT element_id as tid FROM {$wpdb->prefix}icl_translations WHERE `trid` = '$element_id' AND source_language_code = '".ICL_DEFAULT_LANGUAGE."' LIMIT 1";
}
else {
$querystr = "SELECT trid as tid FROM {$wpdb->prefix}icl_translations WHERE `element_id` = '$element_id' AND source_language_code = '".ICL_DEFAULT_LANGUAGE."' LIMIT 1";
}
$translation = $wpdb->get_results($querystr, OBJECT);
if ( ! empty( $translation[0] ) ) {
$post_id = intval($translation[0]->tid);
$translation = get_post($post_id);
return $translation;
}
return false;
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment