Snippets

Documentation Search

  1. Home
  2. Addons
  3. Document Gallery
  4. Snippets

Snippets

Customize the appearance of your CatFolders Document Library with ease by adding PHP snippets.

Ex: Add a new column into Display Fields option.

1. Create a custom code and paste it into a Code Snippets plugin.

Add new column using Code Snippets plugin

2. Here is the result, the “User name” column has been added successfully.

Displaying new column on document gallery using Code Snippets

Following this process can lead you to your personalized job.

Get this snippet here:

// The hook used to add a new column
add_filter( 'catf_dg_columns', 'catf_dg_add_column', 10, 1 );

/**
 * Add a new column
 *
 * @param array $columns An array used to define label and key of column
 * @return array
 */
function catf_dg_add_column( $columns ) {
    // Add label and key for new column here
    $columns[] = array(
        'label' => __( 'User name', 'catfolders-document-gallery' ),
        'key'   => 'user_name',
    );
    return $columns;
}

// The hook used to add content of column
add_filter( 'catf_dg_columns_html', 'catf_dg_add_column_html', 10, 3 );

/**
 * Add content of columns
 *
 * @param array $columns Label and key of column
 * @param array $file Info of file
 * @param array $attr Attributes of table
 * @return array
 */
function catf_dg_add_column_html( $columns, $file, $attr ) {
    $id = get_the_ID();
    $author_id = get_post_field( 'post_author', $id );
    $display_name = get_the_author_meta( 'display_name', $author_id );

    // Content of column
    if ( 'user_name' === $columns['key'] ) {
        ?>
        <td>
            <div class=""><?php echo esc_html( $display_name ); ?></div>
        </td>
        <?php
    }

    return $columns;
}
Was this article helpful to you? No Yes

How can we help?