WordPress.org

Ready to get started?Download WordPress

Forums

Automatically send a post to an admin email (13 posts)

  1. jkoontz
    Member
    Posted 8 years ago #

    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');

  2. jkoontz
    Member
    Posted 8 years ago #

    Do template tags not work inside an add_action function?

  3. Kafkaesqui

    Posted 8 years ago #

    Try:

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

  4. jkoontz
    Member
    Posted 8 years ago #

    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?

  5. Kafkaesqui

    Posted 8 years ago #

    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.

  6. jkoontz
    Member
    Posted 8 years ago #

    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;
    }

  7. jkoontz
    Member
    Posted 8 years ago #

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

  8. jkoontz
    Member
    Posted 8 years ago #

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

  9. jkoontz
    Member
    Posted 8 years ago #

    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.

  10. Dgold
    Member
    Posted 8 years ago #

    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.

  11. jkoontz
    Member
    Posted 8 years ago #

    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?

  12. Kafkaesqui

    Posted 8 years ago #

    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;
    }

  13. Dgold
    Member
    Posted 7 years ago #

    (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!

Topic Closed

This topic has been closed to new replies.

About this Topic

Tags