Tuesday, July 5, 2022
HomeWordPress Developmentplugin growth - Creating an ics calendar from customized publish sort

plugin growth – Creating an ics calendar from customized publish sort


I’ve a customized publish sort known as ‘competicions’, with ACF for date and time (d/m/Y | H:i). Is there any strategy to create an ics calendar from this posts and make it autoupdatable?

I attempted following code however it’s not working, it hundreds an empty calendar.

I am utilizing the code as a plugin.

-Customized publish sort: competicions

-Begin/Finish date: ACF datahora_prova

I want the publish url as an commentary. No extra data is required.

Thanks.

EDIT: right here is my code:

<?php
/**
Plugin Identify: ical feed
 */

// UTILS

// Test if string is a timestamp
perform isValidTimeStamp($timestamp) {
    //if($timestamp == '') return;
    return ((string) (int) $timestamp === $timestamp) 
        && ($timestamp <= PHP_INT_MAX)
        && ($timestamp >= ~PHP_INT_MAX);
}

// Escapes a string of characters
perform escapeString($string) {
    return preg_replace('/([,;])/','$1', $string);
}

// Shorten a string to desidered characters lenght - eg. shorter_version($string, 100);
perform shorter_version($string, $lenght) {
if (strlen($string) >= $lenght) {
        return substr($string, 0, $lenght);
    } else {
        return $string;
    }
}

// Add a customized endpoint "calendar"
perform add_calendar_feed(){
    add_feed('icalfeed', 'export_ics');
    // Solely uncomment these 2 strains the primary time you load this script, to replace WP rewrite guidelines, or in case you see a 404
international $wp_rewrite;
$wp_rewrite->flush_rules( false );
}
add_action('init', 'add_calendar_feed');

// Calendar perform
perform export_ics(){

    // Question the occasion
    $the_event = new WP_Query(array(
        'p' => $_REQUEST['id'],
        'post_type' => 'competicions',
    ));
    
    if($the_event->have_posts()) :
        
        whereas($the_event->have_posts()) : $the_event->the_post();
    
        // In case your model of WP < 5.3.0 use the code under

        /*  The proper date format, for ALL dates is date_i18n('YmdTHisZ',time(), true)
            So in case your date isn't on this format, use that perform    */
    
        $start_date = get_field("datahora_prova", false, false); // EDIT THIS WITH YOUR OWN VALUE
        $end_date = get_field("datahora_prova", false, false); // EDIT THIS WITH YOUR OWN VALUE
        
        // The remaining is identical for any model
        $timestamp = date_i18n('YmdTHisZ',time(), true);
        $uid = get_the_ID();
        $created_date = get_post_time('YmdTHisZ', true, $uid );
        $organiser = get_bloginfo('identify'); // EDIT THIS WITH YOUR OWN VALUE
        $handle=""; // EDIT THIS WITH YOUR OWN VALUE
        $url = get_the_permalink();
        $abstract = get_the_excerpt();
        $content material = html_entity_decode(trim(preg_replace('/ss+/', ' ', get_the_content()))); // removes newlines and double areas
        $title = html_entity_decode(get_the_title());

        //Give the iCal export a filename
        $filename = urlencode( $title.'-ical-' . date('Y-m-d') . '.ics' );
        $eol = "rn";

        //Acquire output
        ob_start();

        // Set the right headers for this file
        header("Content material-Description: File Switch");
        header("Content material-Disposition: attachment; filename=".$filename);
        header('Content material-type: textual content/calendar; charset=utf-8');
        header("Pragma: 0");
        header("Expires: 0");

// The under ics construction MUST NOT have areas earlier than every line
// Credit score for the .ics construction goes to https://gist.github.com/jakebellacera/635416
?>
BEGIN:VCALENDAR
VERSION:2.0
PRODID:-//<?php echo get_bloginfo('identify'); ?> //NONSGML Occasions //EN
CALSCALE:GREGORIAN
X-WR-CALNAME:<?php echo get_bloginfo('identify').$eol;?>
BEGIN:VEVENT
CREATED:<?php echo $created_date.$eol;?>
UID:<?php echo $uid.$eol;?>
DTEND;VALUE=DATE:<?php echo $end_date.$eol; ?>
DTSTART;VALUE=DATE:<?php echo $start_date.$eol; ?>
DTSTAMP:<?php echo $timestamp.$eol; ?>
LOCATION:<?php echo escapeString($handle).$eol; ?>
DESCRIPTION:<?php echo $content material.$eol; ?>
SUMMARY:<?php echo $title.$eol; ?>
ORGANIZER:<?php echo escapeString($organiser).$eol;?>
URL;VALUE=URI:<?php echo escapeString($url).$eol; ?>
TRANSP:OPAQUE
BEGIN:VALARM
ACTION:DISPLAY
TRIGGER;VALUE=DATE-TIME:<?php echo $deadline.$eol; ?>
DESCRIPTION:Reminder for <?php echo escapeString(get_the_title()); echo $eol; ?>
END:VALARM
END:VEVENT
<?php
        endwhile;
?>
END:VCALENDAR
<?php
        //Acquire output and echo
        $eventsical = ob_get_contents();
        ob_end_clean();
        echo $eventsical;
        exit();

    endif;

}
?>

RELATED ARTICLES

LEAVE A REPLY

Please enter your comment!
Please enter your name here

- Advertisment -
Google search engine

Most Popular

Recent Comments