0

Dave serves us with his work on https://daveismyname.com/adding-events-to-microsoft-outlook-from-php-using-ical-bp regarding how to create a ics file form a link. Thanks to him!

I tried to ask him my question but had problems in display my code so I put it here on stack.

Could anyone be so kind and not down vote my even if it is stupid question (for everytime I try to ask something about php this happens)? I am a php newbie and trying my best to be precise! Thanks for patience!

So here my attempt with Daves code:

I use the following in my php calendar template page (WP using “Advanced Custom Fields Pro” plugin to set the data (which works just fine):

                  <div id="wrapper">

                     <?php 

                        $rows = get_field('events');
                        if($rows)
                        {

                            foreach($rows as $row)
                            {
                                echo '<a class="entry-meta-area" href="ical.php?date=' . $row['date_short'] . '&amp;subject=' . $row['event'] .'&amp;">      
                                        <div class="left-div">
                                            <div>
                                                <span>' . $row['date_long'] . '</span>
                                            </div>
                                        </div>                                                
                                        <div class="right-div">
                                            <div>
                                                <span>' . $row['event'] .'</span>
                                            </div>                                  
                                        </div>           
                                    </a>';


                            }

                        }

                        $date      = $_GET['date'];
                        $subject   = $_GET['subject'];

                        $ical = " BEGIN:VCALENDAR
                                  VERSION:2.0
                                  PRODID:-//hacksw/handcal//NONSGML v1.0//EN
                                  BEGIN:VEVENT
                                  UID:" . md5(uniqid(mt_rand(), true)) . "myspecificwebsite.com
                                  DTSTAMP:" . gmdate('Ymd').'T'. gmdate('His') . "Z
                                  DTSTART:".$date."T100000Z
                                  DTEND:".$date."T100000Z
                                  SUMMARY:".$subject."
                                  END:VEVENT
                                  END:VCALENDAR";

                        //set correct content-type-header
                        header('Content-type: text/calendar; charset=utf-8');
                        header('Content-Disposition: inline; filename=calendar.ics');
                        echo $ical;
                        exit;
                    ?>


                </div>

This creates 3 problems on loading the page:

1) Opens my calendar (Mac) on page load instead of on click the row where the data should be collected 2) My calendar gives a warning “could not read the file because no event is defined“ 3) Clicking the link leads to (404) page: …myspecificwebsite.com/calendar-template/ical.php?date=02/26/2016&subject=Annual%20Turnover (should stay on the same page, yet)

Garavani
  • 755
  • 1
  • 13
  • 28
  • 1
    You can't set headers after you have outputted anything to the browser. The ical-generation should be it's own file you link to in your list. – M. Eriksson Apr 27 '16 at 05:12
  • 1
    At a glance, you've combined 2 pages into one. Everything from `$date = $_GET['date'];` to `exit;` should be in a file called `ical.php`, which generates the actual .ics file. – Hobo Apr 27 '16 at 05:13
  • Possible dublicate: http://stackoverflow.com/questions/8028957/how-to-fix-headers-already-sent-error-in-php – M. Eriksson Apr 27 '16 at 05:14
  • Thanks guys for comments! I will try with an extra page. Can I keep the link as it is when this will be on the same folder level as my template php? – Garavani Apr 27 '16 at 05:16
  • Awesome guys! I had to put the complete url, then it worked. Only problem remaining now is the alert that my calendar gives saying that “couldn't read the file. No event was added” Anyway thanks for useful hints!!! – Garavani Apr 27 '16 at 05:27

0 Answers0