Can I add custom fields and taxonomies to the document library?
Document Library Pro comes with all the standard WordPress fields, such as the document title, content, excerpt, featured image, and date. It also comes with 3 custom taxonomies: document categories, document tags, and document authors. It generates other information automatically, such as the file size and type.
You can use any WordPress custom fields or custom taxonomy plugin to create additional fields for your document library, and display them using the table or grid layout. This article explains how to do this.
Creating custom fields
Custom fields are for storing unique information about each document. Only use custom fields for data that you do not need to group or filter by, for example a document reference number, filename, version number or revision date.
- Create as many custom fields as you like using your chosen plugin, and enable it for the
dlp_document
post type. This will add it to the bottom of the Add/Edit Document page. - Go to the Add/Edit Document page and add information to the custom field for each document.
Creating custom taxonomies
Custom taxonomies provide extra ways of grouping your documents. For example, if you want to store the publisher or year for each document, so that users can filter by publisher, language or year, then you should add these as custom taxonomies.
- Create as many custom taxonomies as you like using your chosen plugin, and enable it for the
dlp_document
post type. This will add it to the right hand side of the Add/Edit Document page. - Go to the Add/Edit Document page and tag your documents with the relevant taxonomy terms.
Once created, your custom taxonomies will appear in the left of the WordPress admin under Documents menu item. You can add/edit/delete/restructure your taxonomies centrally from there.
Recommended plugins
You can use any plugin that allows you to create custom fields or taxonomies. Choose one based on which features you need. We recommend the following free plugins:
- Custom fields, taxonomies, and post types - Easy Post Types and Fields
We designed this plugin to simplify the process of creating and managing custom fields, taxonomies, and post types. With compatibility at its core, it is built to work seamlessly with all Barn2 plugins including Document Library Pro. - Custom fields only - Advanced Custom Fields
- Custom fields and taxonomies - Pods or Custom Post Type UI
Displaying custom fields and taxonomies in the table layout
If you are using the table layout for your documents, then you can easily create columns for custom fields and taxonomies. This is the easiest way to display custom fields and taxonomies in the document library, and does not require any code changes.
You can also list each taxonomy in a filter dropdown above the document library so that people can find documents more quickly.
Displaying custom fields and taxonomies in the grid layout
The document grid layout isn't designed to display custom fields and taxonomies because of the limited amount of space in the grid. However, if you would like to do this, then we have provided code snippets to help you with this. This is a developer-level task:
Custom fields
Display the custom field on the document grid cards using one of the available grid card hooks.
The example below illustrates adding this data below the title and before the excerpt. Use a different grid card hook to change the position.
The key of the custom field is custom_field
and should be replaced with the key of the custom field you want to display.
add_action( 'document_library_pro_grid_card_before_excerpt', function ( $document, $args, $post ) {
?>
<div class="dlp-grid-custom-field">
<span class="dlp-grid-custom-field-title"><?php _e( 'Custom Field: ', 'text-domain' ); ?></span>
<?php echo get_post_meta( $document->get_id(), 'custom_field', true ); ?>
</div>
<?php
} );
Custom taxonomies
Display a list of custom taxonomy terms on the document grid cards using one of the available grid card hooks.
The example below illustrates adding this data below the title and before the excerpt. Use a different grid card hook to change the position.
The id of the custom taxonomy is custom_taxonomy
and should be replaced with the id of the custom taxonomy you want to display.
add_action( 'document_library_pro_grid_card_before_excerpt', function ( $document, $args, $post ) { ?> <div class="dlp-grid-custom-tax"> <?php echo get_the_term_list( $document->get_id(), 'custom_taxonomy' ); ?> </div> <?php } );
Displaying custom fields and taxonomies on the single document page
The instructions above tell you how to create custom fields and taxonomies, and display them on the main document library pages.
If you want to display them on the individual page for each document then there are several ways to do this.
Shortcode method
Use the Document Library Pro shortcode to create a table which only lists the custom fields and/or taxonomies for that document. To do this, you need to use the following shortcode options:
- include - specify the ID of the document whose custom fields and taxonomies you want to display in the table.
- content - choose the columns to include in the table, including all the custom fields/taxonomies you are displaying.
- If your main document library page contains extra elements such as the search box, filters, etc., then you should probably use the shortcode options to hide these in the tables on the single document pages. This is because they're not needed when just displaying information about one document, so it's best to keep these tables simple.
For example: [doc_library layout="table" include="123" content="cf:reference,tax:publisher" search_box="false" reset_button="false" totals="false"]
The above shortcode will create a table listing the 'Reference' custom field and 'Publisher' taxonomy for a document with the ID 123, and hides the search box , reset button, and total number of documents. You need to modify the shortcode to add your own custom fields, taxonomies and document ID's.
Developer method
With some custom coding, you can also display them on the individual page for each document. Again, this is a developer-level task:
If you have already created a custom single document page template then you should add code to output your custom fields and taxonomies there. If you're not using a custom template for the single document page then you can adapt the code snippets below and add them to the functions.php file in your theme or child theme.
Custom fields
add_action( 'document_library_pro_single_document_details_list_after', function () { ?> <div class="dlp-document-info-custom-field"> <span class="dlp-document-info-title"><?php_e( 'Custom Field: ', 'text-domain' ); ?></span> <?php echo get_post_meta( get_the_ID(), 'custom_field', true ); ?> </div> <?php } );
Custom taxonomies
add_action( 'document_library_pro_single_document_details_list_after', function () { ?> <div class="dlp-document-info-custom-tax"> <span class="dlp-document-info-title"><?php_e( 'Custom Taxonomy: ', 'text-domain' ); ?></span> <?php echo get_the_term_list( get_the_ID(), 'custom_taxonomy' ); ?> </div> <?php } );