Viewing 2 replies - 1 through 2 (of 2 total)
  • Hi @chcw,

    I used GTranslate plugin one of my client site and their technical support is very good so you can talk with them and hopefully they’ll provide you better workaround.

    You can also try below approaches:

    1. Use home_url() with a Custom Path
      Instead of using admin_url() or site_url(), you can try using home_url() and manually constructing the path:

      This approach might prevent GTranslate from altering the URL.
    wp_localize_script($this->plugin_name, 'MyObj', array(
    'ajax_url' => home_url('/wp-admin/admin-ajax.php'),
    ));
    1. Disable URL Translation for Specific URLs
      Some translation plugins allow you to exclude certain URLs or scripts from being translated. You can check if GTranslate has an option to exclude specific URLs from translation, particularly the admin-ajax.php URL.
    2. Manual Fix in JavaScript
      If the above methods don’t work, you can manually correct the URL in JavaScript. For example, you could strip out the language slug before making the AJAX request:

    This approach is more of a workaround and should be tailored to handle different languages dynamically if your site supports more than just Chinese.

      let ajaxUrl = MyObj.ajax_url.replace('/zh-CN', ''); // Adjust for different language slugs
      console.log("Corrected Ajax url: ", ajaxUrl);
      altaan

      (@altaan)

      To prevent wp_localize_script from localizing the AJAX URL in the array, simply exclude it from the array you pass to wp_localize_script. Here’s an example:

      $data = array(
      ‘some_data’ => ‘value’,
      // ‘ajax_url’ => admin_url(‘admin-ajax.php’), // Do not include this
      );
      wp_localize_script(‘your-script-handle’, ‘localizedData’, $data);

      This way, the ajax_url is not included in the localized data.

      • This reply was modified 3 months ago by altaan.
    Viewing 2 replies - 1 through 2 (of 2 total)
    • You must be logged in to reply to this topic.