• Resolved dkurth

    (@dkurth)


    Hi again,
    Adding more features to my plugin.

    I need a how to on how I can override the file upload and not have the photo appear in the metadata tables. I thought about trying to write my own custom field, but that basic PHP code causes the form to shut upload clicking submit.

    need a recommendation or better yet, how-to that achieves the following:

    1. User selects a file, without the media library opening. Do not want any of these photos to appear in the WordPress environment.
    2. File upload is direct, where I can rename the file according the naming scheme and place it to another location..this case google drive as storage.

Viewing 15 replies - 1 through 15 (of 15 total)
  • Plugin Contributor Michael Beckwith

    (@tw2113)

    The BenchPresser

    Thought I replied to this that I was not able to get around to it yet, but apparently not.

    I have been told and thus also looked it over that https://github.com/CMB2/CMB2-Snippet-Library/blob/master/front-end/cmb2-front-end-submit.php has an example of a way to handle file uploads yourself.

    Specifically these spots:
    https://github.com/CMB2/CMB2-Snippet-Library/blob/master/front-end/cmb2-front-end-submit.php#L38-L46

    https://github.com/CMB2/CMB2-Snippet-Library/blob/master/front-end/cmb2-front-end-submit.php#L238

    https://github.com/CMB2/CMB2-Snippet-Library/blob/master/front-end/cmb2-front-end-submit.php#L254-L287

    It’s the last one that would hold everything to do with how to potentially handle the $_FILES data and where anything goes.

    Thread Starter dkurth

    (@dkurth)

    Those look promising! It was what I was searching the web for, but google failed me. Will get back to you with any questions.

    Thread Starter dkurth

    (@dkurth)

    Quick question. Where is this “media_handle_upload” function defined?

    Plugin Contributor Michael Beckwith

    (@tw2113)

    The BenchPresser

    Thread Starter dkurth

    (@dkurth)

    Decided to change directions and just use your tool only to discover that whenever I upload a file my loading of my data disappears. It still exists in my database, but the technique I was using to load the fields with my database data, is destroyed before displaying.

    Can you explain to me the technique you use? The theory behind it?

    Otherwise, I may need to spend an enormous amount of time trying to reinvent the wheel. The code samples you sent me had great possibilities, until you look at them and realize that they require a POST from a form.

    I can implement that using the standard HTML5 technique, but it requires a submit button. Not sure if I can implement the “submit button” for the photo and still have CMB2 still working.

    for something so simple, this sure is complicated.

    Plugin Contributor Michael Beckwith

    (@tw2113)

    The BenchPresser

    Not quite following what/where things are failing. Are you saying that the $_POST/$_FILES data from PHP and the POST request being made to the server are losing data at some point or it’s varying depending on when you’re interacting with the overall form submission request?

    All in all, we do try to use WP code whenever possible, including things like the media library management code mentioned earlier.

    Thread Starter dkurth

    (@dkurth)

    I understand wanting to use WP as much as possible.

    The samples that were shown, require a “post” (_POST) coming from a independent form. These forms have a two step process. Select the file and then submit it to the server ie: Submit. The submit generates a POST call, in which then you can read the form. That secondary post submital, naturally kills the data written in the fields, since it generates a refresh…naturally.

    SO I went back to the question of how to upload a file to the server, grab the location and then transfer it to where I want the files and remove any metapost table data…OR (the optimal way)..just plan upload the file upon a Update file button, to a custom location with Javascript alone using in a custom field box. I don’t mind a temporary location, but ultimately, my database is going to hold the URL to the location of the file for display purposes. That may be on a completely different server, on Google Drive or somewhere else.

    What I have been doing quite successfully is stuffing the CMB2 fields manually, with an override function or grabbing them, also with an override function on the save. Using the ‘file’ file type with CBM2, causes this data to be crunched. Just the act of pressing the Upload File button causes the problem.

    Next time I try to save the data, my point of reference – the custom post id now 0.

    What I suspect is that you are assuming a post-id in the upload process and since I am overriding, you don’t know what to do with it, so a 0 is assigned. Next time I come through and ask for the present post id, it is now different and my data, in the table, is no longer associated with my list.

    
    add_filter('cmb2_override_MMDListsRecord_meta_value', 'mmd_get_methods_custom_data', 10, 2);
    function mmd_get_methods_custom_data( $dont_override, $PostId )
    

    Thus the question about the theory of operation.

    My goal is two fold:

    1. Do not place the file in WordPress tables of any form. Why, because the more records in that MetaPost table, the slower the site.

    2. If I do use the WP media upload, the “library” tab has to be invisible. Why? Because these set of sites are for the technically challenged. The simplest things scares them and instead of working it through..they just give up and bad mouth the site. Watched it happen with another site..who went belly up. They refused to use it, even though it was a easier service..paying horse show bills. That will cause me to lose money. I know my market and the people using it. It needs to be so simple, a small child can do it and so obvious, there is never any issue. simple simple simple.

    That is were I have been coming from. So I wanted to know the internal theory of how a file is updated. If not through a post, then it must be Javascript code and if so, where? I looked through the source code and found bits and pieces, but not the complete picture. Something on the JavaScript side is uploading a file. I want to know how it is done. If you are saying it is all WP, then I either need to find another way OR find a way to not display the Library tab of WP.

    Thus I started my own custom field:

    function cmb2_render_callback_photo_upload($field, $escaped_value, $object_id, $object_type, $field_type_object)
    {
    	?>
    <script>
              function UploadFile()
                {
                  Java script code here
               }
    </script>
           <input type="file" name="afile" id="afile" accept="image/*" onchange=UploadFile()/>
    	<?php
    }
    add_action( 'cmb2_render_photo_upload', 'cmb2_render_callback_photo_upload', 10, 5 );
    

    Any thought you may have on the subject, I am all ears.
    Been looking at these sites:
    https://gist.github.com/ebidel/2410898

    Plugin Contributor Michael Beckwith

    (@tw2113)

    The BenchPresser

    I wish I had better answers for you much of the time, but you are also using CMB2 in unique/”different” ways than most people.

    That said, without having an idea of what your current form here looks like, it’s still ultimately a form on a page. Theoretically, you could simply hook in to the init hook on the frontend, and process the entirety of the form yourself, sending what $_POST data you want where, handle the $_FILES in whatever way, and carry on.

    CMB2 itself would still handle the saving/processing on its own though, unless hotwired somehow to not do anything.

    I have to believe in this case here, and throughout much of your code, it’s kind of in between those two places. Some places utilize CMB2 completely to handle, a lot intercept data and handle the way they need to be at that point and prevent normal saving, etc. None that just handle everything on your own from the basis of an HTTP request. Definitely part of the long term struggles have been that seemingly everything needs to not be stored in the default places like post meta or attachment post types with their own meta, or options table. I most definitely won’t say that’s bad, but it causes a lot of extra work.

    Thread Starter dkurth

    (@dkurth)

    Ok, I changed directions, because there is no good way to upload outside the CMB2 workset. I tweaked my code and I have figured out how to shut down the user seeing the Media Library tab, leaving only the upload tab. Goal #1 achieved.

    I also know how to move the file to the directory I want, since CMB2 nicely gives the path name, and delete the entries in both the wp_post and wp_postmeta tables. BUT if I do that, I need to tell CMB2, not only the new path (no issue), but the metadata and related via the _wp_attachment_metadata field. would be missing and thus the photo is not displayed in the form.

    {s:5:"width";i:75;s:6:"height";i:59;s:4:"file";s:23:"2019/05/DressagePin.png";s:5:"sizes";a:0:{}s:10:"image_meta";a:12:

    If I can do that, then I have achieved both goals..better user interface and not growing the WordPress tables, slowing the system. Looked in tips and tricks and advance usage plus searching for examples. so far, have not found any.

    What am I missing? What have ya got, oh wise one…..

    • This reply was modified 5 years, 10 months ago by dkurth.
    Plugin Contributor Michael Beckwith

    (@tw2113)

    The BenchPresser

    Clarify for me please. You’re trying to recreate that small preview feature with the file upload fields, but with the details coming from your custom location?

    Meta data like that serialized blob is what is not being set for your setup, and you’re possibly trying to recreate that?

    Thread Starter dkurth

    (@dkurth)

    The short answer is yes, trying to recreate the preview feature. AS in the field data at large, I push the values into the CBM2 data array, prior to display, using the override function call. I need to move the photo into a special location, maybe even google drive.

    The objective is to keep the WordPress database as small as possible for performance purposes. These sites will have upwards of 1,000 listings each and thus 1,000 photos. When you multiple that out, that means there are 3 entries are added to the database, for every single image uploaded. The impact on performance is huge.

    Present state:
    1. Upload is now working with my code using the “file” field type.
    2. 3 entries are being created in the WP database per image
    3. I know how to move the image file using ‘move_uploaded_file’
    4. I know how to delete the WP records, in the database from the upload
    5. I need to find a way to restore the meta data, so the preview works.

    • This reply was modified 5 years, 10 months ago by dkurth.
    Plugin Contributor Michael Beckwith

    (@tw2113)

    The BenchPresser

    Definitely get and understand the desire to keep performance high, but I also know that I’ve accessed sites recently with 6 to 7-digit row totals in some tables. Kind of depends on exactly how active things will be, and as always proactive is good.

    This part feels incomplete:

    {s:5:"width";i:75;s:6:"height";i:59;s:4:"file";s:23:"2019/05/DressagePin.png";s:5:"sizes";a:0:{}s:10:"image_meta";a:12:
    

    That said though, it looks like it’s a serialized object, maybe an array, with spots meant to hold image dimensions by my best guess, as well as what the relative file path is, possibly some other sizes, and then image meta.

    Looking at a basic “file” field type that I had handy, I’m getting these two items for the post meta related to it:

    https://cloudup.com/cH_alG9hc9B

    basically one meta field for the url, and then the id one is the attachment ID.

    If you’re using a different field type than just “file” let me know and I can create one quick, save some data, and check the metadata structure. The angle I’m taking here is helping provide a map of data to utilize for the field.

    Thread Starter dkurth

    (@dkurth)

    I am using just file for this portion.

    This is a google mapped database site, using CMB2 as the interface for entering in new listings on both the admin and the user profile areas. Here is just 1 of the 25 different lists and each list is growing. This is a world wide site. HEAVY database useage and thus speed.

    https://horse-trainers.com/dressage-list/

    If I look up in the database the entire media serialized data it is this:

    a:5:{s:5:"width";i:525;s:6:"height";i:350;s:4:"file";s:31:"2019/05/coming-soon-mlb-585.jpg";s:5:"sizes";a:6:{s:9:"thumbnail";a:4:{s:4:"file";s:31:"coming-soon-mlb-585-150x150.jpg";s:5:"width";i:150;s:6:"height";i:150;s:9:"mime-type";s:10:"image/jpeg";}s:6:"medium";a:4:{s:4:"file";s:31:"coming-soon-mlb-585-300x200.jpg";s:5:"width";i:300;s:6:"height";i:200;s:9:"mime-type";s:10:"image/jpeg";}s:21:"woocommerce_thumbnail";a:5:{s:4:"file";s:31:"coming-soon-mlb-585-300x300.jpg";s:5:"width";i:300;s:6:"height";i:300;s:9:"mime-type";s:10:"image/jpeg";s:9:"uncropped";b:0;}s:29:"woocommerce_gallery_thumbnail";a:4:{s:4:"file";s:31:"coming-soon-mlb-585-100x100.jpg";s:5:"width";i:100;s:6:"height";i:100;s:9:"mime-type";s:10:"image/jpeg";}s:12:"shop_catalog";a:4:{s:4:"file";s:31:"coming-soon-mlb-585-300x300.jpg";s:5:"width";i:300;s:6:"height";i:300;s:9:"mime-type";s:10:"image/jpeg";}s:14:"shop_thumbnail";a:4:{s:4:"file";s:31:"coming-soon-mlb-585-100x100.jpg";s:5:"width";i:100;s:6:"height";i:100;s:9:"mime-type";s:10:"image/jpeg";}}s:10:"image_meta";a:12:{s:8:"aperture";s:1:"0";s:6:"credit";s:0:"";s:6:"camera";s:0:"";s:7:"caption";s:0:"";s:17:"created_timestamp";s:1:"0";s:9:"copyright";s:0:"";s:12:"focal_length";s:1:"0";s:3:"iso";s:1:"0";s:13:"shutter_speed";s:1:"0";s:5:"title";s:0:"";s:11:"orientation";s:1:"0";s:8:"keywords";a:0:{}}}
    

    But that data is a bit deceiving as that same field also contains it has serialized data from woocommerce. And because woocommerce is installed, an upload to wordpress creates multiple version of the same image…thus me wanting to upload outside of WordPress. Only 1 file is necessary for this and I scale it according to the device, rather than create multiple files. (I am looking to stop this feature. That should help.)

    The question is, how do I upload this serialized data to the CMB2 system for the display.

    Plugin Contributor Michael Beckwith

    (@tw2113)

    The BenchPresser

    Chances are that it’s just reading the meta data for the given post that it’s associated with. I’ve never actually checked how the little preview is handled.

    I would be curious what this file field would save without all the extra wiring up and content redirecting, to see what could potentially be done for that mapping.

    For what it’s worth, I’ve unserialized the sample data above and this is what it results in:

    Array(
        [width] => 525
        [height] => 350
        [file] => 2019/05/coming-soon-mlb-585.jpg
        [sizes] => Array(
                [thumbnail] => Array(
                        [file] => coming-soon-mlb-585-150x150.jpg
                        [width] => 150
                        [height] => 150
                        [mime-type] => image/jpeg
                    )
                [medium] => Array(
                        [file] => coming-soon-mlb-585-300x200.jpg
                        [width] => 300
                        [height] => 200
                        [mime-type] => image/jpeg
                    )
                [woocommerce_thumbnail] => Array(
                        [file] => coming-soon-mlb-585-300x300.jpg
                        [width] => 300
                        [height] => 300
                        [mime-type] => image/jpeg
                        [uncropped] => 
                    )
                [woocommerce_gallery_thumbnail] => Array(
                        [file] => coming-soon-mlb-585-100x100.jpg
                        [width] => 100
                        [height] => 100
                        [mime-type] => image/jpeg
                    )
                [shop_catalog] => Array(
                        [file] => coming-soon-mlb-585-300x300.jpg
                        [width] => 300
                        [height] => 300
                        [mime-type] => image/jpeg
                    )
                [shop_thumbnail] => Array(
                        [file] => coming-soon-mlb-585-100x100.jpg
                        [width] => 100
                        [height] => 100
                        [mime-type] => image/jpeg
                    )
            )
        [image_meta] => Array(
                [aperture] => 0
                [credit] => 
                [camera] => 
                [caption] => 
                [created_timestamp] => 0
                [copyright] => 
                [focal_length] => 0
                [iso] => 0
                [shutter_speed] => 0
                [title] => 
                [orientation] => 0
                [keywords] => Array()
            )
    )
    
    Thread Starter dkurth

    (@dkurth)

    this just plum did not work. I will upgrade it using another technique later.

Viewing 15 replies - 1 through 15 (of 15 total)
  • The topic ‘Override File Field Upload’ is closed to new replies.