Skip to content

Actions and Filters

Jim Barnes edited this page Feb 21, 2020 · 2 revisions

This plugin includes the following action and filter hooks, which developers can utilize in a theme or other plugin to override specific markup or functionality.

Navigation

Custom Post Type Hooks

Custom Taxonomy Hooks


ucf_document_label_defaults

Filters the default labels used to build the specific labels used when registering the document custom post type.

Parameters

  • $defaults {array}

The $defaults array holds the default label pieces which includes the following values:

  • $defaults['singular'] : The singular name of the post type. Default: 'Document'
  • $defaults['plural'] : The plural name of the post type. Default: 'Documents'
  • $defaults['text_domain']: The text domain used for translation functions. Default: ucf_document.

Adjusting these values can effect a large number of arrays. For example, if the plural value was updated to "Files", the search_items label would be updated to "Search Files" and the filter_items_list label would be updated to "Filter files list".

Note: An array containing all 3 indexes originally passed in must always be returned.

function update_document_label_defaults( $defaults ) {
    $defaults['plural'] = 'Files';

    return $defaults;
}

add_filter( 'ucf_document_label_defaults', 'update_document_label_defaults', 10, 1 );

ucf_document_labels

This filter allows for specific labels to be overridden, instead of overriding the templating scheme as is done in the above filter. For example, the specific label search_items could be updated to any value.

Parameters

  • $labels {array} The array of labels.

This will include all the labels in this list. To override a specific label, just update the specific index:

function update_document_labels( $labels ) {
    $labels['not_found'] = 'The Document was not found.';

    return $labels;
}

add_filter( 'ucf_document_labels', 'update_document_labels' , 10, 1 );

Note: An array with the original indexes must always be returned.

ucf_document_taxonomies

This filter controls which registered taxonomies will be assigned to the document custom post type.

Parameters

  • $taxonomies {array} The array of taxonomies.

For example, post categories could be added using the following:

function update_document_taxonomies( $taxonomies ) {
    $taxonomies[] = 'category';

    return $taxonomies;
}

add_filter( 'ucf_document_taxonomies', 'update_document_taxonomies', 10, 1 );

Note: An array must always be returned by this filter. The array can be empty.

ucf_document_args

Allows for the modification of any of the args passed into the register_post_type function.

Parameters

  • $args {array} The array of arguments.

For more information on the arguments that can be overridden, see the $args parameter in the register_post_type documentation.

For example, to move the menu position of documents higher in the sidebar menu in wp-admin, you could to the following:

function update_document_args( $args ) {
    $args['menu_position'] = 4;

    return $args;
}

add_filter( 'ucf_document_args', 'update_document_args', 10, 1 );

Note: This function must always return the original array to avoid creating problems with the post type registering.

ucf_document_fields

Allows for the modification of the ACF fields included by default with this plugin.

Parameters

  • $fields {array} The array of ACF Field values

For example, to add a usage description field for documents, you could do the following:

function updated_document_fields( $fields ) {
    $fields[] = array(
        'key'          => 'document_usage_description',
        'label'        => 'Usage Description',
        'name          => 'document_usage_description',
        'type'         => 'textarea',
        'required'     => 0,
        'instructions' => 'Describe the allowed usage of this document.'
    );

    return $fields;
}

add_filter( 'ucf_document_fields', 'update_document_fields', 10, 1 );

For more information on registering ACF fields, see how to Register fields via PHP on the ACF website.

Note: The original $fields array must be returned by the function to keep the included fields.

ucf_document_append_post_meta

This plugin automatically appends the meta data for each document onto the $post object when the object is queried. Additional meta fields can be added using this hook.

Parameters

  • $meta {array} The array of meta added by default
  • $post_id {int} The ID of the post

For example, to add the document_usage_description field we added above to the meta array, we could add the following:

function update_append_post_meta( $meta, $post_id ) {
    $meta['usage_description'] = get_field( 'document_usage_description', $post_id );

    return $meta;
}

add_filter( 'ucf_document_append_post_meta', 'update_append_post_meta', 10, 2 );

Note: The original $meta array must always be returned in order to have access to the default meta that is attached.

ucf_directory_label_defaults

Much like the ucf_document_label_defaults hook above, this hook allows for the template parts that are used to generate the directory taxonomy labels to be modified.

Parameters

  • $labels {array} The labels array to be modified.

ucf_directory_labels

ucf_directory_args