WordPress Schedule post function not working?

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,

Read More

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 ,

Related posts

Leave a Reply

2 comments

  1. @Andrew Bartel

    here is my complete function,

      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
    

    its my function , if you have any confusion in this please let me know.

  2. On line 3 you’re checking the post_status of the post and explicitly checking for publish, which is only set for posts that (you guessed it) are published. When a post is scheduled to be published later, it’s status is set to future. For your example, the first three lines:

    function my_function($post_id) {
    
        $post= get_post($post_id);
    
        if ($post->post_type == 'post' && ($post->post_status == 'publish' || $post->post_status == 'future') ) {
    

    Let me know if that works for you.