Skip to content

Instantly share code, notes, and snippets.

@yratof
Created April 23, 2013 10:59
Show Gist options
  • Save yratof/5442649 to your computer and use it in GitHub Desktop.
Save yratof/5442649 to your computer and use it in GitHub Desktop.
PDF in a widget that has a thumbnail of the front page
// THIS IS A RISK. CONVERT PDF's TO JPG FOR THUMBNAILS.
// Seriously hope it works
function load_pmg_single_doc() {
register_widget('PMG_Single_Doc');
}
add_action('widgets_init', 'load_pmg_single_doc');
class PMG_Single_Doc extends WP_Widget {
function PMG_Single_Doc() {
$widget_ops = array('classname' => 'widget_text', 'description' => __('A widget that displays download links to a document of your choosing.'));
$control_ops = array('width' => 400, 'height' => 350);
$this->WP_Widget('PMG_Single_Doc', 'Single Document Widget', $widget_ops, $control_ops );
}
function widget( $args, $instance ) {
extract($args);
$title = apply_filters( 'widget_title', empty( $instance['title'] ) ? '' : $instance['title'], $instance, $this->id_base );
$url = apply_filters( 'widget_url', empty( $instance['url'] ) ? '' : $instance['url'], $instance );
$desc = apply_filters( 'widget_text', empty( $instance['desc'] ) ? '' : $instance['desc'], $instance );
$alldocslink = apply_filters( 'widget_text', empty( $instance['alldocslink'] ) ? '' : $instance['alldocslink'], $instance );
$alldocstext = apply_filters( 'widget_text', empty( $instance['alldocstext'] ) ? '' : $instance['alldocstext'], $instance );
$before_widget = preg_replace('/class="(.*?)"/i', 'class="$1 downloadsWidget"', $before_widget);
echo $before_widget;
if ( !empty( $title ) ) { echo $before_title . $title . $after_title; } ?>
<?php
if ( $url ) {
$pdf = $url;
$info = pathinfo($pdf);
$filename = basename($pdf,'.'.$info['extension']);
$uploads = wp_upload_dir();
$file_path = str_replace( $uploads['baseurl'], $uploads['basedir'], $url );
$dest_path = str_replace( '.pdf', '.jpg', $file_path );
$dest_url = str_replace( '.pdf', '.jpg', $pdf );
if ( !file_exists( $dest_path ) ) {
$myurl = $file_path.'[0]';
$image = new Imagick($myurl);
$image->setResolution( 300, 300 );
$image->setImageFormat( "png" );
$image->writeImage($dest_path);
};
?>
<div class="entry">
<div class="widgetImg">
<p><a href="<?php echo $url; ?>" title="<?php echo $filename; ?>"><?php echo "<img src='".$dest_url."' alt='".$filename."' class='blueBorder' />"; ?></a></p>
</div>
<div class="widgetText">
<?php echo wpautop( $desc ); ?>
<p><a class="downloadLink" href="<?php echo $url; ?>" title="<?php echo $filename; ?>"><?php echo $filename; ?></a></p>
<?php if ( $alldocslink ) { ?>
<p style="font-size:12px;"><a href="<?php echo $alldocslink; ?>" title="<?php if ( $alldocstext ) { echo $alldocstext; } else { ?>More Brochures<?php } ?>"><?php if ( $alldocstext ) { echo $alldocstext; } else { ?>More Brochures<?php } ?></a></p><?php } ?>
</div>
</div>
<?php }
?>
<?php
echo $after_widget;
}
function update( $new_instance, $old_instance ) {
$instance = $old_instance;
$instance['title'] = strip_tags($new_instance['title']);
$instance['url'] = strip_tags($new_instance['url']);
$instance['alldocslink'] = strip_tags($new_instance['alldocslink']);
$instance['alldocstext'] = strip_tags($new_instance['alldocstext']);
if ( current_user_can('unfiltered_html') )
$instance['desc'] = $new_instance['desc'];
else
$instance['desc'] = stripslashes( wp_filter_post_kses( addslashes($new_instance['desc']) ) ); // wp_filter_post_kses() expects slashed
$instance['filter'] = isset($new_instance['filter']);
return $instance;
}
function form( $instance ) {
$instance = wp_parse_args( (array) $instance, array( 'title' => '', 'desc' => '', 'url' => '', 'alldocslink' => '', 'alldocstext' => '' ) );
$title = strip_tags($instance['title']);
$url = strip_tags($instance['url']);
$alldocslink = strip_tags($instance['alldocslink']);
$alldocstext = strip_tags($instance['alldocstext']);
$desc = esc_textarea($instance['desc']);
?>
<p><label for="<?php echo $this->get_field_id('title'); ?>"><?php _e('Title:'); ?></label>
<input class="widefat" id="<?php echo $this->get_field_id('title'); ?>" name="<?php echo $this->get_field_name('title'); ?>" type="text" value="<?php echo esc_attr($title); ?>" /></p>
<p><label for="<?php echo $this->get_field_id('url'); ?>"><?php _e('URL of PDF attachment <em>(must be PDF)</em>:'); ?></label>
<input class="widefat" id="<?php echo $this->get_field_id('url'); ?>" name="<?php echo $this->get_field_name('url'); ?>" type="text" value="<?php echo esc_attr($url); ?>" /></p>
<p><label for="<?php echo $this->get_field_id('desc'); ?>"><?php _e('Description:'); ?></label>
<textarea class="widefat" id="<?php echo $this->get_field_id('desc'); ?>" name="<?php echo $this->get_field_name('desc'); ?>"><?php echo $desc; ?></textarea></p>
<p><label for="<?php echo $this->get_field_id('alldocslink'); ?>"><?php _e('Link to all documents (optional):'); ?></label>
<input class="widefat" id="<?php echo $this->get_field_id('alldocslink'); ?>" name="<?php echo $this->get_field_name('alldocslink'); ?>" type="text" value="<?php echo esc_attr($alldocslink); ?>" /></p>
<p><label for="<?php echo $this->get_field_id('alldocstext'); ?>"><?php _e('Text for link to all docs (optional):'); ?></label>
<input class="widefat" id="<?php echo $this->get_field_id('alldocstext'); ?>" name="<?php echo $this->get_field_name('alldocstext'); ?>" type="text" value="<?php echo esc_attr($alldocstext); ?>" /></p>
<?php
}
}
?>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment