Support

Account

Home Forums General Issues File Upload – Skip Media Library

Solving

File Upload – Skip Media Library

  • Hi,

    I have created a custom upload function that uploads files to specific folders based on a user being logged in;

    function gist_acf_upload_dir_prefilter($errors, $file, $field) {
        
        // Only allow editors and admins, change capability as you see fit
        if( !current_user_can('edit_pages') ) {
            $errors[] = 'Only Editors and Administrators may upload attachments';
        }
        
        // This filter changes directory just for item being uploaded
        add_filter('upload_dir', 'gist_acf_upload_dir');
        
    }
    
    add_filter('acf/upload_prefilter/key=field_633af0a27092b', 'gist_acf_upload_dir_prefilter', 10, 3 );
    
    function gist_acf_upload_dir($path_info) {
        
        // Set to whatever directory you want the ACF file field to upload to
        $category = get_the_ID();
         $path_info["path"] = $_SERVER['DOCUMENT_ROOT'] . "/wp-content/uploads/scanned-mail/" . $category . "/";
         $path_info["url"] = 'https://' . $_SERVER['SERVER_NAME'] . "/wp-content/uploads/scanned-mail/" . $category . "/";
    	 
    	 return $path_info;
        
    }

    Is there a way that I can have these files completely bypass the media library as they do not need to be in there as well as in their own folders?

    Edit: OR… have these files immediately deleted from the media library would be an acceptable workaround.

  • Exactly how will these files that are uploaded be used? How will you find them if there is no reference to them in the media library? Uploads/Media are a Post Type named “attachment”.

  • Files are uploaded via the file custom field inside a CPT and visible on the front end (see attached). There is no need for them to be seen within the media library.

  • Exactly what code is used to display them in the front end?

  • <div id="scanned-mail">
    
        <div class="grid-x grid-margin-x files">  
            
            <?php if ( $post->post_type == 'company' && $post->post_status == 'publish' ) {
            $attachments = get_posts( array(
                'post_type' => 'attachment',
                'posts_per_page' => -1,
                'post_parent' => $post->ID,
                'exclude'     => get_post_thumbnail_id()
            ) );
    
                if ( $attachments ) {
                    foreach ( $attachments as $attachment ) { ?>
    
                    <div class="cell medium-12 large-12 file b6">
                        
                        <?php $fileurl = wp_get_attachment_url( $attachment->ID ); $filename = get_the_title( $attachment->ID ); ?>
                        
                        <div class="file-type">
    
                            <i class="fa-solid fa-file-pdf"></i>
    
                        </div>
                        
                        <div class="file-name">
                            
                            <span><?php echo $filename; ?></span>
    
                        </div>
                        
                        <div class="file-size">                        
                                        
                            <span><?php $attachment_filesize = size_format( filesize( get_attached_file( $attachment->ID ) ), 2 ); echo $attachment_filesize; ?></span>
                            
                        </div>
                        
                        <div class="file-upload-date">
                            
                            <span><?php echo date('F d, Y H:i:s', strtotime($attachment->post_date)); ?></span>
                            
                        </div>
                        
                        <div class="file-expiry-date">
                            
                            <span><?php echo date('F d, Y H:i:s', strtotime($attachment->post_date. ' + 180 days')); ?></span>
                            
                           
                            
                        </div>
                        
                        <div class="file-actions">
                            
                            <ul>
                                
                                <li>
    
                                    <a href="<?php echo do_shortcode('[pda_private_link file_id="' . attachment_url_to_postid( $fileurl ) . '"]'); ?>" target="_blank"><i class="fa-duotone fa-eye"></i></a>
    
                                </li>
    
                                <li>
    
                                    <a href="<?php echo do_shortcode('[pda_private_link file_id="' . attachment_url_to_postid( $fileurl ) . '"]'); ?>" download><i class="fa-duotone fa-download"></i></a>
    
                                </li>
                            
                            </ul>
                            
                        </div>
                        
                    </div>
    
                    <?php }
    
                }
            }
            ?>
    
        </div>
        
    </div>
  • Maybe you are unaware of how media files work in WP.

    As you can see in your code the files are posts with a post type of attachment

    
    $attachments = get_posts( array(
                'post_type' => 'attachment',
                'posts_per_page' => -1,
                'post_parent' => $post->ID,
                'exclude'     => get_post_thumbnail_id()
            ) );
    

    every post that has a post type of attachment appears in the media library in WP. It would be impossible for you to display these files on the front of the site if they are deleted from the media library.

    It is possible to hide files from appearing in the media library: https://www.google.com/search?q=hide+files+from+the+media+library+in+wp

    https://wordpress.stackexchange.com/questions/271584/can-i-hide-the-attachments-from-media-which-i-uploaded-from-front-end

  • Actually i looking exactly for the opposite case:
    Is there a way i can prevent the frontend-user from selecting the upload function or let the user just choose from the media-gallery. Like preselecting the tab media-upload. Any hints?

  • When you create and image or gallery field you can select if they user can select from the entire media library or only images uploaded to the specific post.

Viewing 9 posts - 1 through 9 (of 9 total)

You must be logged in to reply to this topic.