Custom Fields
Re-Envisioned
Support Meta Box Group File field not force deleting

  • Creator
    Topic
  • #16945
    jcleaveland
    Participant

    Hi,

    I have a file field within a group within a tab with a custom upload directory. The file uploads to the custom directory correctly.

    I have force_delete set to true and it does not work when set to the custom directory or the regular uploads directory. It throws a javascript alert: Error: Invalid file. No error in the console. Any ideas?

                  array(
                    'id' => 'file_link',
                    'type' => 'file',
                    'name' => esc_html__( 'Upload a File' ),
                    'tooltip' =>  esc_html__( 'Select a file from your computer to upload and save as a material for this lesson' ),
                    'force_delete' => true,
                    'max_file_uploads' => 1,
                    'upload_dir' => $upload_dir['basedir'] . '/lms-materials/',
                    'visible' => array( 'type', '=', 'document' )
                  ),
Viewing 4 replies - 1 through 4 (of 4 total)
  • Author
    Replies
  • #16946
    jcleaveland
    Participant

    Here is the HTML as rendered in the browser:

    
        
            Upload a File
        
    
        
            <ul>
                <li>
                    
                        <a href="https://dev-1.obodohub.com/obh-uploads/sites/49/lms-materials/CtS_A1_1.2.jpg" target="_blank" rel="noopener noreferrer">
                            <img width="48" height="64" src="https://dev-1.obodohub.com/obh-includes/images/media/default.png" alt="">
                        </a>
                    
    
                    
                        <a href="https://dev-1.obodohub.com/obh-uploads/sites/49/lms-materials/CtS_A1_1.2.jpg" target="_blank" rel="noopener noreferrer">CtS_A1_1.2</a>
    
                        CtS_A1_1.2.jpg
    
                        
                            <a href="#">
                                Delete
                            </a>
                        
                    
    
                    <input type="hidden" name="materials[material][0][file_link][0]" value="https://dev-1.obodohub.com/obh-uploads/sites/49/lms-materials/CtS_A1_1.2.jpg">
                </li>
            </ul>
            
            
                
            
            
            <input type="hidden" class="rwmb-file-index" name="materials[material][0][_index_file_link]" value="_file_5dce2d87a4c73">
        </div>
    
        
    
    </div>

    Thanks!

    #16985
    Anh Tran
    Keymaster

    Hi Jason,

    Thanks for your feedback. I’ve checked the code and see that the plugin doesn’t support force deleting a file in groups. The reason is that it’s impossible to get the field settings via Ajax (which is used to get the uploaded files to make sure it deletes the correct file – for security reason).

    #16988
    jcleaveland
    Participant

    Hi Anh,

    I understand the issue. Can you think of a workaround?

    I’m using the custom table extension to save the data, so would rwmb_{$field_id}_after_save_field give me the old data on save_post?

    If so, I could compare the old group array to new and if the file url is either not present or not the same, I can parse the old url to find and delete the file that way. Kind of a hack, but should work if that old data is present in the hook.

    The only other thing I’ll need to do is silence that alert 🙂

    Thanks for the help!

    #17019
    Anh Tran
    Keymaster

    I think hook to the after_save_field will work. As it’s PHP, you can compare the group array and delete the file.

    To disable the alert, I think the best way is to hide it with CSS!

Viewing 4 replies - 1 through 4 (of 4 total)
  • You must be logged in to reply to this topic.