You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

Laravel+WordPress架构下,如何外部触发Polylang插件语言切换?

Hey there! I’ve run into similar Laravel-WordPress integration headaches before, so let’s walk through practical solutions to trigger Polylang’s language switch without loading WordPress core into your Laravel app (and avoiding those nasty naming conflicts).


Solution 1: Use WordPress REST API (Cleanest Approach)

The best way to keep your Laravel and WordPress environments isolated is to create a custom WordPress REST endpoint that handles the Polylang language switch. Your Laravel frontend will send a simple HTTP request to this endpoint, no need to load wp-load.php directly.

Step 1: Add the REST Route to WordPress

Add this code to your WordPress theme’s functions.php or a custom plugin:

add_action('rest_api_init', function () {
    register_rest_route(
        'polylang-switch/v1',
        '/set-language/(?P<lang>[a-z]{2})',
        [
            'methods' => 'POST',
            'callback' => 'pll_switch_language_via_api',
            'permission_callback' => '__return_true', // Adjust permissions if needed (e.g., restrict to logged-in users)
        ]
    );
});

function pll_switch_language_via_api($request) {
    $lang = $request->get_param('lang');
    
    // Validate the language is supported by Polylang
    $valid_langs = pll_languages_list(['fields' => 'slug']);
    if (!in_array($lang, $valid_langs)) {
        return new WP_Error('invalid_lang', 'Invalid language code', ['status' => 400]);
    }
    
    // Trigger Polylang's language switch
    pll_set_language($lang);
    
    // Return success response (you can use this to sync Laravel's state if needed)
    return rest_ensure_response([
        'success' => true,
        'current_lang' => pll_current_language()
    ]);
}

Step 2: Update Laravel’s Frontend Logic

Modify your existing language switch JS to send a request to the WordPress REST endpoint after updating Laravel’s session:

function switchLanguage(lang) {
    // First, update Laravel's session (your existing logic)
    fetch('/your-laravel-lang-switch-endpoint', {
        method: 'POST',
        headers: {
            'X-CSRF-TOKEN': document.querySelector('meta[name="csrf-token"]').content,
            'Content-Type': 'application/json'
        },
        body: JSON.stringify({ lang: lang })
    })
    .then(() => {
        // Then trigger Polylang's switch via the REST endpoint
        fetch('https://your-wp-domain.com/wp-json/polylang-switch/v1/set-language/' + lang, {
            method: 'POST',
            credentials: 'include' // Ensures WordPress receives the user's session cookie
        })
        .then(response => response.json())
        .then(data => {
            if (data.success) {
                location.reload(); // Refresh to load translated WP content
            }
        });
    });
}

Polylang uses a cookie (pll_language) to track the active language. You can set this cookie directly from Laravel when switching languages, so WordPress will automatically pick up the correct language on page load.

use Illuminate\Http\Request;
use Illuminate\Support\Facades\Cookie;

public function switchLanguage(Request $request) {
    $lang = $request->input('lang');
    
    // Match valid languages to your Polylang slugs (e.g., 'en', 'zh')
    $supported_langs = ['en', 'zh'];
    if (!in_array($lang, $supported_langs)) {
        return back()->withErrors('Invalid language');
    }
    
    // Update Laravel's session
    session()->put('locale', $lang);
    
    // Set Polylang's cookie (match domain/path to your WordPress setup)
    Cookie::queue(
        'pll_language',
        $lang,
        60*24*30, // 30-day expiry
        '/',
        '.your-main-domain.com', // Use your root domain if Laravel/WP share it
        false, // Set to true if using HTTPS
        true // Mark as HttpOnly for security
    );
    
    return back();
}

Note: Double-check Polylang’s existing cookie settings (via browser dev tools) to match the domain, path, and security flags exactly—this ensures WordPress reads the cookie correctly.


Solution 3: WordPress AJAX Endpoint (Legacy Alternative)

If you prefer using WordPress’s classic AJAX system instead of REST API, this works too:

Step 1: Add AJAX Handler to WordPress

add_action('wp_ajax_pll_switch_language', 'pll_switch_language_via_ajax');
add_action('wp_ajax_nopriv_pll_switch_language', 'pll_switch_language_via_ajax'); // Allow unauthenticated users

function pll_switch_language_via_ajax() {
    if (!isset($_POST['lang'])) {
        wp_send_json_error('Missing language parameter');
    }
    
    $lang = sanitize_text_field($_POST['lang']);
    $valid_langs = pll_languages_list(['fields' => 'slug']);
    
    if (!in_array($lang, $valid_langs)) {
        wp_send_json_error('Invalid language code');
    }
    
    pll_set_language($lang);
    wp_send_json_success(['current_lang' => pll_current_language()]);
}

Step 2: Update Laravel’s JS to Call the AJAX Endpoint

function switchLanguage(lang) {
    // Update Laravel session first
    fetch('/your-laravel-lang-switch-endpoint', {
        method: 'POST',
        headers: {
            'X-CSRF-TOKEN': document.querySelector('meta[name="csrf-token"]').content,
            'Content-Type': 'application/json'
        },
        body: JSON.stringify({ lang: lang })
    })
    .then(() => {
        // Call WordPress AJAX endpoint
        fetch('https://your-wp-domain.com/wp-admin/admin-ajax.php', {
            method: 'POST',
            credentials: 'include',
            body: new URLSearchParams({
                action: 'pll_switch_language',
                lang: lang
            })
        })
        .then(response => response.json())
        .then(data => {
            if (data.success) {
                location.reload();
            }
        });
    });
}

Key Notes to Avoid Issues

  • Session Isolation: Make sure Laravel and WordPress use different session cookie names to prevent conflicts.
  • CORS Configuration: If Laravel and WordPress are on different domains, set up CORS in WordPress to allow requests from your Laravel domain.
  • Security: Always validate language parameters to prevent malicious requests. Adjust permission callbacks in REST/AJAX code if you need to restrict access to logged-in users only.

内容的提问来源于stack exchange,提问作者ikebastuz

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.05.15 06:36:34