您当然有正确的挂钩,但请记住,您是将您的功能专门挂钩到draft_to_publish
操作,即针对数据库中预先存在的post对象的特定情况draft
状态更新为publish
. 请注意,此操作会忽略Wordpress在创建新帖子时自动保存的草稿-这些“草稿”具有post_status
属于auto-draft
.
我不太确定到目前为止您是如何调试该问题的,但如果您还没有调试,我建议您验证操作本身是否在您预期的时间内启动,也许可以通过为其附加一些简单、任意和明显的函数:
function kill_wp( $post ) {
die( \'draft_to_publish fired for post #\' . $post[\'ID\'] . \' entitled, "\' . $post[\'post_title\'] . \'"\' );
}
add_action( \'draft_to_publish\', \'kill_wp\' );
也就是说,部分问题可能在于大写-示例中的操作回调引用了函数
myFunction
定义的函数已命名
myfunction
.
虽然我不确定您想要完成什么,但您可以尝试将您的功能附加到the generic action transition_post_status
它传递了post的新状态、post的先前状态和post对象,这样您就可以获得类似于
function wpse77561_mail_on_publish( $new_status, $old_status, $post ) {
if (get_post_type($post) !== \'post\')
return; //Don\'t touch anything that\'s not a post (i.e. ignore links and attachments and whatnot )
//If some variety of a draft is being published, dispatch an email
if( ( \'draft\' === $old_status || \'auto-draft\' === $old_status ) && $new_status === \'publish\' ) {
$to = \'[email protected]\';
$subject = \'Hi!\';
$body = \'Hi,\' . chr(10) . chr(10) . \'How are you?\';
if( wp_mail( $to, $subject, $body ) ) {
echo(\'<p>Message successfully sent!</p>\');
} else {
echo(\'<p>Message delivery failed...</p>\');
}
}
}
add_action(\'transition_post_status\', \'wpse77561_mail_on_publish\');
还有许多工具可以让您更深入地了解Wordpress的操作执行,例如
action hooks inspector 对于
Debug Bar 插件。