I am trying to use the draft_to_publish hook (http://codex.wordpress.org/Post_Status_Transitions) to run a function when a post that is a draft becomes published. This hook does not appear to be working:
add_action('draft_to_publish', 'myFunction');
When I use this in my plug-in, myFunction is never fired. I have used/tested the function before. I know it is the hook, not the contents of myFunction.
All ‘solutions’ I found just link to the Codex page above. Any ideas about why this hook is not working?
Edit:
Here is an example. It the email will not be sent, because the action never fires:
function myfunction () {
$to = "recipient@example.com";
$subject = "Hi!";
$body = "Hi,nnHow are you?";
if (mail($to, $subject, $body)) {
echo("<p>Message successfully sent!</p>");
} else {
echo("<p>Message delivery failed...</p>");
}
}
add_action('draft_to_publish', 'myFunction');
You certainly have the right hook, but keep in mind that you are hooking your functionality specifically to the
draft_to_publish
action, i.e. to the specific case of a post-object pre-existing in the database withdraft
status being updated topublish
. Note that this action ignores drafts which are automatically saved by WordPress when a new post is created – these “drafts” have apost_status
ofauto-draft
.I’m not exactly sure how you’ve been debugging the issue up to this point, but if you haven’t already, I would recommend verifying that the action itself is firing when you expect it to, perhaps by attaching some simple, arbitrary, and obvious function to it:
That said, part of your problem may lie in capitalization – the action callback in your example references the function
myFunction
while the function that is defined has been namedmyfunction
.Though I am not sure as to what you are trying to accomplish, you could alternately attempt to attach your functionality to the generic action
transition_post_status
which gets passed the the new status of the post, the previous status of the post, and the post object such that you would have something similar toThere are also a number of tools available that might grant you more insight into WordPress’ action execution such as the action hooks inspector for the Debug Bar plugin.