Skip Navigation

[Resolved] Displaying type and size of media post in views

This thread is resolved. Here is a description of the problem and solution.

Problem:

I'm developing a view that I want to display a series of PDF documents from the WordPress media library. I've created a view that selects content from the Media post types, including some custom fields I've added to the Media post type, and everything works, but I'm unable to get a few pieces of data related to the media item that I need, specifically, the file type and the file size.

Solution:

There isn't such a built-in feature within Toolset, but you can create a custom shortcode to get those size and file type information, for example:

https://toolset.com/forums/topic/displaying-type-and-size-of-media-post-in-views/#post-1147002

Relevant Documentation:

This support ticket is created 5 years, 5 months ago. There's a good chance that you are reading advice that it now obsolete.

This is the technical support forum for Toolset - a suite of plugins for developing WordPress sites without writing PHP.

Everyone can read this forum, but only Toolset clients can post in it. Toolset support works 6 days per week, 19 hours per day.

Sun Mon Tue Wed Thu Fri Sat
- 9:00 – 13:00 9:00 – 13:00 9:00 – 13:00 9:00 – 13:00 9:00 – 13:00 -
- 14:00 – 18:00 14:00 – 18:00 14:00 – 18:00 14:00 – 18:00 14:00 – 18:00 -

Supporter timezone: Asia/Hong_Kong (GMT+08:00)

This topic contains 2 replies, has 2 voices.

Last updated by davidL-7 5 years, 5 months ago.

Assisted by: Luo Yang.

Author
Posts
#1146945
media-file-type-size.jpg

Hi, I'm developing a view that I want to display a series of PDF documents from the WordPress media library. I've created a view that selects content from the Media post types, including some custom fields I've added to the Media post type, and everything works, but I'm unable to get a few pieces of data related to the media item that I need, specifically, the file type and the file size. I know WordPress knows what this data is (see attached screenshot), but I can't figure out how to pull it. I've reviewed the support forums and the WordPress codex, and I suspect I need to write a function, but can't quite figure it out.

Any ideas? Here's what I'm trying to produce: hidden link (note the file size and type in the link text after the name of the PDF).

Thanks in advance for the help!

#1147002

Hello,

There isn't such a built-in feature within Toolset, but you can create a custom shortcode to get those size and file type information, for example:
1) Create a custom shortcode [pdf-file-meta], add below codes into your theme file functions.php:

add_shortcode('pdf-file-meta', function($atts){
	$atts = shortcode_atts( array(
		'attachment_id' => get_the_ID(),
		'meta_name' => 'type',
	), $atts );
	
	$res = '';
	$file = get_attached_file( $atts['attachment_id'] );
	
	switch($atts['meta_name']){
		case 'type':
			if ( preg_match( '/^.*?\.(\w+)$/', $file, $matches ) ) {
				$res = esc_html( strtoupper( $matches[1] ) );
			}
			break;
		case 'size':
			$res = size_format(filesize( $file ));
			break;
	}
	return $res;
});

2) Use the custom shortcode, like this:

[pdf-file-meta meta_name="type"] ,  [pdf-file-meta meta_name="size"]
#1148122

That worked! I really appreciate the help, I would have spent hours trying to write that script myself!! I hope this will be useful to others. Thanks again!

This ticket is now closed. If you're a WPML client and need related help, please open a new support ticket.