Home / Widgets / [Condition] TranslatePress in Breakdance Builder | WordPress
Duplicate Snippet

Embed Snippet on Your Site

[Condition] TranslatePress in Breakdance Builder | WordPress

[Condition] TranslatePress in Breakdance Builder | WordPress

Code Preview
php
<?php
add_action("breakdance_register_template_types_and_conditions", function () {
    if (function_exists("trp_get_languages")) {
        $lang_list_full = trp_get_languages();
        $lang_list = array();
        foreach($lang_list_full as $key => $value) {
            $lang_list[] = $key;
        }
        \Breakdance\ConditionsAPI\register([
            "supports" => ["element_display", "templating"],
            "slug" => "TranslatePress4bd-condition", // MUST BE UNIQUE
            "label" => "Language",
            "category" => "TranslatePress",
            "operands" => ["equals", "not equals"],
            "values" => function () use ($lang_list) {
                return [
                    [
                        "label" => "Language",
                        "items" => array_map(function ($lang) {
                            return [
                                "text" => $lang,
                                "value" => $lang,
                            ];
                        }, $lang_list),
                    ],
                ];
            },
            "allowMultiselect" => false,
            "callback" => function (string $operand, $value) {
                // Get the current language from TranslatePress
                $current_lang = get_locale();
                if ($operand === "equals") {
                    return $current_lang === $value;
                }
                if ($operand === "not equals") {
                    return $current_lang !== $value;
                }
                return false;
            },
        ]);
    }
});

Comments

Add a Comment