WordPress.org

Support

Support » Plugins and Hacks » Automatically send a post to an admin email

Automatically send a post to an admin email

  • I’ve searched quite a bit and haven’t found a plugin that will send a post to a specific email every time something is published. So, I’ve decided to create my own plugin, but an running into a problem with the variable populating. The email is sent, but the subject and content are empty. I know this has to be a simple problem, but I can’t figure it out.


    function clbg_send_post($post_ID) {
    $email= 'xxxx@xxxx.xxx';
    $post_title = the_title();
    $post_content = the_content();
    mail($email, $post_title, $post_content);
    return $post_ID;
    }

    add_action('publish_post', 'clbg_send_post');

Viewing 12 replies - 1 through 12 (of 12 total)
  • Do template tags not work inside an add_action function?

    Kafkaesqui

    @kafkaesqui

    Try:

    $post_title = get_the_title();
    $post_content = get_the_content();

    No luck. But I can get it to work by accessing the table directly.

    global $wpdb, $tableposts;
    if (!isset($tableposts))
    $tableposts=$wpdb->posts;

    //Get The Post and the excerpt
    $post_title = $wpdb->get_var("SELECT 'post_title' FROM '$tableposts' WHERE 'ID' = $post_ID");
    $post_text = $wpdb->get_var("SELECT 'post_content' FROM '$tableposts' WHERE 'ID' = $post_ID");<br />

    Obviously, get_the_title way is simplier and more elegant. Any thoughts on what I could be doing wrong?

    Kafkaesqui

    @kafkaesqui

    Most likely the $post object is not available in your function. Change your global scope to:

    global $post, $wp_query;

    and add this line after it:

    $post = $wp_query->post;

    The get_* functions should work after that.

    Still no luck.

    I now have something like:

    function clbg_send_post($post_ID) {
    global $post, $wp_query;

    $post = $wp_query->post;

    $post_title = get_the_title();
    $post_text = get_the_content();

    $email = 'xxxx@xxxx.xxx'

    mail($email, $post_title, $post_text);
    return $post_ID;
    }

    Anyone else with this problem? I’d really like to use the standard API rather than accesing the database directly.

    Well, using get_the_title($post_ID) worked. I don’t know why I didn’t think about that previously.

    Spoke, too soon. It works for get_the_title($post_ID), but not get_the_content($post_ID). Which is strange, because I thought these were parameterless functions.

    I am seeking the same functionality for one of my blogs.

    I was thinking of this approach: getting one of the existing Subscribe To Posts By Email plugin(s), and only subscribing my Admin email. And not necessarily putting the link on the page for anyone to subscribe by email, if I wanted to use it just for Admin. However if you release your plugin I am interested in that.

    I’ll let you know when it’s finished. I could wrap up the whole thing right now if I access the DB directly, but I’d rather not circumvent the API. I know there has to be other people that need access to this information outside the Loop. Anybody?

    I was finally able to look at this a bit closer and think, after scrapping my first suggestion, I can offer *a* solution:

    function clbg_send_post($post_ID) {
    $post = get_postdata($post_ID);
    $post_title = $post['Title'];
    $post_text = $post['Content'];

    $email = 'xxxx@xxxx.xxx';

    mail($email, $post_title, $post_text);
    return $post_ID;
    }

    (1) Has this ever been made into a plugin to email every post to Admin when the author hits “Publish”?

    (2) Can this plugin also work to send every new Comment to the Admin?

    Thanks!

Viewing 12 replies - 1 through 12 (of 12 total)
  • The topic ‘Automatically send a post to an admin email’ is closed to new replies.
Skip to toolbar