Skip to content

Instantly share code, notes, and snippets.

@absowoot
Last active March 13, 2023 15:22
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 absowoot/a043ed04e5676231c9bff3dde2dcb7b3 to your computer and use it in GitHub Desktop.
Save absowoot/a043ed04e5676231c9bff3dde2dcb7b3 to your computer and use it in GitHub Desktop.
Bricks WPML Conditions
<?php
add_filter('bricks/dynamic_tags_list', function ($tags) {
$tags[] = array(
'name' => '{wpml_language_slug}',
'label' => __('WPML Language Slug', 'textdomain'),
'group' => 'site',
);
return $tags;
});
add_filter('builder/settings/template/controls_data', function ($data) {
$data['controls']['templateConditions']['fields']['main']['options']['wpml_language'] = __('WPML Language', 'textdomain');
$data['controls']['templateConditions']['fields']['language'] = array(
'type' => 'select',
'options' => array( // Hardcoded for now
'en' => __('English', 'textdomain'),
'ja' => __('Japanese', 'textdomain'),
),
'placeholder' => __('Select', 'textdomain'),
'required' => array(
'main',
'=',
'wpml_language',
),
);
return $data;
}, 1);
add_filter('bricks/screen_conditions/scores', function ($scores, $condition, $post_id, $preview_type) {
if ($condition['main'] === 'wpml_language' && isset($condition['language'])) {
$current_lang = apply_filters('wpml_current_language', null);
if ($current_lang === $condition['language']) {
$scores[] = 10;
}
}
return $scores;
}, 10, 4);
add_filter('bricks/dynamic_data/render_content', function($content, $post, $context = 'text') {
if (strpos($content, '{wpml_language_slug}') !== false) {
$content = apply_filters('wpml_current_language', null);
}
return $content;
}, 10, 3);
?>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment