• Resolved Nazrinn

    (@nazrinn)


    Hello guys!

    I was wondering how I could display my images from the live site on my “localhost” staging since all the optimized URL changed to https://localhost? The images are in Optimole’s cloud, and I transferred the website from live to local using Duplicator.

    The image are currently not displaying at all.
    The images on live site are linked like this:
    https://ml275xldtxd5.i.optimole.com/AuPUZI8.6ey0~25ef9/w:1024/h:782/q:75/id:4b849919f7b558288070b17c2cae3bfd/https://barbandcarole.ca/Evans_D5S4843byMichelleValberg_full-e1616910336445.png

    While the same image link in local looks like this (Error 400):
    https://ml275xldtxd5.i.optimole.com/60b8Baw.6ey0~25ef9/w:1024/h:782/q:75/id:4b849919f7b558288070b17c2cae3bfd/https://localhost/dev/barbandcarole/Evans_D5S4843byMichelleValberg_full-e1616910336445.png

    How do I proceed for the URL images to point toward the live domain? Thanks!

    • This topic was modified 3 years, 7 months ago by Nazrinn.
Viewing 5 replies - 1 through 5 (of 5 total)
  • Plugin Support Vytis

    (@bvytis)

    Hi @nazrinn,

    Images are in the cloud so you can use them, however, this is more related to how you can change URLs of all your images in WordPress instance than related to Optimole plugin.

    You can try the approach described in this guide to update all the image URLs in your instance – https://www.wpbeginner.com/plugins/how-to-update-urls-when-moving-your-wordpress-site/

    I hope it helps!

    Thread Starter Nazrinn

    (@nazrinn)

    Hi there. Thank you Vitys. I was thinking the Show images only from these sites option was for that. It’s not the case?

    Anyway, the solution yo proposed didn’t work unfortunately. Despite the URL updater plugin telling me this, I see no change in the image URL on the frontend. It is still saying “localhost”. (I entered this into the fields.)

    Furthermore, I’d only like to affect media optimized by Optimole. I have PDF and video files which I wouldn’t like to go through with the URL change. It’s not a thing you can do with Optimole?

    Would rolling back picture be a good temporary fix or would it affect the live site?

    Thanks!

    Thread Starter Nazrinn

    (@nazrinn)

    Hi Vitys.

    So, I found out that the solution you proposed doesn’t work because Velvet Blues Update URLs plugin update the guid (image URL) column if the post_type is attachment in the post table. Like so:

    
    UPDATE $wpdb->posts SET guid = replace(guid, %s, %s) WHERE post_type = 'attachment'"
    

    However, Optimole-optimized URL are actually stored in the postmeta table rather than the post table. I’m working on a hand-coded fix. I’ll let you know what it looks like.

    Thread Starter Nazrinn

    (@nazrinn)

    Okay so I sorta have a solution. It’s not perfect, it creates some bugs (some images come back to me as 404), but it does the job. Most of my image are appearing as expected!

    Here is my code. It automatically execute itself after a page reload following the code implementation.

    <?php
    function point_image_url_to_main_domain() {
      $images = array();
      $old_url = get_home_url();
      $new_url = "https://barbandcarole.ca";
    
      global $wpdb;
      $optimole_img_ids = $wpdb->get_col("SELECT post_id FROM $wpdb->postmeta WHERE meta_key = 'optimole_offload' AND meta_value = 'true'");
    
      foreach ($optimole_img_ids as $img_id) {
        $img_metadata = $wpdb->get_var("SELECT meta_value FROM $wpdb->postmeta WHERE meta_key = '_wp_attachment_metadata' AND post_id = $img_id");
        if(str_contains($img_metadata, $old_url)) {
          $img_metadata = str_replace($old_url, $new_url, $img_metadata); //transforms the Optimole URL
          $query = $wpdb->update(
            $wpdb->postmeta,
            ['meta_value' => $img_metadata],
            ['post_id' => $img_id, 'meta_key' => '_wp_attachment_metadata']
          ); //use $wpdb->prepare if you make this into a user input.
          //$query = 1;
    
          if($query === false) {
            echo "<p>The query failed!</p>";
          } elseif ($query === 0) {
            echo "<p>The query succeeded but no rows were updated.</p>";
          } elseif ($query > 0) {
            echo "<p>Query successful! Image ".$img_id."'s metadata was updated.</p>";
          }
        }
      }
    }
    
    add_action("wp_after_admin_bar_render", "point_image_url_to_main_domain");
    ?>
    

    I get a few error 404 from the server trying to look for images in localhost still rather than through the barbandcarole.ca Optimole url (probably caused by WHERE meta_key = 'optimole_offload' AND meta_value = 'true'"), plus one 400 error over which my script was unable to go over (it was a CSS background image).

    Those 4XX error triggers an error from the Optimole plugin:

    Warning: Trying to access array offset on value of type bool in C:\xampp\htdocs\dev\barbandcarole\wp-content\plugins\optimole-wp\inc\tag_replacer.php on line 428
    

    but I don’t sweat it too much and will try to fix the remaining error tomorrow by amelioration my code.

    In my honest opinion, this kind of URL repointing should be a feature that comes with Optimole. It is really crucial for us advanced users who needs to make image-related adjustments (mostly in HTML/CSS) in test (hear: staging) environments.

    • This reply was modified 3 years, 7 months ago by Nazrinn.
    • This reply was modified 3 years, 7 months ago by Nazrinn.
    • This reply was modified 3 years, 7 months ago by Nazrinn.
    • This reply was modified 3 years, 7 months ago by Nazrinn.
    Thread Starter Nazrinn

    (@nazrinn)

    Well I figured migrating my live postmeta table to my local staging environment worked just fine. *facepalm*

    Though I understand this is not a perfect fix. As for my PHP, this is the latest version I came up with. Pretty similar. Still went with that first quick and dirty fix because the latter was too tricky to work around and caused bug, but it’s a start to create that feature I was thinking about.

    <?php
    function point_image_url_to_main_domain() {
      $old_url = get_home_url();
      $new_url = "https://barbandcarole.ca";
      
      global $wpdb;
      $post_ids = $wpdb->get_col("SELECT post_id FROM $wpdb->postmeta WHERE meta_key = 'optimole_offload' AND meta_value = 'true'");
      foreach ($post_ids as $post_id) {
        $meta_value = $wpdb->get_var("SELECT meta_value FROM $wpdb->postmeta WHERE post_id = $post_id AND meta_value LIKE '%$old_url%'");
      
        if(is_null($meta_value) === false) {
          $meta_value = str_replace($old_url, $new_url, $meta_value); //transforms the Optimole URL
      
          $query = $wpdb->update(
            $wpdb->postmeta,
            ['meta_value' => $meta_value],
            ['post_id' => $post_id]
          );
      
          if($query === false) {
            echo "<p>The query failed!</p>";
          } elseif ($query === 0) {
            echo "<p>The query succeeded but no rows were updated.</p>";
          } elseif ($query > 0) {
            echo "<p>Query successful! Post ".$post_id."'s metadata was updated.</p>";
          }
        }
      }
    }
    add_action("wp_after_admin_bar_render", "point_image_url_to_main_domain");
    
    ?>

    Let me know what you think.

Viewing 5 replies - 1 through 5 (of 5 total)
  • The topic ‘Getting images to show on local staging?’ is closed to new replies.