By uploading a PDF attachment, this plugin convert the cover page to jpeg and attach it as a post thumbnail file. It also allows displaying a thumbnail icon and inserting PDF link with a cover image into the editor.
This plugin hooks to the media editor and generates the first page image of PDF by using ImageMagick with GhostScript. It requires no setup, just activate the plugin via the admin install page. Allow to set a PDF as an image.
This Plugin replaces and extends the following features.
A generated image file is registered as post children of a PDF file and it has different size variations. Files build a tree like below.
Select a PDF file in the Media Uploader and insert it into the the editor. An output HTML is automatically rewritten like below.
<a class="link-to-pdf" title="dummy-pdf" href="http://exmaple.com/wp-content/uploads/2015/01/dummy-pdf.pdf" target="_blank"><img width="227" height="320" class="size-medium wp-image-9999 thumb-of-pdf" src="http://exmaple.com/wp-content/uploads/2015/01/dummy-pdf-pdf-227x320.jpg" alt="thumbnail-of-dummy-pdf" /></a>
*If you are using a document viewer plugin (like GDE) and want insert html which is made by it, select “Default (Title)” of “media” selector.
A generated image ID is stored in [‘_thumbnail_id’] custom field of a PDF attachment post. An image ID is exportable by using get_post_thumbnail_id($your_pdf_id) or get_post_meta($your_pdf_id, ‘_thumbnail_id’, true) functions in a template file.
$pdf_id = 'your PDF file ID'; if ( $thumbnail_id = get_post_thumbnail_id( $pdf_id ) ) { echo '<a class="pdf-link image-link" href="'.wp_get_attachment_url( $pdf_id ).'" title="'.esc_attr( get_the_title( $pdf_id ) ).'" target="_blank">'.wp_get_attachment_image ( $thumbnail_id, 'medium' ).'</a>'; }
Or more simply, you can call images and link to PDF files by wp_get_attachment_image($your_pdf_id, $size) and wp_get_attachment_link($your_pdf_id, $size) functions. (v1.2 or later) *If the plugin is deactivated, this function just will return empty.
$pdf_id = 'your PDF file ID'; echo wp_get_attachment_image ( $pdf_id, 'medium' ); echo wp_get_attachment_link ( $pdf_id, 'medium' );
If you call all PDFs that are attached to the post. You can get them by get_posts function.
Examples.
Dynamically get PDFs that are attached to the post.
<?php $pdfs = get_posts( 'posts_per_page=-1&post_type=attachment&post_mime_type=application/pdf&post_parent='.$post->ID ); if( $pdfs ): foreach( $pdfs as $pdf ): $thumbnail_id = get_post_thumbnail_id( $pdf->ID ); if( $thumbnail_id ): echo '<li>'; echo '<a href="'wp_get_attachment_url( $pdf->ID )'" class="link-to-pdf">'; echo wp_get_attachment_image ( $thumbnail_id, 'medium' ); echo '</a>'; echo '</li>'; endif; endforeach; endif; ?>
The plugin support you to set PDF as Featured Image. (v1.2 or later)
Strictly explaining, when you set a PDF as Featured Image, the plugin automatically set the thumbnail which is registered with this PDF.
You can call thumbnail by using get_the_post_thumbnail function.
echo get_the_post_thumbnail ( $post->ID, 'medium' );
May you want pdf file from the post thumbnail.
$thumb_id = get_post_thumbnail_id ( $post->ID ); $pdf_id = get_post( $thumb_id )->post_parent; if ( $pdf_id && get_post_mime_type ( $pdf_id ) === 'application/pdf' ){ $pdf = get_post($pdf_id); echo '<a class="link-to-pdf" href="'.wp_get_attachment_url($pdf_id).'" title="'.esc_html($pdf->post_title).'" target="_blank">'.get_the_post_thumbnail().'</a>'."\n"; }
The plugin allows you to insert [caption] short-code into the post content area as when insert an image. If you want to display attachment title, description, file type, file size and so on, use img_caption_shortcode filter in your functions.php.
Here’s an example code…
function add_attachment_data_in_caption( $empty, $attr, $content ) {
$attr = shortcode_atts( array( ‘id’=>”, ‘align’=>’alignnone’, ‘width’=>”, ‘caption’=>” ), $attr );
if ( 1 > (int) $attr[‘width’] || empty( $attr[‘caption’] ) ) return ”;
if ( $attr[‘id’] ) {
$attr[‘id’] = ‘id=”‘ . esc_attr( $attr[‘id’] ) . ‘” ‘;
$attachment_id = explode(‘_’, $attr[‘id’]);
$attachment_id = $attachment_id[1];// get attachment id
if( get_post_mime_type ( $attachment_id ) === ‘application/pdf’ ){
$attachment = get_post( $attachment_id );
$bytes = filesize( get_attached_file( $attachment->ID ) );
if ($bytes >= 1073741824) $bytes = number_format($bytes / 1073741824, 2). ‘ GB’;
elseif ($bytes >= 1048576) $bytes = number_format($bytes / 1048576, 2). ‘ MB’;
elseif ($bytes >= 1024) $bytes = number_format($bytes / 1024, 2). ‘ KB’;
elseif ($bytes > 1) $bytes = $bytes. ‘ bytes’;
elseif ($bytes == 1) $bytes = $bytes. ‘ byte’;
else $bytes = ‘0 bytes’;
$attr[‘caption’] =
‘title : ‘ .$attachment->post_title. ‘
‘ . // title
‘caption : ‘ .$attr[‘caption’]. ‘
‘ .// caption
‘size : ‘ .$bytes. ‘
‘ . // file size
‘filetype : ‘ .get_post_mime_type ( $attachment_id ). ‘
‘ . // file type
‘description : ‘ .$attachment->post_content. ‘
‘; // description
}
}
return '<div ' .$attr['id']. 'class="wp-caption ' .esc_attr( $attr['align'] ). '" style="max-width: ' .( 10 + (int) $attr['width'] ). 'px;">' . do_shortcode( $content ). '<p class="wp-caption-text">' .$attr['caption']. '</p>' . '</div>'; } add_filter('img_caption_shortcode', 'add_attachment_data_in_caption', 10, 3);
You can generate thumbnails of any already-uploaded PDFs.
Activate the plugin and Click “Generate Thumbnails Now” link in “settings”. (v1.3.3 or later)
function pigen_filter_attachment_link ( $html, $attach_id, $attach_url, $attach_output ){ $attach_title = get_the_title( $attach_id ); $html = '<a class="link-to-pdf" href="'.$attach_url.'" rel="attachment wp-att-' .esc_attr($attach_id). '" title="'.esc_attr( $attach_title ).'" target="_blank">' .$attach_output. '</a>'; return $html; }; add_filter( 'pigen_filter_attachment_link', 'pigen_filter_attachment_link', 10, 4 );
function pigen_filter_attachment_output ( $attach_output, $thumbnail_id, $thumbnail, $size, $align ){ $attach_output = '<img src="'. $thumbnail[0] .'" alt="'.get_post_meta( $thumbnail_id, '_wp_attachment_image_alt', true ).'" width="'. $thumbnail[1] .'" height="'. $thumbnail[2] .'" class="'.( $align ? 'align' .$align. ' ' : '' ). 'size-' .esc_attr( $size ). ' wp-image-'.$thumbnail_id.'" />'; return $attach_output; }; add_filter( 'pigen_filter_attachment_output', 'pigen_filter_attachment_output', 10, 5 );
Filters allow you to add your own modify imageMagick’s behaviour by hooking.
Sample usage for imageMagick user.
function pigen_filter_convert_file_basename( $file_basename ){ $file_basename = str_replace( '.jpg', '.png', $file_basename ); return $file_basename; }; add_filter( 'pigen_filter_convert_file_basename', 'pigen_filter_convert_file_basename' ); function pigen_filter_convert_imageMagick( $imageMagick, $before_name, $after_name, $max_width, $max_height ){ $imageMagick = "convert -density 150 -quality 80 -background black -flatten {$max_width}x{$max_height} {$before_name} {$after_name}"; return $imageMagick; }; add_filter( 'pigen_filter_convert_imageMagick', 'pigen_filter_convert_imageMagick', 10, 5 );
Sample usage for imagick extension user.
function pigen_filter_convert_file_basename( $file_basename ){ $file_basename = str_replace( '.jpg', '.png', $file_basename ); return $file_basename; }; add_filter( 'pigen_filter_convert_file_basename', 'pigen_filter_convert_file_basename' ); function pigen_filter_convert_imagick( $imagick ){ $imagick->setImageBackgroundColor( 'black' ); $imagick->setCompressionQuality( 80 ); $imagick->setImageFormat( 'png' ); return $imagick; }; add_filter( 'pigen_filter_convert_imagick', 'pigen_filter_convert_imagick' );
Automatically set PDF thumbnail as featured image.
function save_pdf_thumb_as_featuredimage ( $post_id ) { if ( wp_is_post_revision( $post_id ) ) return; if ( get_post_type( $post_id ) !== 'post' ) return; // set your post type if ( get_post_meta( $post_id, '_thumbnail_id', true ) ) return; // post already has featured image $attaches = get_posts ( 'post_parent='.$post_id.'&numberposts=-1&post_type=attachment&post_mime_type=application/pdf&orderby=menu_order&order=ASC' ); if ( $attaches ): foreach( $attaches as $attach ): if ( $thumb_id = get_post_meta( $attach->ID, '_thumbnail_id', true ) ){ // if pdf has thumbnail update_post_meta( $post_id, '_thumbnail_id', $thumb_id ); break; } endforeach; endif; } add_action( 'save_post', 'save_pdf_thumb_as_featuredimage' );
Automatically set first image/PDF as featured image.
function save_thumbnail_as_featuredimage ( $post_id ) { if ( wp_is_post_revision( $post_id ) ) return; if ( get_post_type( $post_id ) !== 'post' ) return; // set your post type if ( get_post_meta( $post_id, '_thumbnail_id', true ) ) return; // post already has featured image $args = array( 'post_parent' => $post_id, 'post_type' => 'attachment', 'numberposts' => -1, 'post_status' => null, 'orderby' => 'menu_order date', 'order' => 'ASC ASC' ); $attaches = get_posts($args); if ( $attaches ): foreach( $attaches as $attach ): if ( $attach->post_mime_type == 'application/pdf' ){ if ( $thumb_id = get_post_meta( $attach->ID, '_thumbnail_id', true ) ){ // if pdf has thumbnail update_post_meta( $post_id, '_thumbnail_id', $thumb_id ); break; } } elseif ( preg_match("/^image\//", $attach->post_mime_type ) ) { update_post_meta( $post_id, '_thumbnail_id', $attach->ID ); break; } endforeach; endif; } add_action( 'save_post', 'save_thumbnail_as_featuredimage' );
Starting from $0 per month.
Rating
Reviewers
20 reviews
Tags
Developed By
Mizuho Ogino
Quick & Easy
Common Ninja has a large selection of powerful Wordpress plugins that are easy to use, fully customizable, mobile-friendly and rich with features — so be sure to check them out!
Testimonial plugins for Wordpress
Galleries plugins for Wordpress
SEO plugins for Wordpress
Contact Form plugins for Wordpress
Forms plugins for Wordpress
Social Feeds plugins for Wordpress
Social Sharing plugins for Wordpress
Events Calendar plugins for Wordpress
Sliders plugins for Wordpress
Analytics plugins for Wordpress
Reviews plugins for Wordpress
Comments plugins for Wordpress
Portfolio plugins for Wordpress
Maps plugins for Wordpress
Security plugins for Wordpress
Translation plugins for Wordpress
Ads plugins for Wordpress
Video Player plugins for Wordpress
Music Player plugins for Wordpress
Backup plugins for Wordpress
Privacy plugins for Wordpress
Optimize plugins for Wordpress
Chat plugins for Wordpress
Countdown plugins for Wordpress
Email Marketing plugins for Wordpress
Tabs plugins for Wordpress
Membership plugins for Wordpress
popup plugins for Wordpress
SiteMap plugins for Wordpress
Payment plugins for Wordpress
Coming Soon plugins for Wordpress
Ecommerce plugins for Wordpress
Customer Support plugins for Wordpress
Inventory plugins for Wordpress
Video Player plugins for Wordpress
Testimonials plugins for Wordpress
Tabs plugins for Wordpress
Social Sharing plugins for Wordpress
Social Feeds plugins for Wordpress
Slider plugins for Wordpress
Reviews plugins for Wordpress
Portfolio plugins for Wordpress
Membership plugins for Wordpress
Forms plugins for Wordpress
Events Calendar plugins for Wordpress
Contact plugins for Wordpress
Comments plugins for Wordpress
Analytics plugins for Wordpress
Common Ninja Apps
Browse our extensive collection of compatible plugins, and easily embed them on any website, blog, online store, e-commerce platform, or site builder.
Bring Focus to Products & Increase Conversions With Product Blobs
Display Vimeo Videos in a Stylish and Interactive Scrolling Slider
Boost Confidence & Enhance Reliability for Higher Sales
Showcase Videos, Enhance Design & Increase Conversions
Create Stunning Blogger Feeds & Improve User Experience
Present Data Effectively & Convert Users With Elegant Charts & Graphs
Increase Trust & Improve Credibility To Drive Sales Up. The service is not endorsed, sponsored, or provided by Amazon.
Create Stunning TikTok Feeds & Improve User Experience
Bringing Your Business Locations Closer to Your Site Visitors
Connect instantly with visitors via our customizable Call Button
Enhance Design & Draw Attention to Videos
Floating pop-ups offering endless creative possibilities
More plugins
The Common Ninja Search Engine platform helps website builders find the best site widgets, apps, plugins, tools, add-ons, and extensions! Compatible with all major website building platforms - big or small - and updated regularly, our Search Engine tool provides you with the business tools your site needs!