• Resolved docbt

    (@docbt)


    Hi!

    I have some serious problems with elementor running on a multilingual page on subdomains.

    My site is running on https://www.domain.de for german, en.domain.de for english and ja.domain.de for japanese.

    I change wp_home and wp_base_url dynamicly. Which works create, except with Elementor.

    If I want to edit content for a specific language, I have to use the subdomain for the language: e.g. english = https://en.domain.de/wp-admin/post.php?post=2801&action=elementor

    This works for german and japanese sites, but for english sites I am getting an endless loading loop in the left sidebar caused by the admin-ajax.php .

    Status 400
    Bad Request
    Version HTTP/2
    Referrer Policyno-referrer-when-downgrade

    The left Elementor sidebar is a little bit greyed out and I see the loading circle the whole time…

    Looking at the http headers, the problem seems to be that Elementor is using ‘www.domain.de’ as host for SOME requests (e.g. action “elementor_ajax” and “action “elementor_get_images_details”) by admin-ajax.php, but the origin is the subdomain ‘en.domain.de’. So the return is 400…

    Some other requests by admin-ajax.php (the heartbeat) are correct with en.domain.de as host and origin. Return is 200 and works.

    Screenshot-2020-10-07-095119

    Is there a config value / filter / hook to change the host used by Elemtor?
    Or is it saved in the database?

    And why is it working for ja.domain.de and https://www.domain.de? Thats strange, isn′t it? ??

    Thanks in advance!
    Andy

    • This topic was modified 4 years, 5 months ago by docbt.
    • This topic was modified 4 years, 5 months ago by Yui.
