Support

Account

Home Forums Backend Issues (wp-admin) upload prefilter function get CPT name or slug

Helping

upload prefilter function get CPT name or slug

  • Hello

    I whish to get the cpt single name or slug as folder name for my upload filter.

    I tried this but still echo empty value :

    function dossier_documents_prefilter($errors, $file, $field) {
        
        
        if( !current_user_can('edit_pages') ) {
            $errors[] = 'alert message';
        }
    
        add_filter('upload_dir', 'dossier_documents');
        
    }
    
    add_filter('acf/upload_prefilter/key=field_61a0d766d288f', 'dossier_documents_prefilter', 10, 3 );
    
    function dossier_documents($param) {
    
        $current_queried_post_type = get_post_type( get_queried_object_id() );
        $slug = $current_queried_post_typej->labels->singular_name;
       
       // $slug = str_replace(' ', '_', strtolower($postType) );   
    
        $custom_dir = '/uploads/documents/'.$slug.''; // Echo my CPT name as folder name
        $param['path'] = WP_CONTENT_DIR . $custom_dir;
        $param['url'] = WP_CONTENT_URL . $custom_dir;
    
        return $param;
    }
    
    add_filter('acf/upload_prefilter/key=field_61a49554d6a33', 'documents_perso_prefilter',10, 3 );

    where is the problem ?

  • The post ID is submitted when the file is uploaded in $_POST[‘post_id’]

    
    $post = get_post(intval($_POST['post_id']));
    
Viewing 2 posts - 1 through 2 (of 2 total)

You must be logged in to reply to this topic.