Puck
Puck

Reputation: 217

How to Fire an Event Only Once on WordPress Post Publish?

Thanks for any help with this.

I need to know how to hook wp_insert_post (or whatever is similar and better?) without it firing multiple times. What is the correct way to do this in WordPress?

For example:

In Plugin.php

add_filter( 'wp_insert_post', 'add_data') );
...
function add_data()
{
    // This line is outputted twice
    terseErrorLog("This code was executed.");
}

Upvotes: 1

Views: 1887

Answers (1)

manishie
manishie

Reputation: 5322

Try this:

function add_data() {
  global $post;
  if ($post - > post_status == "publish") {
    terseErrorLog("This code was executed.");
  }
}
add_action('save_post', 'add_data');

Upvotes: 1

Related Questions