Skip to content

Instantly share code, notes, and snippets.

@JoryHogeveen
Last active April 15, 2025 17:04
Show Gist options
  • Save JoryHogeveen/1a9f41406f2e1f1b542d725a1954f774 to your computer and use it in GitHub Desktop.
Save JoryHogeveen/1a9f41406f2e1f1b542d725a1954f774 to your computer and use it in GitHub Desktop.
WP MU Plugin: Polylang multidomain Elementor assets URL
<?php
/**
* MU plugin. Need to be used together with regular plugin.
*
* Plugin Name: Polylang Elementor Cross Domain Assets URL
* Description: Fixes cross origin domain issues with Elementor and Polylang
* Version: 1.0
* Author: Jory Hogeveen
* Author URI: http://www.keraweb.nl/
*/
add_filter( 'plugins_url', function ( $url ) {
if ( false === strpos( $url, 'elementor' ) ) {
return $url;
}
$pll_options = get_option( 'polylang' );
if ( ! isset( $pll_options['domains'] ) ) {
// Not a multidomain configuration.
return $url;
}
$domains = $pll_options['domains'];
$host = $_SERVER['HTTP_HOST'];
foreach ( $domains as $domain ) {
if ( false !== strpos( $domain, $host ) ) {
$url_parts = parse_url( $url );
$url = str_replace( $url_parts['host'], $host, $url );
break;
}
}
return $url;
} );
<?php
/*
* Non-MU plugin. Need to be used together with MU plugin.
*
* Based on code from @nicmare: https://github.com/polylang/polylang/issues/590#issuecomment-721782112
*
* Plugin Name: Polylang Elementor Cross Domain Assets
* Description: Fixes cross origin domain issues with Elementor and Polylang
* Version: 1.0
* Author: Jory Hogeveen
* Author URI: http://www.keraweb.nl/
*/
Polylang_Elementor_Assets::get_instance();
class Polylang_Elementor_Assets
{
private static $_instance = null;
protected $current_domain = '';
protected $default_domain = '';
protected $current_language = '';
protected $default_language = '';
protected $all_domains = array();
protected function __construct() {
add_action( 'init', array( $this, 'init' ) );
}
public function init() {
if ( ! function_exists( 'pll_current_language' ) ) {
return;
}
$return = OBJECT;
$current_language = pll_current_language( $return );
$default_language = pll_default_language( $return );
if ( ! $current_language || ! $default_language ) {
return;
}
$is_preview = isset( $_GET['elementor_preview'] );
$is_editor = ( isset( $_GET['action'] ) && 'elementor' === $_GET['action'] );
if ( ! $is_editor && ! $is_preview ) {
return;
}
$languages = pll_the_languages( array( 'raw' => true ) );
foreach ( $languages as $language ) {
$this->all_domains[] = $language['url'];
if ( false !== stripos( $language['url'], $_SERVER['SERVER_NAME'] ) ) {
$current_language = PLL()->model->get_language( $language['slug'] );
break;
}
}
$this->current_domain = $current_language->home_url;
$this->default_domain = $default_language->home_url;
$this->current_language = $current_language->slug;
$this->default_language = $default_language->slug;
add_filter( 'script_loader_src', array( $this, 'translate_url' ) );
add_filter( 'style_loader_src', array( $this, 'translate_url' ) );
add_filter( 'allowed_http_origins', array( $this, 'add_allowed_origins' ) );
add_filter( 'wp_get_attachment_image_attributes', array( $this, 'check_for_src' ),10,2 );
add_filter( 'admin_url', array( $this, 'modify_adminy_url_for_ajax' ), 10, 3 );
add_filter( 'post_row_actions', array( $this, 'elementor_links_fix' ), 12, 2 );
add_filter( 'page_row_actions', array( $this, 'elementor_links_fix' ), 12, 2 );
add_filter( 'elementor/editor/localize_settings', array( $this, 'translate_url_recursive' ) );
}
public function translate_url( $url ) {
return str_replace( $this->default_domain, $this->current_domain, $url );
}
public function translate_url_recursive( $data ) {
if ( is_string( $data ) ) {
$data = $this->translate_url( $data );
} elseif ( is_array( $data ) ) {
$data = array_map( array( $this, 'translate_url_recursive' ), $data );
}
return $data;
}
public function add_allowed_origins( $origins ) {
$origins[] = $this->current_domain;
$origins = array_merge( $origins, $this->all_domains );
return $origins;
}
public function modify_adminy_url_for_ajax( $url, $path, $blog_id ) {
if ( 'admin-ajax.php' == $path ) {
return $this->translate_url( $url );
}
return $url;
}
public function check_for_src($attr, $attachment) {
$attr['src'] = $this->translate_url( $attr['src'] );
$attr['srcset'] = $this->translate_url( $attr['srcset'] );
return $attr;
}
// change the edit and elementor-edit links in post table
public function elementor_links_fix( $actions, $post ) {
if(empty($actions['edit_with_elementor'])) return $actions;
if(!function_exists("pll_get_post_language")) return $actions;
if ( pll_get_post_language( $post->ID ) != pll_default_language() ) {
$actions['edit'] = $this->translate_url( $actions['edit'] );
$actions['edit_with_elementor'] = $this->translate_url( $actions['edit_with_elementor'] );
}
return $actions;
}
public static function get_instance() {
if ( ! self::$_instance ) {
self::$_instance = new self();
}
return self::$_instance;
}
}
@JanWebofka
Copy link

2022 Worked !

@investblog
Copy link

investblog commented Nov 13, 2023

The solution works in 2023! Jory, thank you so much, you really helped out by posting this simple but ingenious code.
The only thing was that I had to do a little bit of processing for some of the templates:

`
public function check_for_src($attr, $attachment) {
if ( isset( $attr['src'] ) ) {
$attr['src'] = $this->translate_url( $attr['src'] );
}

if ( isset( $attr['srcset'] ) ) {
    $srcset_parts = explode( ', ', $attr['srcset'] );
    $srcset_parts = array_map( function( $src ) {
        $src_info = explode( ' ', $src );
        $src_info[0] = $this->translate_url( $src_info[0] );
        return implode( ' ', $src_info );
    }, $srcset_parts );
    $attr['srcset'] = implode( ', ', $srcset_parts );
}
return $attr;

}
`

@emmasatti
Copy link

Bonjour,
j'ai installé votre plugin sur mon site pour régler les soucis d'accès à Elementor depuis le backoffice sur un site où le FR et le EN sont sur 2 URLS distinctes... ça ne fonctionne pas 😭 Ai-je loupé quelque chose ?
Merci pour votre aide.
Emma

@JoryHogeveen
Copy link
Author

Hi @emmasatti
Sorry but I have no idea why it wouldn't work for you without debugging your environment.
This code is provided freely but I cannot guarantee it will work for all installations.

@emmasatti
Copy link

Hi @JoryHogeveen,
finalement ça marche avec le plugin Connect Polylang for Elementor. Je l'avais déjà testé, mais j'ai compris aujourd'hui qu'il fallait supprimer WPS Hide Login qui bloquait l'identification sur la 2e langue.
Merci d'avoir pris le temps de me répondre 😊

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment