0

Need a piece of code in functions.php that redefines the path Jetengine Form saves media files to...

Ideally into the following path AND with another meta field entry in the path.

/wordpress folder/crewmembers/username/'meta field'/filename

Tried these

How Can I Organize the Uploads Folder by Slug (or ID, or FileType, or Author)?

How to set custom upload directory per user after moving content directory

I can get Wordpress to save the files to the new path but I can't seem to change the part from Jetengine code.

Background:

I am building a members site using Elementor and Jetengine. Using Jetengine Forms, users can enter their info and upload files to their user profile.

Per design (hardcoded - see below) Jetengine Forms stores uploaded media files into the following folder:

/wp-content/uploads/'User ID''Filename'

(I've unticked the 'Organize my uploads into month- and year-based folders.' in WP > Settings > Media)

Having asked Crocoblock Support if there is any way to change this. The answer unfortunately is no :(

The following code is from the Jetengine plugin. /wp-content/plugins/jet-engine/includes/modules/forms/file-upload.php

    /**
     * Process files upload
     *
     * @param  boolean $files [description]
     * @return [type]         [description]
     */
    public function process_upload( $files = false, $settings = array() ) {

        $settings = wp_parse_args( $settings, array(
            'max_size'          => wp_max_upload_size(),
            'max_files'         => 1,
            'insert_attachment' => false,
        ) );

        $insert_attachment = filter_var( $settings['insert_attachment'], FILTER_VALIDATE_BOOLEAN );

        if ( ! $files ) {
            $files = $_FILES;
        }

        if ( empty( $files ) || ! is_array( $files ) ) {
            return false;
        }

        if ( count( $files ) > $settings['max_files'] ) {
            wp_send_json_error( $settings['messages']['upload_max_files'] );
        }

        $result = array();
        $index  = 0;

        foreach ( $files as $file ) {

            if ( ! $file['size'] > $settings['max_size'] ) {
                wp_send_json_error( $settings['messages']['upload_max_size'] );
            }

            if ( ! empty( $settings['mime_types'] ) && ! in_array( $file['type'], $settings['mime_types'] ) ) {
                wp_send_json_error( $settings['messages']['upload_mime_types'] );
            }

            $result[] = $this->upload_file( $file, $insert_attachment );

        }

        return $result;

    }

    /**
     * Upload file
     *
     * @return [type] [description]
     */
    public function upload_file( $file = array(), $insert_attachment = false ) {

        $result = array();

        if ( ! function_exists( 'wp_handle_upload' ) ) {
            include_once ABSPATH . 'wp-admin/includes/file.php';
            include_once ABSPATH . 'wp-admin/includes/media.php';
        }

        add_filter( 'upload_dir', array( $this, 'apply_upload_dir' ) );

        $upload = wp_handle_upload(
            $file,
            array( 'test_form' => false )
        );

        if ( empty( $upload['error'] ) && $insert_attachment ) {

            $filepath   = $upload['file'];
            $attachment = wp_insert_attachment(
                array(
                    'guid'           => $upload['url'],
                    'post_mime_type' => $upload['type'],
                    'post_title'     => preg_replace( '/\.[^.]+$/', '', basename( $filepath ) ),
                    'post_content'   => '',
                    'post_status'    => 'publish'
                ),
                $filepath,
                0,
                true
            );

            if ( ! is_wp_error( $attachment ) ) {
                $metadata = wp_generate_attachment_metadata( $attachment, $filepath );
                wp_update_attachment_metadata( $attachment, $metadata );
            } else {
                $this->errors[] = $attachment->get_error_message();
            }

            $upload['attachment'] = $attachment;

        } elseif ( ! empty( $upload['error'] ) ) {
            $this->errors[] = $upload['error'];
        }

        remove_filter( 'upload_dir', array( $this, 'apply_upload_dir' ) );

        return $upload;

    }

    /**
     * Returns upload subdirectory
     *
     * @return [type] [description]
     */
    public function get_upload_dir() {

        $user_id       = get_current_user_id();
        $user_dir_name = $user_id ? $user_id : 'guest';
        $user_dir_name = apply_filters( 'jet-engine/forms/file-upload/user-dir-name', $user_dir_name );

        return $this->upload_base() . '/' . $user_dir_name;
    }

    /**
     * Returns upload base directory
     *
     * @return [type] [description]
     */
    public function upload_base() {
        return apply_filters( 'jet-engine/forms/file-upload/dir', 'jet-engine-forms' );
    }

    /**
     * Change upload directory for JetEngine uploads
     *
     * @param  [type] $pathdata [description]
     * @return [type]           [description]
     */
    public function apply_upload_dir( $pathdata ) {

        $dir = $this->get_upload_dir();

        if ( empty( $pathdata['subdir'] ) ) {
            $pathdata['path']   = $pathdata['path'] . '/' . $dir;
            $pathdata['url']    = $pathdata['url'] . '/' . $dir;
            $pathdata['subdir'] = '/' . $dir;
        } else {
            $new_subdir         = '/' . $dir . $pathdata['subdir'];
            $pathdata['path']   = str_replace( $pathdata['subdir'], $new_subdir, $pathdata['path'] );
            $pathdata['url']    = str_replace( $pathdata['subdir'], $new_subdir, $pathdata['url'] );
            $pathdata['subdir'] = $new_subdir;
        }

        return $pathdata;

    }
6
  • Obviously the bit you should be looking at is apply_upload_dir. You should be able to make this work by overriding the two filters called in get_upload_dir() and upload_base(), or if you've already got an upload_dir filter you can just set the priorities to make sure that will be run after the one that this code registers temporarily.
    – Rup
    Jan 23, 2022 at 12:50
  • @Rup - Thanks. But can this be done as snippet of code in the functions.php? I don't want to edit the original code. Kind of a noop when it comes to coming up with the code itself... Sorry
    – wpdemic
    Jan 23, 2022 at 13:05
  • How open would you be to abandoning JetEngine Forms and instead just putting together your own forms? If you must use a plugin, Contact Form 7 is very flexible under the hood, but if the plugin developers told you that you can't do it, then I'd take their advice. Jan 25, 2022 at 23:43
  • @TonyDjukic - thanks for the reply. The developers didn't tell it couldn't be done. It's more like they don't support it directly. I am guessing it can be done in the functions.php file with a filter. I am just not sure how to proceed with this part.
    – wpdemic
    Jan 26, 2022 at 15:52
  • Sorry but you had written "asked Crocoblock Support if there is any way to change this. The answer unfortunately is no :("... so I assumed that meant they hadn't provided a method of doing so. You have to keep in mind that other aspects of that plugin may rely on the code above to perform other functions relating to uploaded images. So if the developer hasn't build a method to permit hooking into it, it may be inadvisable to do so. That's more so what I was getting at. If they're telling you it's 'unsupported' then I'd listen to them. Jan 26, 2022 at 16:25

0

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Browse other questions tagged or ask your own question.