简单的wordpress作者通知功能

时间:2014-04-07 07:29:05

标签: php wordpress

你好,所有的开发人员 今天我有一个非常奇怪和简单的问题,我需要在WordPress网站上批准帖子时通知帖子作者..奇怪的是当我在我的主题函数中使用以下代码时它不起作用&#39 ;当我手动输入电子邮件变量时...感谢您的帮助

    function notify_new_post($post_id) {
   // if( ( $_POST['post_status'] == 'publish' )  ) {
        $post = get_post($post_id);
        $author = get_userdata($post->post_author);
        $author_email = $author->user_email;
        // "x_123@windowslive.com";
        $email_subject = "Your post has been published.";

        ob_start(); ?>

        <html>
            <head>
                <title>New post at <?php bloginfo( 'name' ) ?></title>
            </head>
            <body>
                <p>
                    Hi <?php echo $author->user_firstname ?>,
                </p>
                <p>
                    Your post <a href="<?php echo get_permalink($post->ID) ?>"><?php the_title_attribute() ?></a> has been published.
                </p>
            </body>
        </html>

        <?php

        $message = ob_get_contents();

        ob_end_clean();

        wp_mail( $author_email, $email_subject, $message );
        //    }
}

add_action( 'transition_post_status', 'notify_new_post' ); ?>

1 个答案:

答案 0 :(得分:1)

您不应该使用transition_post_status,因为

This function's access is marked as private. That means it is not intended for use by plugin and theme developers, but only in other core functions.

here中的

。您可以使用publish_post代替,您的函数需要进行一些重构。重要的是,您使用的是自定义pust类型,因此操作名称必须采用publish_{custom_post_type_name};

之类的格式
<?php

function notify_new_post($post_id) {
   if( ( $_POST['post_status'] == 'publish' ) && ( $_POST['original_post_status'] != 'publish' ) ) {
        $post = get_post($post_id);
        $author = get_userdata($post->post_author);
        $author_email = $author->user_email;
        // "x_123@windowslive.com";
        $email_subject = "Your post has been published.";

        ob_start(); ?>

        <html>
            <head>
                <title>New post at <?php bloginfo( 'name' ) ?></title>
            </head>
            <body>
                <p>
                    Hi <?php echo $author->user_firstname ?>,
                </p>
                <p>
                    Your post <a href="<?php echo get_permalink($post->ID) ?>"><?php the_title_attribute() ?></a> has been published.
                </p>
            </body>
        </html>

        <?php

        $message = ob_get_contents();

        ob_end_clean();

        wp_mail( $author_email, $email_subject, $message );
    }
}

add_action( 'publish_{custom_post_type_name}', 'notify_new_post', 100 ); // Increase priority 

?>