WPML Cross Domain 問題,導致 Elementor 打不開

如果是子網域的話,可以自己同步。

但如果是不同網域的話,不太能同步。但至少可以做一些接軌的動作

首先 wp-config.php

if ( isset( $_SERVER['HTTP_HOST'] ) ) {
define( 'WP_HOME', 'https://' . $_SERVER['HTTP_HOST'] );
define( 'WP_SITEURL', 'https://' . $_SERVER['HTTP_HOST'] );
}

另外編輯 Elementor 的連結做個調整

add_filter( 'elementor/editor/url', 'redirect_elementor_to_correct_domain' );

function redirect_elementor_to_correct_domain( $editor_url ) {
    // 獲取當前頁面的 ID
    $post_id = get_the_ID();
    if ( ! $post_id ) {
        return $editor_url;
    }

    // 獲取當前頁面在所有語言中的翻譯資訊
    $translations = apply_filters( 'wpml_post_translations', array(), $post_id );

    // 獲取當前語言代碼
    $current_language = apply_filters( 'wpml_current_language', null );

    // 如果沒有翻譯資訊或當前語言,則直接返回
    if ( empty( $translations ) || ! $current_language ) {
        return $editor_url;
    }

    // 找到當前語言的對應頁面 ID
    $translated_post_id = $translations[ $current_language ];

    // 取得該頁面的原始編輯 URL
    $original_edit_url = get_edit_post_link( $translated_post_id, '' );

    // 從原始編輯 URL 擷取網域部分
    $parsed_original_url = parse_url( $original_edit_url );
    if ( ! isset( $parsed_original_url['host'] ) ) {
        return $editor_url;
    }
    $correct_host = $parsed_original_url['host'];

    // 檢查目前後台的網域是否與正確的網域不同
    $current_host = $_SERVER['HTTP_HOST'];
    if ( $current_host !== $correct_host ) {
        // 如果不同,則重新構建新的編輯 URL
        $parsed_editor_url = parse_url( $editor_url );
        $new_editor_url = str_replace( $current_host, $correct_host, $editor_url );

        // 確保新網址是有效的
        if ( filter_var($new_editor_url, FILTER_VALIDATE_URL) ) {
            return $new_editor_url;
        }
    }

    return $editor_url;
}

參考文件:https://wpml.org/forums/topic/cross-domain/