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 User name column to the 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;
}

Ex: Add a new file description column to the Display Fields option

  1. Get this snippet and paste it into Code Snippets plugin to show the file description column for your document gallery
// 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' => __( 'Description', 'catfolders-document-gallery' ),
        'key'   => 'description',
    );
    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 $column 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( $column, $file, $attr ) {
	$document_id = isset($file['document_id']) ? $file['document_id'] : "";
	
	if (empty($document_id)){
		return;
	}

    // Content of column
    if ( 'description' === $column['key'] ) {
        ?>
        <td>
            <div class=""><?php echo esc_html( get_post_field( 'post_content', $document_id ) ); ?></div>
        </td>
        <?php
    }

    return $column;
}

2. Once done, check your document gallery; you can see a new column has been added successfully.

show the file description column in the CatFolders Documemt Gallery
Was this article helpful to you? No Yes

How can we help?