By the way, my plugin works outside of the loop: it is a very basic email trigger.
<?php
/*
Plugin Name: ...
*/
define('WP_USE_THEMES', false);
require('/var/www/html/wordpress/wp-blog-header.php');
add_action('publish_post', 'email_team' );
global $wp_query;
$post_ID = $wp_query->post->ID;
function get_post_data($post_ID) {
$my_id = $post_ID;
$post_id = get_post($my_id);
$post_title = $post_id->post_title;
return $post_title;
}
/* that's where things get confusing...for me, anyway */
$xmy_id = $post_ID;
$xpost_id = get_post($xmy_id);
$author = $xpost_id->post_author;
$user_info = get_userdata($author);
$d_name = $user_info->display_name ;
function email_team($post_ID) {
global $email_list;
$to = $email_list;
$subject = 'Blog Update';
$message = 'There is a new post authored by '.$d_name.' titled "'. get_post_data($post_ID) .'" on the blog ' . get_bloginfo('url') . '.';
$headers = 'From: [email protected]' . "\r\n" .
'Reply-To: [email protected]' . "\r\n" .
'X-Mailer: PHP/' . phpversion();
mail($to, $subject, $message, $headers);
return $post_ID;
}
?>
<?php
register_activation_hook(__FILE__,'email_team_install');
register_deactivation_hook( __FILE__, 'email_team_remove' );
function email_team_install() {
add_option("email_list", 'Default', '', 'yes');
}
function email_team_remove() {
delete_option('email_list');
}
?>
<?php
if ( is_admin() ) {
add_action('admin_menu', 'email_team_admin_menu');
function email_team_admin_menu() {
add_options_page('Email Team', 'Email Team', 'administrator','email-team', 'email_team_html_page');
}
}
?>
<?php
$email_list=get_option('email_list');
function email_team_html_page() {
?>
<div>
<h2>Email Team - Options</h2>
<form method="post" action="options.php">
<?php wp_nonce_field('update-options'); ?>
<table width="510">
<tr valign="top">
<th scope="row"></th>
<td>Enter email addresses (separated by commas)
<input name="email_list" type="text" size="100" id="email_list"
value="<?php echo get_option('email_list'); ?>" /></td>
</tr>
</table>
<input type="hidden" name="action" value="update" />
<input type="hidden" name="page_options" value="email_list" />
<p>
<input type="submit" value="<?php _e('Save Changes') ?>" />
</p>
</form>
</div>
<?php
}
?>
If I remove the block of code with get_userdata
, everything works as expected.
Any help is welcome.