TranslatePress Condition for Breakdance

Tips, Tutorials

Contents

On multilingual websites, there are often use cases where certain elements should be displayed or hidden depending on the language. Depending on the plugin, there are different ways to achieve this. This article deals explicitly with the implementation of a TranslatePress condition for Breakdance.

Display depending on the URL

One way to achieve this with the on-board tools of Breakdance is to use conditions to retrieve the URL and check whether a certain language code exists in the string. For example, you could check whether the string "/en/" is in the URL and show or hide the element depending on this.

Use TranslatePress shortcodes

TranslatePress also offers shortcodes that can be used for this purpose. To do this, insert all content that is to be shown/hidden into the shortcode and adjust the corresponding parameters.

[trp_language language="fi"] English content only [/trp_language]

Custom TranslatePress Breakdance Condition

The most elegant way to achieve your goal is to add a custom condition for TranslatePress to Breakdance. To do this, you can add the following PHP code to your installation.

After you have inserted this code, the custom condition "TranslatePress" appears in the Breakdance Conditions. All languages that you have set up are displayed here. As with the other Breakdance conditions, you can apply the condition to elements here. The corresponding elements are then displayed or hidden depending on the selected language.

<?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;
            },
        ]);
    }
});
Share
Contact now

Let's finish your website.

What we guarantee

You will only receive an invoice if we have been able to help!

Our team works hard every day to solve problems quickly for our customers or to achieve optimal results during implementation. As a result, bedemy is now known for its first-class service and has many happy customers. Our past experience and belief in our abilities enables us to offer new customers a unique promise that underlines our commitment to customer satisfaction.

We only issue an invoice if we have really been able to help!

If we are unable to help with a problem or implementation, we will not issue an invoice. Even if we have already invested time. You have no risk, because even in the event that we have implemented everything as requested, our costs are transparent, agreed in advance and never exceed the planned budget!

What our help costs

No nasty surprises

We charge according to a fixed hourly rate, which we will inform you of in a written offer before we charge you for the use of our team. This means that implementing a small feature or fixing a problem is usually quick (often within a few minutes) and therefore doesn't cost much. The implementation of a complete site, on the other hand, can take a few days (depending on the scope).

But you don't have to worry about the costs.

Firstly, you will always receive a quote from us in advance via email, even for smaller support requests. Secondly, we always stick to this cost estimate unless we tell you otherwise. If, during implementation, we realize that the task assigned to us is more complex and will take longer, we will contact you with a revised cost estimate. You can then decide whether you are prepared to accept this revised offer or not. If it's too much for you, we'll stop the project and you won't pay a penny. Not even for the work we have already invested in the project up to that point (according to the initial estimate).