【问题标题】:What is the wordpress hook when I publish a new post, (not when I update a published one)?当我发布新帖子时(而不是当我更新已发布的帖子时),wordpress 钩子是什么?
【发布时间】:2017-04-06 07:13:16
【问题描述】:

我想在首次发布新帖子时发送电子邮件(而不是在编辑时)

我试过了:

add_action('publish_post', 'postPublished');

但是 postPublished 在我更新已发布的帖子时也会调用。 我只想仅在第一次发布帖子时将其命名为

问候

【问题讨论】:

  • 你可以查看帖子的ID

标签: wordpress wordpress-hook


【解决方案1】:

我想你要找的钩子是draft_to_publish

如果您只想在新帖子上发送电子邮件,我会考虑在帖子发布时进行定位。您甚至可以查看 transition_post_status 钩子,并在帖子已被编辑时设置条件,这样的事情可能会起作用:

function so_post_40744782( $new_status, $old_status, $post ) {
    if ( $new_status == 'publish' && $old_status != 'publish' ) {
        $author = "foobar";
        $message = "We wanted to notify you a new post has been published.";
        wp_mail($author, "New Post Published", $message);
    }
}
add_action('transition_post_status', 'so_post_40744782', 10, 3 );

【讨论】:

  • 这也是我要开始的地方。以下是有关它的更多信息:codex.wordpress.org/Post_Status_Transitions
  • @AndyWarren 我也在考虑这个问题,并进行了编辑。
  • 这些参数是什么:'so_post_40744782', 10, 3?
  • 优先级和接受的参数,你可以阅读它here
【解决方案2】:

您应该阅读 WordPress Codex 中的 Post Status Transitions 以完全理解问题。

例如,涵盖大多数情况的可能解决方案是:

add_action('draft_to_publish', 'postPublished');
add_action('future_to_publish', 'postPublished');
add_action('private_to_publish', 'postPublished');

【讨论】:

    【解决方案3】:
    function your_callback( $post_id, $post  ) {
    
            if (strpos($_SERVER['HTTP_REFERER'], '&action=edit') !== false) {
    
                //edit previous post
            }
            else{
                //just add a new post
            }
        }
        add_action( 'publish_{your_post_type}', 'your_callback', 10, 3 );
    

    【讨论】:

      猜你喜欢
      • 2022-08-14
      • 2020-03-16
      • 1970-01-01
      • 1970-01-01
      • 2013-12-11
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2023-03-08
      相关资源
      最近更新 更多