• Resolved matanzari

    (@matanzari)


    Hey,

    After doing some digging, I see that there’s a filter ‘cloudinary_id’ (cloudinary-image-management-and-manipulation-in-the-cloud-cdn/php/class-media.php:695) which seems to allow changing the public id when uploading.
    I tried adding the following:

    
    add_filter( 'cloudinary_id', 'prefix_get_cloudinary_public_id', 10, 2 );
    function prefix_get_cloudinary_public_id( $public_id, $attachment_id ) {
    
    	$public_id_from_db = prefix_get_cloudinary_id_by_attachment_id( $attachment_id );
    
    	if ( empty( $public_id_from_db ) ) {
    		global $wpdb;
    		$wpdb->insert( "{$wpdb->prefix}prefix_images", [ 'attachment_id' => $attachment_id ] );
    
    		$public_id_from_db = prefix_get_cloudinary_id_by_attachment_id( $attachment_id );
    
    		if ( ! empty( $public_id_from_db ) ) {
    			$public_id = $public_id_from_db;
    		}
    	}
    
    	return $public_id;
    }
    

    but this doesn’t seem to work.
    Some debugging shows that the hook is called and the public id output is correct (its numeric), but is seems like the file name is used anyways.

    Is there something wrong with my implementation? Or maybe this hook doesn’t do what I think it does.

    I’m using the plugin’s latest version (2.1.2), WP version 5.3.2.

    Thanks.

Viewing 3 replies - 1 through 3 (of 3 total)
  • Thread Starter matanzari

    (@matanzari)

    Updated solution:

    
    add_filter( 'cloudinary_upload_options', function( $options, $attachment_post, $upload_object ){
      
      /**
      * Structure of options as follows:
      $options = array(
        'unique_filename' => false, // Allow for auto filenames. See Cloudinary docs.
        'resource_type'   => $resource_type, // Type can be 'image' or 'video' or custom.
        'public_id'       => $public_id, // Changing this will change the public ID as stored in Cloudinary.
        'context'         => array(
          'caption' => esc_attr( $post->post_title ), // Set the caption data in Cloudinary.
          'alt'     => $post->_wp_attachment_image_alt, // set the alt text of the asset in Cloudinary
        ),
      );
      **/
      
      // To change the asset public_id. this can be any valid public id
      $options['public_id'] = $options['public_id'] . '-custom-text';
      
      return $options;
    }, 10, 3 );
    
    Plugin Support Erwin @ Cloudinary

    (@erwincloudinary)

    Hi @matanzari,

    I apologize that we completely missed this post and didn’t get back to you sooner.
    It appears you are trying to change the public id upon syncing, and I’m glad to hear you found the solution by modifying our plugin.

    If I may ask, can you share your use-case why do you want to change the public id to something else instead of using the original filename?

    Regards,
    Erwin Lukas

    Plugin Support Erwin @ Cloudinary

    (@erwincloudinary)

    Hi @matanzari,

    Upon reviewing our internal ticket, I believe you also opened an internal ticket to Cloudinary and got it resolved since then.

    I’m going to resolve this post as well.

    Thanks,
    Erwin Lukas

Viewing 3 replies - 1 through 3 (of 3 total)
  • The topic ‘Changing Public Id’ is closed to new replies.