WordPressのカスタムフィールドのデータをGoogleカレンダーにインポートさせるために、iCalendar形式のデータをはき出させる 2

以前、「WordPressのカスタムフィールドのデータをGoogleカレンダーにインポートさせるために、iCalendar形式のデータをはき出させる」の記事で、本宅コンサデコンサのCSプレイヤーズの選手データの誕生日データを公開GoogleカレンダーにインポートさせるためのPHPコードを作成した。

このときは、直接、記事にPHPを記述していたのだが、その後、状況が変わって昨今では直接実行させるのが難しくなった。当時、Exec-PHPプラグイン、その後php code for postプラグインなどを用いて、それを実行していたが、最近ではそれも使用できなくなった。

なので、PHPコードをショートコード化させる必要があるのだが、function.phpを直接弄ってコードを転記したところ、度々落ちるので別の方法を考慮することにした。

今回用いたのは、Insert PHP Code Snippetプラグイン。


基本的に、「WordPressのカスタムフィールドのデータをGoogleカレンダーにインポートさせるために、iCalendar形式のデータをはき出させる」の記事で記載したPHPコードを転記。

・phpコードの最初の宣言文と、文末の終了文は記述せず。
・また、直接表示するHTMLコード部分などは、echoを用いて表示。

という感じで、書き換えたのが以下の文。それぞれの項目の意味は、先の「WordPressのカスタムフィールドのデータをGoogleカレンダーにインポートさせるために、iCalendar形式のデータをはき出させる」に記載しているのでそれを参照のこと。

このコードを使うときは、Insert PHP Code Snippetプラグイン指定のショートコードを記述し、PHPコードに自分でつけた名前を記述する。

<!--?php
echo 'BEGIN:VCALENDAR<br /-->‘;
echo ‘METHOD:PUBLISH‘;
echo ‘VERSION:2.0‘;
echo ‘X-WR-CALNAME:誕生日‘;
echo ‘PRODID:-//Apple Inc.//iCal 4.0.4//EN‘;
echo ‘X-WR-CALDESC:‘;
echo ‘X-APPLE-CALENDAR-COLOR:#E51717‘;
echo ‘X-WR-TIMEZONE:Asia/Tokyo‘;
echo ‘CALSCALE:GREGORIAN‘;

$posts = get_posts(‘numberposts=1000&post_type=players&orderby=meta_value_num&order=ASC’);
global $post;

if($posts): foreach($posts as $post): setup_postdata($post);
echo ‘BEGIN:VEVENT‘;
echo ‘TZID:Japan‘;
echo ‘CREATED:’;
list($year, $month, $day) = array( post_custom(‘Birthday-y’), post_custom(‘Birthday-m’), post_custom(‘Birthday-d’));
$nowDate = sprintf(“%04d%02d%02d”, $year, $month, $day);
print $nowDate;
echo ‘T’;
the_time(‘hms’);
echo ‘Z‘;
echo ‘UID:CSPLAYERS-CONSADECONSA-‘;
list($year, $month, $day) = array( post_custom(‘Birthday-y’), post_custom(‘Birthday-m’), post_custom(‘Birthday-d’));
$nowDate = sprintf(“%04d%02d%02d”, $year, $month, $day);
print $nowDate;
echo ‘-‘;
the_time(‘ymdhms’);

echo ‘
DTEND;TZID=Asia/Tokyo;VALUE=DATE:’;
list($year, $month, $day) = array( post_custom(‘Birthday-y’), post_custom(‘Birthday-m’), post_custom(‘Birthday-d’)+1);
$nowDate = sprintf(“%04d%02d%02d”, $year, $month, $day);
print $nowDate;

echo ‘
RRULE:FREQ=YEARLY‘;
echo ‘TRANSP:OPAQUE‘;
echo ‘SUMMARY:【誕生日】’;
the_title();

echo ‘
LOCATION:‘;
echo ‘DESCRIPTION:CSプレイヤーズ: ‘;
the_permalink();

echo ‘
DTSTART;TZID=Asia/Tokyo;VALUE=DATE:’;
list($year, $month, $day) = array( post_custom(‘Birthday-y’), post_custom(‘Birthday-m’), post_custom(‘Birthday-d’));
$nowDate = sprintf(“%04d%02d%02d”, $year, $month, $day);
print $nowDate;

echo ‘
DTSTAMP:’;
list($year, $month, $day) = array( post_custom(‘Birthday-y’), post_custom(‘Birthday-m’), post_custom(‘Birthday-d’));
$nowDate = sprintf(“%04d%02d%02d”, $year, $month, $day);
print $nowDate;
echo ‘T’;
the_time(‘hms’);
echo ‘Z’;
echo ‘
SEQUENCE:40‘;
echo ‘END:VEVENT‘;

endforeach; endif;
echo ‘END:VCALENDAR’;

?>

そうすると、ブラウザ上の記事では、以下のような感じで表示される。これをコピーして、テキストファイルを作成(文字コード:UTF-8、改行:CRLF、拡張子:ics)。Googleカレンダーにインポートさせる。

BEGIN:VCALENDAR
METHOD:PUBLISH
VERSION:2.0
X-WR-CALNAME:誕生日
PRODID:-//Apple Inc.//iCal 4.0.4//EN
X-WR-CALDESC:
X-APPLE-CALENDAR-COLOR:#E51717
X-WR-TIMEZONE:Asia/Tokyo
CALSCALE:GREGORIAN
BEGIN:VEVENT
TZID:Japan
CREATED:19600306T100146Z
UID:CSPLAYERS-CONSADECONSA-19600306-960101100146
DTEND;TZID=Asia/Tokyo;VALUE=DATE:19600307
RRULE:FREQ=YEARLY
TRANSP:OPAQUE
SUMMARY:【誕生日】ペレイラ
LOCATION:
DESCRIPTION:CSプレイヤーズ: https://www.consadeconsa.com/players/1996/%e3%83%9a%e3%83%ac%e3%82%a4%e3%83%a9/
DTSTART;TZID=Asia/Tokyo;VALUE=DATE:19600306
DTSTAMP:19600306T100146Z
SEQUENCE:40
END:VEVENT

(以下続く)

END:VCALENDAR



コメントを残す

メールアドレスが公開されることはありません。 が付いている欄は必須項目です