I don’t believe there are fields in $post called topic_title and author_title and the post_author is just an ID which you need to use to pass to get_userdata($id)
I would suggest changing the code to:
$message .= print_r($post, true) . "\r\n\r\n";
then add a topic/reply that will be moderated and your notification email will have debug info from print_r() in it.
To get the topic/reply title you may need to look at the post metadata but I’m not sure where bbPress stores that info.
Ian.
Thread Starter
kraigg
(@kraigg)
Thanks Ian. I did what you said and added the print_r, which displays a list of available objects in the moderation email.
I can now retrieve the topic/reply title by doing:
$message .= $post->post_title;
The only thing is I can’t retrieve is the post_author. I’m able to get the author ID by doing:
$message .= $post->post_author;
But I’m not able to convert the author ID into the author’s name using get_userdata($id). The error I get is “Catchable fatal error: Object of class WP_User could not be converted to string” when I try to do:
$author_id = $post->post_author ;
$author_name = get_userdata($author_id);
I’m assuming it’s because one is a string and one is number.
function notify_admin($post_id) {
if (get_option(self::TD . 'notify')) {
$blogname = wp_specialchars_decode(get_option('blogname'), ENT_QUOTES);
$message = sprintf(__('New topic/reply awaiting moderation on your site %s: %s', self::TD), $blogname, $blogurl) . "\r\n\r\n";
/* Add body of topic/reply to email */
$post = get_post($post_id);
if ($post) {
$author_id = $post->post_author ;
$author_name = get_userdata($author_id);
$message .= "<strong>The following content was posted:</strong>\r\n";
$message .= "'" . $author_name . ":\r\n";
$message .= "'" . $post->post_title . ":\r\n" . $post->post_content . "'\r\n\r\n";
// $message .= print_r($post, true) . "\r\n\r\n";
}
@wp_mail(get_option('admin_email'), sprintf(__('[%s] Moderation required', self::TD), $blogname), $message);
}
}
Thread Starter
kraigg
(@kraigg)
I think I’ve found a way to get the author name! This seems to work.
Change my current code above to:
function notify_admin($post_id) {
if (get_option(self::TD . 'notify')) {
$blogname = wp_specialchars_decode(get_option('blogname'), ENT_QUOTES);
$message = sprintf(__('New topic/reply awaiting moderation on your site %s: %s', self::TD), $blogname, $blogurl) . "\r\n\r\n";
/* Add body of topic/reply to email */
$post = get_post($post_id);
if ($post) {
$author_id = $post->post_author ;
$author_name = get_userdata($author_id);
$message .= "<strong>The following content was posted:</strong>\r\n";
$message .= "Posted by " . $author_name->user_firstname . " " . $author_name->user_lastname . ":\r\n";
$message .= "'" . $post->post_title . ":\r\n" . $post->post_content . "'\r\n\r\n";
}
@wp_mail(get_option('admin_email'), sprintf(__('[%s] Moderation required', self::TD), $blogname), $message);
}
}
Glad you got it working.
I have not checked, but for an anonymous user you may find that $author_id is 0 or null.
Either way – I’ll merge in those changes for the next version and add a check for anonymous users just in case.
Ian.
Thread Starter
kraigg
(@kraigg)
Thanks again for all your help, and for the great plugin. You rock!
Added the above to V1.4 of the plugin.