/**
* Get theme option using WP Rapid Fields framework.
*
* @param string $option_name
* @param mixed $default
* @return mixed
*/
function ws_get_option($option_name, $default = '')
{
$lang = function_exists('pll_current_language') ? pll_current_language('slug') : '';
$default_lang = function_exists('pll_default_language') ? pll_default_language('slug') : '';
if ($lang && $lang === $default_lang) {
// Default language: Try base options first, then suffixed options
$options = get_option('tqb_options_opts', []);
if (is_array($options) && isset($options[$option_name]) && $options[$option_name] !== '') {
return $options[$option_name];
}
$options = get_option('tqb_options_opts_' . $lang, []);
if (is_array($options) && isset($options[$option_name]) && $options[$option_name] !== '') {
return $options[$option_name];
}
}
elseif ($lang) {
// Non-default language: Try suffixed options first, then base options
$options = get_option('tqb_options_opts_' . $lang, []);
if (is_array($options) && isset($options[$option_name]) && $options[$option_name] !== '') {
return $options[$option_name];
}
$options = get_option('tqb_options_opts', []);
if (is_array($options) && isset($options[$option_name]) && $options[$option_name] !== '') {
return $options[$option_name];
}
}
else {
// No Polylang: use base options
$options = get_option('tqb_options_opts', []);
if (is_array($options) && isset($options[$option_name]) && $options[$option_name] !== '') {
return $options[$option_name];
}
}
return $default;
}
/**
* Get Polylang-aware post meta value.
* Tries language-specific meta key first, falls back to base key.
*
* @param int $post_id Post ID
* @param string $meta_key Base meta key (without language suffix)
* @param bool $single Whether to return a single value
* @return mixed
*/
function ws_get_post_meta($post_id, $meta_key, $single = true)
{
$lang = function_exists('pll_current_language') ? pll_current_language('slug') : '';
$default_lang = function_exists('pll_default_language') ? pll_default_language('slug') : '';
if ($lang && $lang === $default_lang) {
// Default language: Try base meta first, then suffixed meta
$val = get_post_meta($post_id, $meta_key, $single);
if (!empty($val)) {
return $val;
}
$lang_key = $meta_key . '_' . $lang;
if (metadata_exists('post', $post_id, $lang_key)) {
$val = get_post_meta($post_id, $lang_key, $single);
if (!empty($val)) {
return $val;
}
}
}
elseif ($lang) {
// Non-default language: Try suffixed meta first, then base meta
$lang_key = $meta_key . '_' . $lang;
if (metadata_exists('post', $post_id, $lang_key)) {
$val = get_post_meta($post_id, $lang_key, $single);
if (!empty($val)) {
return $val;
}
}
return get_post_meta($post_id, $meta_key, $single);
}
return get_post_meta($post_id, $meta_key, $single);
}
/**
* Get Polylang-aware term meta value.
* Tries language-specific meta key first, falls back to base key.
*
* @param int $term_id Term ID
* @param string $meta_key Base meta key (without language suffix)
* @param bool $single Whether to return a single value
* @return mixed
*/
function ws_get_term_meta($term_id, $meta_key, $single = true)
{
$lang = function_exists('pll_current_language') ? pll_current_language('slug') : '';
$default_lang = function_exists('pll_default_language') ? pll_default_language('slug') : '';
if ($lang && $lang === $default_lang) {
// Default language: Try base meta first, then suffixed meta
$val = get_term_meta($term_id, $meta_key, $single);
if (!empty($val)) {
return $val;
}
$lang_key = $meta_key . '_' . $lang;
if (metadata_exists('term', $term_id, $lang_key)) {
$val = get_term_meta($term_id, $lang_key, $single);
if (!empty($val)) {
return $val;
}
}
}
elseif ($lang) {
// Non-default language: Try suffixed meta first, then base meta
$lang_key = $meta_key . '_' . $lang;
if (metadata_exists('term', $term_id, $lang_key)) {
$val = get_term_meta($term_id, $lang_key, $single);
if (!empty($val)) {
return $val;
}
}
return get_term_meta($term_id, $meta_key, $single);
}
return get_term_meta($term_id, $meta_key, $single);
}
/**
* Get Polylang-aware user meta value.
* Tries language-specific meta key first, falls back to base key.
*
* @param int $user_id User ID
* @param string $meta_key Base meta key (without language suffix)
* @param bool $single Whether to return a single value
* @return mixed
*/
function ws_get_user_meta($user_id, $meta_key, $single = true)
{
$lang = function_exists('pll_current_language') ? pll_current_language('slug') : '';
$default_lang = function_exists('pll_default_language') ? pll_default_language('slug') : '';
if ($lang && $lang === $default_lang) {
// Default language: Try base meta first, then suffixed meta
$val = get_user_meta($user_id, $meta_key, $single);
if (!empty($val)) {
return $val;
}
$lang_key = $meta_key . '_' . $lang;
if (metadata_exists('user', $user_id, $lang_key)) {
$val = get_user_meta($user_id, $lang_key, $single);
if (!empty($val)) {
return $val;
}
}
}
elseif ($lang) {
// Non-default language: Try suffixed meta first, then base meta
$lang_key = $meta_key . '_' . $lang;
if (metadata_exists('user', $user_id, $lang_key)) {
$val = get_user_meta($user_id, $lang_key, $single);
if (!empty($val)) {
return $val;
}
}
return get_user_meta($user_id, $meta_key, $single);
}
return get_user_meta($user_id, $meta_key, $single);
}