WordPress: Primary media library

Comments

8 comments

  • Avatar
    Ido

    Hi

    Currently, our WP plugin is geared towards delivering resources.

    If you want to upload a resource directly to Cloudinary, you could use any of our upload methods to upload to the account directly.

    We are not familiar with any plugin that uploads directly to Cloudinary.

    Please note that you could migrate images to Cloudinary from the media library, so if images are uploaded there, you could migrate them to Cloudinary from the WP media library.

    Hope this helps.

    0
    Comment actions Permalink
  • Avatar
    Jesse Bryant

    Hi Ido,

    Yes, that migration is good, but only in theory I'm afraid.

    I store all products to our WooCommerce page through the API, and the source (src) of the images are from the Cloudinary URLs.  It is here where it is storing the image in WP, rather than just using the cloudinary URL, bit annoying, but that's not your issue.

    What I have researched by using the migration your plugin provides however is update the _wp_attached_file value from wp_postmeta table with the URL of the migrated/uploaded image.  But in my case, because the value of _wc_attachment_source is already the Cloudinary URL - I'm of course doubling up on the same image in Cloudinary if I migrate it.

    I have made an SQL query to simply copy the value for each image:

    UPDATE wp_postmeta as source
    INNER JOIN wp_postmeta target ON target.`post_id` = source.`post_id` AND target.`meta_key` = '_wp_attached_file'
    SET target.`meta_value` = source.`meta_value`
    WHERE source.`meta_key` = '_wc_attachment_source' AND source.`meta_value` LIKE '%res.cloudinary.com%'

    While this worked in the database, it can't seem to pick up the URL when it comes to display the image - what else can I do for this query?  I have 10's of thousands of images, and I can assure you I'm not going to run through the migration doing only 20 (in bulk actions) at a time...

    Thanks.

    0
    Comment actions Permalink
  • Avatar
    Jesse Bryant

    Bump...

    I've attempted the following function, from attempting to reverse-engineer what happens in your plugin... Doesn't seem to work - but should it?

    function cMigrate($attachment_id) {
        global $wpdb;

        // Query Single
        $sql = "SELECT     target.`post_id`,
                          source.`meta_value` as source,
                          target.`meta_value` as target
              FROM wp_postmeta source
              INNER JOIN wp_postmeta target ON target.`post_id` = source.`post_id` AND target.`meta_key` = '_wp_attached_file'
              WHERE source.`meta_key` = '_wc_attachment_source' AND source.`meta_value` LIKE '%res.cloudinary.com%'
                AND target.`post_id` = $attachment_id";
        $res = $wpdb->get_results($sql);

      if (!isset($res[0])) return;
        // Get Attachment
        $attachment = get_post($attachment_id);

        $title = $attachment->post_title;
        $caption = $attachment->post_content;
        $post_parent = $attachment->post_parent;

        $new_url = $res[0]->source;
        $new_attachment = array(
            'ID'                => intval($attachment_id),
            'post_mime_type'    => "image/png",
            'guid'                 => $new_url,
            'post_parent'         => $post_parent,
            'post_title'          => $title,
            'post_content'        => $caption
        );

        $id = wp_insert_attachment($new_attachment, $new_url, $post_parent);

      // Delete?
      if (false) {
        unlink($res[0]->target);
      }
    }
    add_action('add_attachment','cMigrate',5,1);

    Thanks.

    0
    Comment actions Permalink
  • Avatar
    Ido

    Hi

     

    There are several things that can be done in order to migrate the images from the WP media library to the Cloudinary one faster.

    The simplest approach would be to tweak the media library to show more images. You could do that by changing this variable: $media_per_page = 20; (see a reference here)

    Another option is to list all images in the media library the same way media library does it, and then call "upload_to_cloudinary($attachment_id, $migrate)" function from our plugin.

    another thing you could try is to use a CLI command that the community created that migrates automatically. Note that we didn't create or maintain that tool, so we can't guarantee its effectiveness.

    You could find it here: https://github.com/cloudinary/cloudinary_wordpress/pull/26

    Let me know if any of these help?

     

    0
    Comment actions Permalink
  • Avatar
    Jesse Bryant

    Hi Ido,

    This does help, but I think I've taken myself off course a little - want to use the "Upload to Cloudinary" just so that WP will use the URLs from there, but I don't want to upload the product using the original URL, only to have the same image copied back in to Cloudinary.

    I've got it fixed now, I might blog about it soon - but I've basically taken the above method to replace the meta data, I've also borred the method your plugin uses wp_get_attachment_url to give me the URL from _wc_attachment_source, and as for the thumbnails in _wp_attachment_metadata, I've developed a script to iterate through the sizes - and strip out everything starting from the the first "-".

    That, along with automatically deleting the file off our server from the above method - has kept things rather clean.

    Cheers,
    Jesse.

    0
    Comment actions Permalink
  • Avatar
    Ido

    Thanks for letting us know!

    Glad to hear you found a solution that works for you.

    If you do decide to blog about it let us know :)

     

    Thanks,

    Ido

    0
    Comment actions Permalink
  • Avatar
    Jesse Bryant

    Hi again,

    Wrote a small article about my journey here.  Would be delighted to hear your comments.

    Cheers,
    Jesse.

    0
    Comment actions Permalink
  • Avatar
    Ido

    Thanks for sharing!

     

    I've passed this to the relevant team member.

    If it's relevant we would contact you for further details.

    0
    Comment actions Permalink

Please sign in to leave a comment.