Viewing 6 replies - 1 through 6 (of 6 total)
  • Thread Starter docbt

    (@docbt)

    Ok, I solved it myself ?? The solution is to change the URL of the admin-ajax.php to a relative path, instead of absolute.

    Add this to your themes functions.php:

    function modify_adminy_url_for_ajax( $url, $path, $blog_id ) {
        if ( "admin-ajax.php" == $path ) {
            $url = "admin-ajax.php";
        }
        return $url;
    }
    add_filter("admin_url", "modify_adminy_url_for_ajax", 10, 3 );

    And add all your domains to the allowe origins:

    add_filter('allowed_http_origins', 'add_allowed_origins');
    function add_allowed_origins($origins) {
        $origins[] = 'https://www.domain.de';
        $origins[] = 'https://ja.domain.de';
        $origins[] = 'https://en.domain.de';        
        return $origins;
    }
    • This reply was modified 4 years, 5 months ago by docbt.
    Thread Starter docbt

    (@docbt)

    The above code won′t work in sub-subpaths. So better use this one:

    function modify_adminy_url_for_ajax( $url, $path, $blog_id ) {
        if ( "admin-ajax.php" == $path ) {
            $url = home_url( '/' ) . WP_ADMIN_DIR . "/admin-ajax.php";
        }
        return $url;
    }
    add_filter("admin_url", "modify_adminy_url_for_ajax", 10, 3 );

    Hey Doc i had the exact same problem with a german main domain and multiple domains. i can not make changes to domain origin policy serverside. by the way i am using polylang. and its always important to open page edit mode with the correct domain!
    my domain setup:
    base (german): domain.de
    2nd language (english): domain.io
    3rd language (polish): pl.domain.io
    4th language (spanish): es.domain.io

    so if you want to edit an english page, you have to change address url domain from domain.de/* to domain.io/* ! and you have to do the following steps too:

    things i did to fix the frontend issues every use get confronted with:

    1. hook into assets output:

    add_filter( 'script_loader_src', 'wpse47206_src' );
    add_filter( 'style_loader_src', 'wpse47206_src' );
    function wpse47206_src( $url )
    {
        if (strpos($_SERVER['SERVER_NAME'],'pl.domain.io') !== false) {
            return str_replace('domain.de', 'pl.domain.io', $url);
        }
        if (strpos($_SERVER['SERVER_NAME'],'es.domain.io') !== false) {
            return str_replace('domain.de', 'es.domain.io', $url);
        }
        if (strpos($_SERVER['SERVER_NAME'],'domain.io') !== false) {
            return str_replace('domain.de', 'domain.io', $url);
        }
        if (strpos($_SERVER['SERVER_NAME'],'domain.de') !== false) {
            return str_replace('domain.io', 'domain.de', $url);
        }
    
        return $url;
    }

    2. hook into image attributes output paths:

    function check_for_src($attr, $attachment){
        if (strpos($_SERVER['SERVER_NAME'],'pl.domain.io') !== false) {
            $find_and_replace = str_replace("domain.de","pl.domain.io",$attr["src"]);
            $attr["src"] = $find_and_replace;
            $attr["srcset"] = $find_and_replace;
            return $attr;
        }
        if (strpos($_SERVER['SERVER_NAME'],'es.domain.io') !== false) {
            $find_and_replace = str_replace("domain.de","es.domain.io",$attr["src"]);
            $attr["src"] = $find_and_replace;
            $attr["srcset"] = $find_and_replace;
            return $attr;
        }
        if (strpos($_SERVER['SERVER_NAME'],'domain.io') !== false) {
            $find_and_replace = str_replace("domain.de","domain.io",$attr["src"]);
            $attr["src"] = $find_and_replace;
            $attr["srcset"] = $find_and_replace;
        }
        if (strpos($_SERVER['SERVER_NAME'],'domain.de') !== false) {
            $find_and_replace = str_replace("domain.io","domain.de",$attr["src"]);
            $attr["src"] = $find_and_replace;
            $attr["srcset"] = $find_and_replace;
        }
        return $attr;
    }
    add_filter("wp_get_attachment_image_attributes","check_for_src",10,2);

    now every path problem gets solved. not very elegant but works. path get the same domain like in address bar. there will be no cross domain issue anymore.

    next issue was elementor editor for admin users. elementor always fires admin-ajax.php 400 error and i had no clue where to look for. all i could see was still a wrong domain in js console calling admin-ajax.php. besides that i still had some minor cors issues with SVG and font assets. those where fixed by the chrome addon “CORS Unblock”. thats just fine as only editors need to install this addon.

    last thing to do is still admin-ajax.php issue. glad i found your thread here!!! had no idea where to fix that. but with your first code, this issue is gone. by the way: second code with WP_ADMIN_DIR constant does not work. WP_ADMIN_DIR seems unknown!

    Hey Doc,

    I have the same issue while saving/publish from Elementor. can You please help me with this
    server error (400 error)

    Thread Starter docbt

    (@docbt)

    Hi guys!
    @nicmare: With a relative path some other plugins might break. For me Ultimate Member for example.
    So you should really use the second code I posted together with
    define('WP_ADMIN_DIR', 'wp-admin');
    in your wp-config.php

    The CORS problem can be solved by adding the following code for all your domains to your functions.php

    function add_allowed_origins($origins) {
        $origins[] = 'https://www.domain.de';
        $origins[] = 'https://ja.domain.de';
        $origins[] = 'https://en.domain.de';        
        return $origins;
    }
    add_filter('allowed_http_origins', 'add_allowed_origins');

    This way noone needs to install a browser plugin ??

    Your solution with the include style and script loader src is not relevant for me, cause I am using a static URL for all of my inlcudes/scripts/images/stylesheets (static.domain.de) anyway. This is good for the page speed and search engines (see cookie-free domain).

    Here is my complete wp-config stuff for dynamic URLs with SSO (Single Sign On):

    $currentpath = preg_replace('@/+$@','',dirname($_SERVER['SCRIPT_NAME']));
    $currentpath = preg_replace('/\/wp.+/','',$currentpath);
    $protocol=$_SERVER['PROTOCOL'] = isset($_SERVER['HTTPS']) && !empty($_SERVER['HTTPS']) ? 'https' : 'http';
    $currenthost = $protocol . "://".$_SERVER['HTTP_HOST'];
    define('WP_HOME',$currenthost.$currentpath);
    define('WP_SITEURL',$currenthost.$currentpath);
    define("WP_CONTENT_URL", "https://static.domain.de/wp-content"); 
    define('WP_PLUGIN_URL', 'https://static.domain.de/wp-content/plugins');
    //define('DOMAIN_CURRENT_SITE', $currenthost );
    define('COOKIE_DOMAIN', '.domain.de'); // Added by W3 Total Cache
    define('COOKIEPATH', '/');
    define('COOKIEHASH', '####Secret_Random_String###');
    //define('SITECOOKIEPATH', '/');
    define('AUTH_KEY', '####Secret_Random_String###);
    define('SECURE_AUTH_KEY', '####Secret_Random_String###');
    define('LOGGED_IN_KEY', '####Secret_Random_String###');
    define('AUTH_SALT', '####Secret_Random_String###');
    define('SECURE_AUTH_SALT', '####Secret_Random_String###');
    define('LOGGED_IN_SALT', '####Secret_Random_String###');
    ini_set('session.cookie_domain', '.domain.de');
    
    define('WP_ADMIN_DIR', 'wp-admin');

    @isuruxcode
    What did you try already?

    pintaf

    (@pintaf)

    Just created an account to thank you @docbt Many thanks for digging trough this and sharing your solution.

Viewing 6 replies - 1 through 6 (of 6 total)
  • The topic ‘Bad Request (400) when editing multilingual page’ is closed to new replies.