通过 Forminator 为每张上传的图像附加唯一 ID 前缀

问题描述 投票:0回答:1

我正在使用 WordPress 挂钩在 Forminator 表单提交后创建新帖子。在 Forminator 表单中,有一个文件上传字段,用户可以从中上传图像。我希望这些图像在提交后用作新创建的帖子的缩略图。我在主题的functions.php 文件中使用以下代码:

add_action('forminator_form_after_handle_submit', 'create_post_with_image_and_redirect', 10, 2);
add_action('forminator_form_after_save_entry', 'create_post_with_image_and_redirect', 10, 2);

function create_post_with_image_and_redirect($form_id, $response) {
    // Check if the form ID matches
    if ($form_id == 761) {
        // Get the uploaded image file
        $uploaded_image = $_FILES['upload-1'];

        // Check if an image was uploaded
        if (!empty($uploaded_image['name'])) {
            // Define the upload directory
            $upload_dir = wp_upload_dir();
            $upload_path = $upload_dir['path'] . '/';

            // Generate a unique filename
            $image_filename = $uploaded_image['name'];

            // Move the uploaded image to the upload directory
            $image_path = $upload_path . $image_filename;
            move_uploaded_file($uploaded_image['name'], $image_path);

            // Create the post title.
            $post_title = 'Test-Title';

            $post_content .= "\nimage path: " . $image_path . "\nfilepath: " . $image_filename;
            // Create a new post
            $post_data = array(
                'post_title'    => $post_title,
                'post_content'  => $post_content,
                'post_status'   => 'publish',
                'post_type'     => 'post'
            );

            // Insert the post into the database
            $post_id = wp_insert_post($post_data);

            // Attach the image to the post
            if ($post_id && !is_wp_error($post_id)) {
                $attachment = array(
                    'post_title'     => $image_filename,
                    'post_content'   => '',
                    'post_status'    => 'inherit',
                    'post_mime_type' => $uploaded_image['type'],
                );

                $attachment_id = wp_insert_attachment($attachment, $image_path, $post_id);
                if (!is_wp_error($attachment_id)) {
                    require_once(ABSPATH . 'wp-admin/includes/image.php');
                    $attachment_data = wp_generate_attachment_metadata($attachment_id, $image_path);
                    wp_update_attachment_metadata($attachment_id, $attachment_data);
                    set_post_thumbnail($post_id, $attachment_id); // Set the image as featured image
                }
            }
        }
    }
}

行为如下: 唯一的 id 前缀附加到图像的名称并导致问题。 例子: 用户上传具有以下名称的图像:Screenshot-2023-07-25-171253.png 然后用户提交表单。 php-error.log 中收到以下错误消息:

[05-Sep-2023 12:21:47 UTC] PHP Warning:  getimagesize(/var/www/html/wp-content/uploads/Screenshot 2023-07-25 171253.png): Failed to open stream: No such file or directory in /var/www/html/wp-includes/media.php on line 5307
[05-Sep-2023 12:21:47 UTC] PHP Warning:  exif_imagetype(/var/www/html/wp-content/uploads/Screenshot 2023-07-25 171253.png): Failed to open stream: No such file or directory in /var/www/html/wp-includes/functions.php on line 3268
[05-Sep-2023 12:21:47 UTC] PHP Warning:  file_get_contents(/var/www/html/wp-content/uploads/Screenshot 2023-07-25 171253.png): Failed to open stream: No such file or directory in /var/www/html/wp-includes/functions.php on line 3291

当我查看目录 /var/www/html/wp-content/uploads 时,我看到以下内容:

ll -t
total 1820
drwxr-xr-x 6 www-data www-data  12288 Sep  5 00:36 ./
-rw-r--r-- 1 www-data www-data   2052 Sep  5 00:36 hNOIpKCMymwC-Screenshot-2023-07-25-171253.png
...

以下唯一 ID 前缀

hNOIpKCMymwC-
已附加到图像的原始名称。 我做了一些研究,发现 Forminator 插件附加了这个唯一的 id,以防止在两个用户碰巧上传同名图像时覆盖图像。但是,我需要找到解决方案,以便我可以正确使用上传的图像作为缩略图,并找到一种方法来获取此唯一标识符的值并在我的代码中使用它。

php wordpress wordpress-theming custom-wordpress-pages wordpress-shortcode
1个回答
-1
投票

要处理 Forminator 插件向上传的图像添加唯一 ID 前缀的问题,您需要修改代码以适应此行为。您可以按照以下步骤操作:

  1. 检索唯一 ID :由于 Forminator 会在文件名后附加一个唯一 ID,因此您需要找到一种方法来检索此修改后的文件名。不幸的是,Forminator 没有提供直接获取此名称的简单方法。但是,您可以通过检查

    $_FILES
    数组或最新文件的上传目录(假设文件刚刚上传)来解决此问题。

  2. 修改文件名处理:更新处理图像文件名的代码部分以使用此唯一 ID。

这是经过这些更改的代码的修订版本:

add_action('forminator_form_after_handle_submit', 'create_post_with_image_and_redirect', 10, 2);
add_action('forminator_form_after_save_entry', 'create_post_with_image_and_redirect', 10, 2);

function create_post_with_image_and_redirect($form_id, $response) {
    // Check if the form ID matches
    if ($form_id == 761) {
        // Get the uploaded image file
        $uploaded_image = $_FILES['upload-1'];

        // Check if an image was uploaded
        if (!empty($uploaded_image['name'])) {
            // Define the upload directory
            $upload_dir = wp_upload_dir();
            $upload_path = $upload_dir['path'] . '/';

            // Assuming that the latest uploaded file in the directory is the one we need
            $files = scandir($upload_dir['path'], SCANDIR_SORT_DESCENDING);
            $newest_file = $files[0]; // Get the latest file name

            // Generate a unique filename
            $image_filename = $uploaded_image['name'];

            // Move the uploaded image to the upload directory
            $image_path = $upload_path . $newest_file;
            move_uploaded_file($uploaded_image['tmp_name'], $image_path);

            // Rest of your code...
        }
    }
}

在这段代码中,

$newest_file
变量用于获取最近上传的文件,该文件应该是具有唯一ID前缀的图像。需要注意的是,这种方法假设没有其他文件同时上传到同一目录,这可能会导致选择错误的文件。

此解决方案是一种解决方法,可能不是最可靠的。理想情况下,Forminator 应该提供一种直接检索修改后的文件名的方法,但如果该方法不可用,则可以谨慎使用此方法。

© www.soinside.com 2019 - 2024. All rights reserved.