Forum Replies Created

Viewing 8 replies - 1 through 8 (of 8 total)
  • Thread Starter msmilkov

    (@msmilkov)

    Hello Team,

    Any updates on this one?

    Thanks!

    Thread Starter msmilkov

    (@msmilkov)

    Hello @wpmudev-support8,

    Thank you for the response! I tried this solution and it kind of worked. So what happens now is that the code executes properly (images are properly uploaded, post is created), however, I receive an error when Submitting the form and hence the Form Redirect doesn’t work. The error messages are:

    Error: Your form is not valid, please fix the errors!

    And also:

    Error saving Form. Failed to read uploaded file.

    I tried the exact same code but with AJAX as an option for the Upload Method, form submits successfully (no error message & proper redirect) but the code doesn’t execute and therefore, no new post is created.

    I also tried to play with the Behavior Settings and changed the “Method” from Ajax to Page reload, same issue but the page reloads.
    Also, I tried to change the validation from On Submit to Server Side, still the same issue.

    Thread Starter msmilkov

    (@msmilkov)

    Hello @wpmudevsupport13,

    Thanks for the reply! Are there any updates regarding this one?

    Thread Starter msmilkov

    (@msmilkov)

    Hello @wpmudevsupport14,

    Thank you for the response. I tried your solution and I can confirm that the code now works as expected. You can consider this topic resolved and close it.

    Thank you for all your support!

    Thread Starter msmilkov

    (@msmilkov)

    Hey @wpmudevsupport15,

    Thank you for providing the resources. Unfortunately, after one week of struggling I couldn’t make it work using the provided code snipped. Below, I am providing more information about the ways that I tried and couldn’t achieve my goal:

    I tried generating the uid using time() & uniqid() functions. I observed the following results:

    $unique_id = uniqid();
    $image_filename = $unique_id . '-' . $uploaded_image['name'];


    The value of $image_path variable printed in the newly created post is:

    image path: /var/www/html/wp-content/uploads/1694693112-Screenshot 2023-07-25 171253.png

    However, in the /uploads directory, the file name got overwritten again and hence the same error messages were received:

    /uploads$ ll -t
    total 188
    
    -rw-r--r-- 1 www-data www-data 2052 Sep 14 15:05  uk6YoNXPDZqV-Screenshot-2023-07-25-171253.png

    So even the unique id generated by time() & uniqid() functions got overwritten from “1694693112-” to “uk6YoNXPDZqV-“.

    I also tried to create a static name for the uploaded file, just for testing purposes and the results were the following:

    $image_filename = 'test-file';
    

    Error messages:

    [14-Sep-2023 12:00:46 UTC] PHP Warning:  getimagesize(/var/www/html/wp-content/uploads/test-file): Failed to open stream: No such file or directory in /var/www/html/wp-includes/media.php on line 5307
    [14-Sep-2023 12:00:46 UTC] PHP Warning:  exif_imagetype(/var/www/html/wp-content/uploads/test-file): Failed to open stream: No such file or directory in /var/www/html/wp-includes/functions.php on line 3268
    [14-Sep-2023 12:00:46 UTC] PHP Warning:  file_get_contents(/var/www/html/wp-content/uploads/test-file): Failed to open stream: No such file or directory in /var/www/html/wp-includes/functions.php on line 3291

    Uploaded image name:

    /uploads$ ll -t
    total 188
    
    -rw-r--r-- 1 www-data www-data 2052 Sep 14 15:00  ZhuBfX04ogBB-Screenshot-2023-07-25-171253.png

    So my question is: Is there a way to turn off the unique id prefix that Forminator appends to the front of each uploaded file, so I can generate it using other solutions that I can have control on? Whatever I do, Forminator always overwrites the filename with unique prefix + original filename.

    Thank you in advance for your support, let me know if I can provide you more information if needed!

    Thread Starter msmilkov

    (@msmilkov)

    Hello Zafer,
    Thank you for the swift response!
    I tried the code with the modifications that you recommended

    237 function create_post_with_image_and_redirect($form_id, $response) {
    238     // Check if the form ID matches
    239     if ($form_id == 761) {
    240         // Get the uploaded image file
    241         $uploaded_image = $_POST['upload-1'];
    242
    243         // Check if an image was uploaded
    244         if (!empty($uploaded_image)) {
    245             // Define the upload directory
    246             $upload_dir = wp_upload_dir();
    247             $upload_path = $upload_dir['path'] . '/'. basename($uploaded_image);

    However, I run into the following warning:

    PHP Warning:  Undefined array key "upload-1" in /var/www/html/wp-content/themes/hello-elementor/functions.php on line 241

    In addition, no new post was created from the form submission and also the image did not get uploaded to /var/www/html/wp-content/uploads.

    Thread Starter msmilkov

    (@msmilkov)

    Hello,

    Thank you for the response!

    I managed to resolve my issue with the following modified code:

    add_action ( 'forminator_form_after_handle_submit', 'create_post_and_redirect', 10, 2 );
    add_action( 'forminator_form_after_save_entry', 'create_post_and_redirect', 10, 2 );
    
    function create_post_and_redirect($form_id, $response) {
        // Check if the form ID matches
        if ( $form_id == 761 ) {
            // Get the submitted textarea value
            $textarea_value = sanitize_text_field($_POST['textarea-1']);
    
            // Create a new post
            $post_data = array(
                'post_title'    => 'New Test Post',
                'post_content'  => $textarea_value,
                'post_status'   => 'publish',
                'post_type'     => 'post'
            );
    
            // Insert the post into the database
            $post_id = wp_insert_post($post_data);
        }
    }

    I also used the Forminator Settings that you previously showed me to fix the redirections, so now when I submit the Forminator Form, a new WordPress is being created with the content taken from the Form. Next, the users gets redirected to a page where he can see all Posts on the Website.

    Thank you for your help, feel free to close this topic.

    Thread Starter msmilkov

    (@msmilkov)

    Hello @wpmudevsupport13

    Thank you for the response!

    Do you have any updates yet? One additional detail that I did not provide in my initial question is that the WordPress Theme that I used is: Hello Elementor
    Also, I use a Docker environment (not sure if that will be useful to you).

Viewing 8 replies - 1 through 8 (of 8 total)