Created
January 12, 2013 19:55
-
-
Save cstrouse/4520209 to your computer and use it in GitHub Desktop.
Wordpress shortcode to grab all archived newsletters from a Constant Contact archive page and output as a table. To use add the require statement and the shortcode function to your theme's functions.php, upload the simple_html_dom.php file to the theme directory (you can get it from sf.net) and then replace the Constant Contact archive link with…
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 require('simple_html_dom.php'); | |
function generate_newsletter_archives($atts, $content = null) { | |
// setup the beginning of our data table for archives | |
$out = '<table cellpadding="0" cellspacing="0" id="newsletter-archives"><tr><th>Resource</th><th>Date</th></tr><tbody>'; | |
// fetch the page which contains all of the newsletter issues that have been archived | |
$archive_page = str_get_html(wp_remote_retrieve_body(wp_remote_get('http://archive.constantcontact.com/fs127/1103640951068/archive/1111407854121.html'))); | |
// fetch third table on page which contains the archives | |
$archive_table = $archive_page->find('table', 2); | |
// the nested search is required because some list elements contain data other than what we want | |
foreach($archive_table->find('li') as $archived_item) { | |
// extract the URL and title of each archived issue | |
foreach($archived_item->find('a') as $archived_link) { | |
$issue_link = $archived_link->href . "\n\n"; | |
$issue_name = $archived_link->innertext . "\n\n"; | |
} | |
// extract the date for each archived issue | |
foreach($archived_item->find('span') as $archived_item_date) { | |
$issue_date = str_replace('(', '', $archived_item_date->innertext); | |
$issue_date = str_replace(')', '', $issue_date) . "\n\n"; | |
// build the string for each table row - one issue per row | |
$out .= '<tr><td><a href="'.trim($issue_link).'" target="_blank">'.trim($issue_name).'</a></td><td>'.trim($issue_date).'</td></tr>'; | |
} | |
} | |
// append the end of the table | |
$out .= '<tr><td colspan="2"></td></tr></tbody></table>'; | |
return $out; | |
} | |
add_shortcode('newsletter_archives', 'generate_newsletter_archives'); | |
?> |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment