Basem Sherin Dahroug
Basem Sherin Dahroug

Reputation: 55

simple wordpress author notify function

hello there all fellow developers today i had a very strange and simple problem , i need to notify post authors when there posts are approved in WordPress site .. the strange thing when i use the following code in my theme functions it doesn't work only work's when i type the email variable manually .. thanks for your help

    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;
        // "[email protected]";
        $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' ); ?>

Upvotes: 0

Views: 168

Answers (1)

H&#252;seyin BABAL
H&#252;seyin BABAL

Reputation: 15550

You shouldn't use transition_post_status because

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.

in here . You can use publish_post instead and your function needs a bit refactoring. And important point here, you are using custom pust type, so action name must be in a format like 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;
        // "[email protected]";
        $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 

?>

Upvotes: 1

Related Questions