• Resolved arypneta

    (@arypneta)


    Does StockPack have a filter/hook to customize the file name? I looked at the documentation and didn’t see one.

    We’re finding that a lot of the time the default file name is super long and we’d like to automate truncating it ourselves in PHP (vs. having our content editors have to do it manually).

    Our thought would be to truncate the file name part to no more than 100 characters and then add “-stockpack-“, the provider name, and the media file id for uniqueness (stockpack_media_id). Basically, something like “a-picture-of-a-cat-stockpack-adobe-stock-1930493”.

    So, some kind of filter/hook that provides the generated name, the stock photo provider, and the stockpack_media_id as parameters somehow would be super helpful!

Viewing 6 replies - 1 through 6 (of 6 total)
  • Plugin Author StockPack

    (@stockpack)

    Hi @arypneta the easiest way would be to rename it after it is created via the following action

    do_action( 'stockpack_after_attachment_uploaded', $attach_id, $image );

    This runs right after download, let me know if this works for you,
    Thank you,
    Stockpack

    Thread Starter arypneta

    (@arypneta)

    Okay, thanks. I was using that action to update the description and other things already, but I wasn’t sure if that was the best way to update the file name.

    After testing, I can pretty easy update the post_title and post_name (slug) fields for the attachment post using that action. But, that doesn’t actually update the underlying file name/link to the actual file. It keeps the value it was uploaded with. After further testing, it appears that I can do some more manipulation with get_attached_file, update_attached_file, wp_generate_attachment_metadata, and wp_update_attachment_metadata, but I still feel like it would be much simpler to set a custom file name at the time of upload (since that is then all handled).

    But, I do seem to have gotten it working now, so if you can’t add that option, it’s not a big deal. Thanks for your help!

    Plugin Author StockPack

    (@stockpack)

    It was indeed a surprise that I haven’t added a filter directly for the filename, I will add that with the next release.

    Thank you!
    Stockpack

    Thread Starter arypneta

    (@arypneta)

    Okay, great – thanks!

    Plugin Author StockPack

    (@stockpack)

    @arypneta I have added the filter, here's an example on how to use it: 



    add_filter('stockpack_attachment_filename', 'add_custom_filename', 10, 2);

    function add_custom_filename( $filename, $image ) {
    /**
    * {
    * "id": 195254766,
    * "provider": "adobe_stock",
    * "description": "Morning tea waiting in the kitchen",
    * "url": "https:\/\/as2.ftcdn.net\/v2\/jpg\/01\/95\/25\/47\/500_F_195254766_DY0j0rMq4CvRgaTHPeQHWSSiVGF502Z4.jpg",
    * "download": "https:\/\/as2.ftcdn.net\/v2\/jpg\/01\/95\/25\/47\/1000_F_195254766_DY0j0rMq4CvRgaTHPeQHWSSiVGF502Z4.jpg",
    * "sizes": {
    * "full": {
    * "height": 667,
    * "width": 1000,
    * "orientation": "landscape",
    * "url": "https:\/\/as2.ftcdn.net\/v2\/jpg\/01\/95\/25\/47\/1000_F_195254766_DY0j0rMq4CvRgaTHPeQHWSSiVGF502Z4.jpg"
    * },
    * "medium": {
    * "height": 333,
    * "width": 500,
    * "orientation": "landscape",
    * "url": "https:\/\/as2.ftcdn.net\/v2\/jpg\/01\/95\/25\/47\/500_F_195254766_DY0j0rMq4CvRgaTHPeQHWSSiVGF502Z4.jpg"
    * },
    * "thumbnail": {
    * "height": 147,
    * "width": 220,
    * "orientation": "landscape",
    * "url": "https:\/\/as2.ftcdn.net\/jpg\/01\/95\/25\/47\/220_F_195254766_DY0j0rMq4CvRgaTHPeQHWSSiVGF502Z4.jpg"
    * }
    * }
    */
    return 'custom_filename_'.$image->id . $filename;
    }
    Thread Starter arypneta

    (@arypneta)

    Great, thanks! I had to change

    $image->id

    to

    $image->data->id

    to get the test snippet to work, but after that it was fine.

Viewing 6 replies - 1 through 6 (of 6 total)
  • You must be logged in to reply to this topic.