• This is my first attempt at a plugin, and I’m not even sure what I need to google to find the answer. I wrote (copy and pasted) a plugin to email the content of a post when you click publish. That part works, but if you schedule a post, it sends a blank email when it is published. I think the problem is because of how I’m getting the post:

    global $wpdb, $post_ID, $current_user;
    
    $post = $wpdb->get_row( "SELECT post_title FROM $wpdb->posts WHERE ID = " . $post_ID);
    $content = $wpdb->get_row( "SELECT post_content FROM $wpdb->posts WHERE ID = " . $post_ID);

    Then later I’ve used:

    `add_action(‘publish_post’, ‘rhk_send_email’);’

    Again, this works if you click Publish, but I need it to work for scheduled posts.

    Thanks!

Viewing 2 replies - 1 through 2 (of 2 total)
  • Look into publish_post hook. i am not sure but this will help you i think. Thanks

    Thread Starter rhkukui

    (@rhkukui)

    Thanks, I was already using the publish_post hook. I changed it to publish_future_post but I don’t know if that was necessary or not. I eventually figured it out. I had to pass the ID to the function. In case anyone else cares, here’s my final solution:

    function rhk_send_email($id) {
    
    	$post = get_post($id);
    
    	$to = "email@domain.com";
    	$subject = $post->post_title;
    	$message = $post->post_content;
    
    	// To send HTML mail, the Content-type header must be set
    	$headers  = 'MIME-Version: 1.0' . "\r\n";
    	$headers .= 'Content-type: text/html; charset=iso-8859-1' . "\r\n";
    
    	mail($to, $subject, $message, $headers);
    }
    
    add_action('publish_future_post', 'rhk_send_email');
Viewing 2 replies - 1 through 2 (of 2 total)
  • The topic ‘sending an email when a scheduled post is published’ is closed to new replies.