Created
December 23, 2014 19:48
-
-
Save JamiesonRoberts/7443c2093cf8dcf41dea to your computer and use it in GitHub Desktop.
Custom Wordpress Function for RSS output and file size calculation
This file contains 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 | |
/* | |
* | |
* Custom function to add a field to the top of all RSS feeds. Captures the most recent post from a custom post type, finds the attached | |
* PDF file (news archive) and the calculates the size and outputs the variables into the RSS XML. | |
* | |
* | |
*/ | |
add_action('rss2_head','current_archive'); | |
add_action('rss_head','current_archive'); | |
add_action('commentsrss2_head','current_archive'); | |
function current_archive() { | |
$pdfArchive = new WP_query( array ( | |
'post_type' => 'wn_back_issues', | |
'showposts' => 1 | |
) ); | |
if ( $pdfArchive->have_posts() ): | |
while ( $pdfArchive->have_posts() ): $pdfArchive->the_post(); | |
$thumb_id = get_post_thumbnail_id(); | |
$thumb_url_array = wp_get_attachment_image_src($thumb_id); | |
$thumb_url = $thumb_url_array[0]; | |
$media = get_attached_media('application/pdf'); | |
foreach($media as $pdfLinks) { | |
$pdfFileLink = array( wp_get_attachment_url( $pdfLinks->ID ) ); | |
$pdfServerLink = array( get_attached_file( $pdfLinks->ID ) ); | |
} | |
$pdfFileSize = number_format( filesize( $pdfServerLink[0] ) / 1048576 , 2 ); | |
?> | |
<archivepdf> | |
<currentpdf><?php echo $pdfFileLink[0]; ?></currentpdf> | |
<pdfCover><?php echo $thumb_url; ?></pdfCover> | |
<pdfSize><?php echo $pdfFileSize; ?>MB</pdfSize> | |
</archivepdf> | |
<?php | |
endwhile; | |
endif; | |
wp_reset_postdata(); | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment