Support » Plugins » add_action with publish_post?

  • I am attempting to run a function anytime someone publishes a post. I have written a few other plugins but nothing dealing with and add_action and the publish_post. To create a quick demo I created a new plugin and activated it. The plugin contains a simple email bit right out of a codex example. The plugin contain this….

    function email_me($post_ID)  {
        $friends = '';
        mail($friends, "Blog Post",
          'New blog post has been submitted.');
        return $post_ID;
    add_action ( 'publish_post', 'email_me' ); //should trigger function on publish post

    I was thinking that this would simple fire off an email each time a post was published. But it doesn’t. No error, nothing happens. I have been thinking about this for a while and I am not sure why it doesn’t. Is it because the plugin doesn’t ever really publish a post within the plugin? Is there something else I need to add to the plugin to make the connection to a post being published?

    My goal is to simply have a function run with each post published. If someone can point me in the right direction that would be great.


Viewing 2 replies - 1 through 2 (of 2 total)
  • I am also having this problem with a plugin I have created; I am trying to have an email sent when a post is published via the ‘publish_post’ hook. I even tried using the ‘publish_page’ hook just to see if it would work, but to no avail. Nothing happens. I’m using WordPress 3.2.

    Is this a known bug with WordPress?

    husar: You actually have to use the add_filter() function, not the add_action(). As ‘publish_post’ is a hook and not an action 🙂

    This code should work perfectly (using the function above).

    add_filter ( 'publish_post', 'email_me' );

    Happy hooking!

    – Pascal

Viewing 2 replies - 1 through 2 (of 2 total)
  • The topic ‘add_action with publish_post?’ is closed to new replies.