Menu url to tempalte and WPML

Just thought I would share with the community on the issue I was having referencing Bricks Templates from the main menu. In my case I have a custom post type “dashboards” which is translated differently into other languages.

The issue is that there is no support to reference Bricks Templates as a dynamic target URL so I had to use Custom URL and type /dashboards/ on all the buttons, etc. I needed to reference to the archive page of the custom post.

When I translated the pages, WPML would keep it but future translations would wipe out the URL.

So instead I use this humongous function

function get_translated_url($slug = '', $post_type = 'bricks_template') {
    if (empty($slug)) {
        return '';
    }

    // Get the original template by slug
    $original = get_page_by_path($slug, OBJECT, $post_type);
    if (!$original) {
        return '';
    }

    // Get the translated post ID
    $translated_id = apply_filters('wpml_object_id', $original->ID, $post_type, true, ICL_LANGUAGE_CODE);
    if (!$translated_id) {
        return '';
    }

    $translated_post = get_post($translated_id);
    if (!$translated_post) {
        return '';
    }

    // Get translated slug
    $translated_slug = $translated_post->post_name;

    // Build URL with language prefix
    $lang_prefix = (ICL_LANGUAGE_CODE !== 'en') ? '/' . ICL_LANGUAGE_CODE : '';

    return home_url($lang_prefix . '/' . $translated_slug . '/');
}

I white list it in for dynamic use in Bricks (echo) and then instead of CUstom URL I use Dynamic {echo:get_translated_url(‘dashboards’)}. - if in EN it gives me /dashboards if in FR it gives me /fr/tableaux-de-bord and so forth.

Maybe too specific for my custom post type/WPML use case but maybe it can help someone.