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.
There are also lots of ways to add extra fields to the document library. You can add extra fields as either custom fields or taxonomies. These article explains what they are, and how to add them.
What are custom fields and taxonomies?
Custom fields
- Purpose - Adding extra data which will be unique to each document.
- Examples - Document reference number, filename, version number or revision date.
- How to add them - Document Library Pro has a built-in feature to add custom fields, or you can add more advanced custom field types using a free WordPress custom fields plugin. See below for details.
Custom taxonomies
- Purpose - Adding extra data which you will use to group and filter your documents.
- An example - You might create custom taxonomies for Publisher, Language and Year, and then add filters for Publisher, Language and Year above your document library table.
- How to add them - There are several free plugins that you can use to add custom taxonomies. See below for details.
Creating custom fields
Built-in method
Document Library Pro has a built-in method for adding custom fields to your documents. This uses the custom fields which are part of WordPress itself.
To use this feature:
- Enable custom fields in the Document Fields option on the plugin settings page.
- Go to the Add/Edit Document page (Documents → Add New) and scroll down to the custom fields section at the bottom.
- Add the custom field data for each document as needed:
Plugin method
Alternatively, you can add custom fields to the 'Documents' post type using any WordPress custom fields plugin. We particularly recommend Easy Post Types and Fields, which is a free Barn2 plugin which we designed to work with Document Library Pro. However, other plugins such as Advanced Custom Fields and Pods are fine too.
After adding custom fields to the 'Documents' post type, you will see them at the bottom of the 'Add/Edit Documents' screen for each document. You can then add the custom field data to each document as needed.
Creating custom taxonomies
You can add custom taxonomies to the 'Documents' post type using any WordPress taxonomy plugin. We recommend Easy Post Types and Fields because it is a free Barn2 plugin which we have designed to work alongside Document Library Pro. You can also use Pods or Custom Post Type UI.
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, and also from the right hand side of the Add/Edit Document page.
Displaying custom fields and taxonomies
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.
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 and if you don't know how to implement it then you can use our plugin customization service.
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
}, 10, 3 );
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
}, 10, 3 );
Single document page
You can enable custom fields in the single document page settings. This will display them as text in the right hand side of the document page in the front end.
By default all custom fields are shown, but if you want to specify which fields to show, you can use the following filter:
add_filter( 'document_library_pro_custom_fields', function( $custom_fields_list, $post_id ) {
// Delete an unwanted custom field from the custom fields list.
unset( $custom_fields_list['my_custom_field_slug'] );
return $custom_fields_list;
}, 10, 2 );
For more advanced ways to display custom fields and taxonomies, there are several other methods you can use:
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
} );