Created
March 4, 2012 07:34
-
-
Save BronsonQuick/1971133 to your computer and use it in GitHub Desktop.
A WordPress widget to display all attachments to a post/page of a certain type
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?php | |
/* | |
* A WordPress widget that displays all PDF's attached to the current post | |
* You can adapt this widget to extract other file types by altering the 'post_mime_type' => 'application/pdf' | |
* argument refer to: http://en.wikipedia.org/wiki/Internet_media_type#List_of_common_media_types for mime types | |
*/ | |
class Sennza_PDF_Download_Widget extends WP_Widget | |
{ | |
/** constructor */ | |
function __construct() | |
{ | |
parent::WP_Widget( /* Base ID */ | |
'sennza_pdf_download_widget', /* Name */ | |
'Sennza PDF Download Widget', array( 'description' => 'A widget to display all pdf attachments for a page' )); | |
} | |
/** @see WP_Widget::widget */ | |
function widget( $args, $instance ) | |
{ | |
extract( $args ); | |
$title = apply_filters( 'widget_title', $instance['title'] ); | |
echo $before_widget; | |
if ( !empty($title) ) { | |
echo $before_title . $title . $after_title; | |
} | |
global $post; | |
$args = array( | |
'post_type' => 'attachment', | |
'post_mime_type' => 'application/pdf', | |
'numberposts' => -1, | |
'post_status' => 'inherit', | |
'post_parent' => $post->ID | |
); | |
$attachments = new WP_Query( $args );?> | |
<aside id="attachments" class="widget"> | |
<ul class="downloads"> | |
<?php | |
while ( $attachments->have_posts()) : $attachments->the_post(); ?> | |
<li><a | |
href="<?php echo wp_get_attachment_url( $attachments->ID ); ?>"><?php echo $post->post_title; ?></a> | |
<span | |
class="download-details"><?php echo substr($post->post_mime_type, strpos($post->post_mime_type, '/') + 1) . ' - ' . size_format( filesize( get_attached_file($post->ID) ) ); ?></span> | |
</li> | |
<?php endwhile; ?> | |
</ul> | |
</aside> | |
<?php echo $after_widget; | |
} | |
/** @see WP_Widget::update */ | |
function update( $new_instance, $old_instance ) | |
{ | |
$instance = $old_instance; | |
$instance['title'] = strip_tags( $new_instance['title'] ); | |
return $instance; | |
} | |
/** @see WP_Widget::form */ | |
function form( $instance ) | |
{ | |
if ($instance) { | |
$title = esc_attr( $instance['title'] ); | |
} | |
else { | |
$title = __( 'PDF Downloads', 'text_domain' ); | |
} | |
?> | |
<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 $title; ?>"/> | |
</p> | |
<?php | |
} | |
} | |
add_action( 'widgets_init', create_function( '', 'register_widget("sennza_pdf_download_widget");' ) ); | |
?> |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment