WordPressのプラグインでDayOfYearを表示するものを作成してみました
プラグイン作成準備
最初にディレクトリを作成します。今回は自分用に作成するので特に名前も気にしていないのですが、公開する場合には名前のかぶらないものにすることをお勧めします。
1 | $ mkdir utbt_doycalendar |
このディレクトリにplugin.phpファイルを作成しヘッダを作成します。
この辺りはお決まりですので、このあたりのサイトに書かれている通りにします
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 | <?php /* Plugin Name: Doy Calendar Plugin URI: http://labo.utsubopeo.com/ Description: Display Doy Calendar Version: 1.0.0 Author: k-utsubo Author URI: http://labo.utsubopeo.com/ License: GPL2 */ ?> <?php /* Copyright 2016 k-utsubo This program is free software; you can redistribute it and/or modify it under the terms of the GNU General Public License, version 2, as published by the Free Software Foundation. This program is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for more details. You should have received a copy of the GNU General Public License along with this program; if not, write to the Free Software Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA */ ?> |
こんな感じで十分でしょうか?
ショートコード対応
今回はWordpressのプラグインとしてショートコードをページに記入レバ表示できるような仕様にします。
また、ページには1年間分のカレンダーを表示し、年をテキストボックスでフォーム入力し、Submitすることにより切り替えます。
この際に、FormからはGetパラメタで送信し、プラグイン内でGetパラメタを取得して年を切り替えます。
カレンダー作成
最初から作ってもいいのですが、こちらを参考にさせていただきました。
ソースコード
で、完成したコードがこちら
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 | <?php /* Plugin Name: Doy Calendar Plugin URI: http://labo.utsubopeo.com/ Description: Display Doy Calendar Version: 1.0.0 Author: k-utsubo Author URI: http://labo.utsubopeo.com/ License: GPL2 */ ?> <?php /* Copyright 2016 k-utsubo This program is free software; you can redistribute it and/or modify it under the terms of the GNU General Public License, version 2, as published by the Free Software Foundation. This program is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for more details. You should have received a copy of the GNU General Public License along with this program; if not, write to the Free Software Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA */ ?> <?php date_default_timezone_set('Asia/Tokyo'); // php5.1以降のWarning対応 function utbt_calendar_func($atts,$content=''){ $year = (isset($_GET["utbt_year"]) && $_GET["utbt_year"] != "") ? $_GET["utbt_year"] : ""; $year = htmlspecialchars($year, ENT_QUOTES); $cal= new UtbtCalendar; return $cal->calendar($year); } add_shortcode('utbt_calendar','utbt_calendar_func'); class UtbtCalendar{ function calendar($year=''){ if (empty($year) && empty($month)) { $year = date('Y'); } $html="<form method='get' action='".$_SERVER['REQUEST_URI']."'>Input Yea r:<input type='text' name='utbt_year' value='$year'><input type='submit'>"; for($i=1;$i<=12;$i++){ $html.=$this->calendar_exec($year,$i); } $html.="</form>"; return $html; } //http://shanabrian.com/web/php_calendar.php private function calendar_exec($year = '', $month = '') { $tab=""; $str=""; if (empty($year) && empty($month)) { $year = date('Y'); $month = date('n'); } //月末の取得 $l_day = date('j', mktime(0, 0, 0, $month + 1, 0, $year)); //初期出力 $html = <<<EOM <table class="calendar"> <caption>{$year}/{$month}</caption> <tr> <th><font color="#FF0000">Sun</font></th> <th>Mon</th> <th>Tue</th> <th>Wed</th> <th>Thr</th> <th>Fri</th> <th><font color="#0000FF">Sat</font></th> </tr>\n EOM; $lc = 0; // 月末分繰り返す for ($i = 1; $i < $l_day + 1;$i++) { $classes = array(); $class = ''; // 曜日の取得 $week = date('w', mktime(0, 0, 0, $month, $i, $year)); // 曜日が日曜日の場合 if ($week == 0) { $str .= $tab."\t\t<tr>\n"; $lc++; } // 1日の場合 if ($i == 1) { if($week != 0) { $str .= $tab."\t\t<tr>\n"; $lc++; } $str .= $this->repeatEmptyTd($week); } if ($week == 6) { array_push($classes, '#0000FF'); } else if ($week == 0) { array_push($classes , '#FF0000'); } if (count($classes) > 0) { $class = '"'.implode(' ', $classes).'"'; } $doy = date('z', mktime(0, 0, 0, $month, $i, $year))+1; $str .= $tab."\t\t\t".'<td><font color='.$class.'>'.$i.'('.$doy.')</font></td>'."\n"; // 月末の場合 if ($i == $l_day) { $str .= $this->repeatEmptyTd(6 - $week); } // 土曜日の場合 if ($week == 6) { $str .= $tab."\t\t</tr>\n"; } } $html .=$str; $html .= "</table>\n"; return $html; } private function repeatEmptyTd($n = 0) { return str_repeat("\t\t<td> </td>\n", $n); } } ?> |
リリース
できたコードをディレクトリごとZIPファイルにし、Wordpressのプラグインに追加します。
また、ページにはこんな感じで書けば無事カレンダーが表示されます
1 | [utbt_calendar] |
デモ
作成したデモサイトはこちらになります