Support

Account

Home Forums Front-end Issues Rename file to postid.extension in Elementor loop

Unread

Rename file to postid.extension in Elementor loop

  • Hi!
    I’m using elementor loop item. I display a custom post type in elementor loop item. In every item I show acf_form for that particular custom post type.

    I want to rename the file name to the post id but I can’t get it to work. I don’t know if its because its in a loop item but I don’t think so because I would expect the file name to have which ever id but no.. it still has its original name. What am I missing here? I show the upload field with basic uploader and not wp in acf_form() and the campaign_featured_image is an image field that returns an URL and uploads to post.

    add_filter(‘acf/upload_prefilter/name=campaign_featured_image’, ‘acf_upload_prefilter’, 10, 3);
    function acf_upload_prefilter($errors, $file, $field) {
    // Pass the current post ID to the rename filter using a global
    if (isset($_POST[‘post_id’])) {
    $post_id = intval($_POST[‘post_id’]);
    add_filter(‘wp_handle_upload_prefilter’, function($file) use ($post_id) {
    $extension = pathinfo($file[‘name’], PATHINFO_EXTENSION);
    $file[‘name’] = $post_id . ‘.’ . $extension;
    return $file;
    }, 11, 1);
    }
    return $errors;
    }

Viewing 1 post (of 1 total)

You must be logged in to reply to this topic.