Conditionally Disabling Weglot Translation for Specific URLs

When using Weglot on your website, you might occasionally encounter issues where its translation processes interfere with certain functionalities, like generating PDFs or handling custom endpoints. In

Suppose you’re facing an issue where generating PDFs is not working correctly due to Weglot's translation being applied to those URLs. In this case, instead of disabling the entire Weglot plugin, you can selectively disable its translation process for specific URLs.

add_action('init', 'conditionally_apply_weglot_translation');
function conditionally_apply_weglot_translation() {
    $current_url = $_SERVER['REQUEST_URI'];
    // Check if the current URL contains 'front-end'
    if (strpos($current_url, 'front-end') !== false) {
        // Disable Weglot translation for this specific request
        add_filter('weglot_active_translation_before_process', function() {
            return false;
        });
    }
}

Why Use This Approach?

  • Avoid full deactivation: Instead of disabling the entire Weglot plugin (which affects your whole site), this solution allows you to keep Weglot active while bypassing translation only for specific scenarios.

  • Resolve conflicts efficiently: For example, if generating PDFs or handling specific routes fails due to translations, disabling Weglot only for those URLs resolves the issue without affecting the rest of your website.

Last updated