2

I am working on a plugin that sent an email when a post is published using

 add_action('save_post','my_function');

 my_function($post_id)
 {
  //do everything here
 }

its working fine whenever a new post is published or its being updated from quick edit,

but the problem is that its not working when a post is schedule for future publish, for this I googled it, and find the following

  add_action('publish_future_post', 'my_function');

this is the same function as used for above action,

I also found following action on some results,

  add_action('future_to_publish', 'my_function');

but the last 2 action not working , mean its not sending any email,

can anyone help me to figure it out ,

4

2 回答 2

0

在第 3 行,您正在检查帖子的 post_status 并明确检查发布,这仅针对(您猜对了)已发布的帖子设置。当帖子计划稍后发布时,它的状态设置为将来。对于您的示例,前三行:

function my_function($post_id) {

    $post= get_post($post_id);

    if ($post->post_type == 'post' && ($post->post_status == 'publish' || $post->post_status == 'future') ) {

让我知道这是否适合你。

于 2013-08-14T16:47:08.063 回答
0

@安德鲁·巴特尔

这是我的完整功能,

  function my_function($post_id) {

   $post= get_post($post_id);

    if ($post->post_type == 'post' && $post->post_status == 'publish') {

     global $current_user;

  get_currentuserinfo();

  $usernamme = $current_user->user_login;

  $email= $current_user->user_email;

  $fname = $current_user->user_firstname;

  $lname = $current_user->user_lastname;

 $disname =  $current_user->display_name;

 $id =  $current_user->ID;

 $user = new WP_User($id);



if ( !empty( $user->roles ) && is_array( $user->roles ) ) 

{

    foreach ( $user->roles as $role )

        $user_role = $role;

        $upper = ucfirst($user_role);

}

$email_post_options = get_option('email_post_options');

$adminemail =(!empty($email_post_options['adminemail'])) ? $email_post_options['adminemail'] : get_bloginfo('admin_email');

if(isset($email_post_options['rol']))

{

            $msg = '';

            $postdet = get_post($post_id);

            $title = $postdet->post_title;

            //$excerpt = substr($postdet->post_content,0,150);

            $pdate = $postdet->post_date;

            $permalink = get_permalink($post_id);

            $price = get_post_meta( $post_id, '_my_meta_value_key', true );

            $date = get_post_meta( $post_id, '_my_meta_date_key', true );


    foreach($email_post_options['rol'] as $mailrol) // the roles which are saved from the plugin settings page, which is telling that who's role email will be received when a new post from the user is created.

    {


         if($mailrol==$upper)

         {
             $name = $fname.' '.$lname;
            $usename = ($name!=' ')? $name : $usernamme;


            $msg .='Full Name / Username : ' .$usename."\n";

            $msg .='Title : '.$title."\n";

            //$msg .='<p>Content : '.$excerpt.'</p>';

            $msg .='Link = '.$permalink."\n";

            $msg .='Price is = '.$price."\n";

            $msg .='Added date = '.$date."\n";

            $msg .='Published date = '.$pdate."\n";

            $msg .='Total Posts : '.count_user_posts($id)."\n";
            echo $msg;
            if($email_post_options['npemail']==1)

            {

                wp_mail($adminemail, 'New Post', $msg);

            }



         }

    }

}

  } // end if

} // end function

这是我的功能,如果您对此有任何困惑,请告诉我。

于 2013-08-14T10:12:12.820 回答