• Hi,

    I have a custom post type Termin (event) with starting dates etc. I want to generate a .ics file when the event is saved or updated. My problem is that sometimes the function is triggered also on new post giving me the download prompt of the browser instead of bringing me to the new post admin screen.

    I don’t understand why my function is triggered on new post although I use pre_post_update. Here my function:

    add_action('pre_post_update', 'ma_create_termin_ics',1,10);
     function ma_create_termin_ics($post_id) {
      if ($parent_id = wp_is_post_revision($post_id)) {
        $post_id = $parent_id;
      }
      $p_type = get_post_type($post_id);
      if ($p_type == 'termin') {
        $upload_dir = wp_upload_dir();
        $ics_dir = $upload_dir['basedir'] . '/ics';
        if (!file_exists($ics_dir)) {
          mkdir($ics_dir, 0755, true);
        }
        $filephad = $ics_dir . '/' . $post_id . '_ics.ics';
    
        header('Content-Type: text/Calendar; charset=utf-8');
        header('Content-Disposition: attachment; filename=' . $post_id . '_ics.ics');
        header('Content-Transfer-Encoding: binary');
    
        $ics_file = fopen($filephad, 'wb');
        fwrite($ics_file, 'BEGIN:VCALENDAR' . "\n");
    .................
        fwrite($ics_file, 'END:VCALENDAR');
    
        fclose($ics_file);
      }
    }

    Any ideas?

    Thanks

Viewing 1 replies (of 1 total)
  • Moderator bcworkz

    (@bcworkz)

    “Update” in relation to the parent function is broader in scope than what we would expect from our human concept of the term. New posts are an update in the sense the status is updated from draft to publish. To most accurately capture only the event you want you should use one of the dynamic hooks in wp_transition_post_status(). I think the one you are after is ‘publish_to_publish’. If that is not it, hook ‘transition_post_status’ and figure out what combination of new and old statuses will work for you.

Viewing 1 replies (of 1 total)

The topic ‘pre_post_update fires on new post button’ is closed to new replies.