New Feature: Earn 12 Badges
Collect up to 12 exclusive badges by sharing your plugin collections and engaging with the community.
See all Badgesby Mizuho Ogino
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.
*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 ''.wp_get_attachment_image ( $thumbnail_id, 'medium' ).'';
}
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.
ID );
if( $pdfs ): foreach( $pdfs as $pdf ):
$thumbnail_id = get_post_thumbnail_id( $pdf->ID );
if( $thumbnail_id ):
echo '';
echo '';
echo wp_get_attachment_image ( $thumbnail_id, 'medium' );
echo '';
echo ' ';
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 ''.get_the_post_thumbnail().''."n";
}
The plugin allows you to insert 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
'' .
do_shortcode( $content ). '';
}
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 = '' .$attach_output. '';
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 = '';
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' );
All texts and images on this product page are protected by copyright and are the property of the author Mizuho Ogino. You will be redirected to the retailer to download the plugin. We act solely as a search engine for plugins and are not affiliated with the retailer or Mizuho Ogino.
Get your first Badge
Earn your first badge by sharing your collections. Every master was once a beginner. Welcome to your coding journey.
Click the symbol on the desired plugin to create a collection. The symbol appears when you hover over the plugin.