Skip to content

Instantly share code, notes, and snippets.

@ScottPhillips
Created August 6, 2012 04:36
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
Star You must be signed in to star a gist
Save ScottPhillips/3270523 to your computer and use it in GitHub Desktop.
Output Wordpress Comments as XML
<?php
// Load WordPress functions
require('wp-load.php');
$post = get_page($_REQUEST["id"]);
$id = $post->ID;
$withcomments = true;
$comments = getWPcomments();
$containerName = "comment-set"; $elementName = "comment";
// XML header
$xml = "<?xml version=\"1.0\" encoding=\"utf-8\"?>\n" . "<" . $containerName . ">\n";
$xml = display_comments($xml, $elementName, $comments);
// Close XML container
$xml .= "</" . $containerName . ">\n";
header("Content-type: text/xml");
echo $xml;
// Adapted from comments_template() in wp-includes/comment-template.php
function getWPcomments() {
global $wp_query, $withcomments, $post, $wpdb, $id, $comment, $user_login, $user_ID, $user_identity;
// Reading in WordPress comments into $comments array.
if ( ! (is_single() || is_page() || $withcomments) )
return;
$req = get_option('require_name_email');
$commenter = wp_get_current_commenter();
extract($commenter, EXTR_SKIP);
/** @todo Use API instead of SELECTs. */
if ( $user_ID) {
$comments = $wpdb->get_results($wpdb->prepare("SELECT * FROM $wpdb->comments WHERE comment_post_ID = %d AND (comment_approved = '1' OR ( user_id = %d AND comment_approved = '0' ) ) ORDER BY comment_date", $post->ID, $user_ID));
} else if ( empty($comment_author) ) {
$comments = $wpdb->get_results($wpdb->prepare("SELECT * FROM $wpdb->comments WHERE comment_post_ID = %d AND comment_approved = '1' ORDER BY comment_date", $post->ID));
} else {
$comments = $wpdb->get_results($wpdb->prepare("SELECT * FROM $wpdb->comments WHERE comment_post_ID = %d AND ( comment_approved = '1' OR ( comment_author = %s AND comment_author_email = %s AND comment_approved = '0' ) ) ORDER BY comment_date", $post->ID, $comment_author, $comment_author_email));
}
return $comments;
}
/* display_comments - Generates WordPress comments contained in $comments as XML elements. display_comments function is adapted from the WordPress file wp-content/themes/default/comments.php.
*/
function display_comments($xml, $elementName, $comments) {
if ($comments) :
$commentcounter = 0;
foreach ($comments as $comment) :
$commentcounter++;
$xml .= "<" . $elementName . ">\n";
$xml .= "<id>comment-" . $comment->comment_ID . "</id>\n";
$xml .= "<date>" . mysql2date( get_option('date_format'), $comment->comment_date) . "</date>\n";
// Code fragment from get_comment_author_link() and get_comment_author() in wp-includes/comment-template.php
$xml .= "<cite><![CDATA[";
$url = $comment->comment_author_url;
if ( empty($comment->comment_author) )
$author = "Anonymous";
else
$author = $comment->comment_author;
if ( empty( $url ) || 'http://' == $url )
$xml .= $author;
else
$xml .= "<a href=\"" . $url . "\" rel=\"nofollow\">" . $author . "</a>";
$xml .= "]]></cite>\n";
if(function_exists('get_avatar')) $xml .= "<avatarimg><![CDATA[" . get_avatar($comment->comment_author_email, '45') . "]]></avatarimg>\n";
if ($comment->comment_approved == '0')
$xml .= "<text>Your comment is awaiting moderation.</text>\n";
else {
$newComment = str_replace(chr(13), '<br />', $comment->comment_content, $count);
$xml .= "<text><![CDATA[" . $newComment . "]]></text>\n";
}
$xml .= "</" . $elementName . ">\n";
endforeach; /* end for each comment */
else : // this is displayed if there are no comments so far
if ('open' == $post-> comment_status) :
// If comments are open, but there are no comments.
else : // comments are closed
$xml .= "<text>" . "Comments are closed." . "</text>\n";
endif;
endif;
return $xml;
}
?>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment