WordPress.org

Ready to get started?Download WordPress

Forums

add_action with publish_post? (3 posts)

  1. husar
    Member
    Posted 3 years ago #

    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 = 'myemail@someemail.com';
        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.

    Cheers.

  2. Bonesnap
    Member
    Posted 2 years ago #

    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?

  3. Pascal Meunier
    Member
    Posted 2 years ago #

    husar: You actually have to use the add_filter() function, not the add_action(). As 'publish_post' is a hook and not an action :)
    http://codex.wordpress.org/Plugin_API/Hooks_2.0.x

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

    add_filter ( 'publish_post', 'email_me' );

    Happy hooking!

    - Pascal

Topic Closed

This topic has been closed to new replies.

About this Topic