WordPressのカスタム投稿タイプを使い出勤表を作成した。
出勤表用のカスタム投稿ポスト(schedule)をfunction.phpで定義する。カスタム投稿ポスト(schedule)にたいしてタイトルとカスタムフィールド(出勤表入力域)を定義する。
function.phpには出勤表を出力する関数も定義する。この関数はカスタム投稿ポスト(schedule)表示用テンプレート(schedule.php)から呼び出される。
<?php
/**
* カスタム投稿タイプ schedule
* 出勤表用のカスタム投稿タイプscheduleを定義
*/
add_action('init', 'create_schedule_sequence');
function create_schedule_sequence () {
$labels = array(
'name' => '出勤表',
'singular_name' => '出勤表',
'add_new' => '出勤表を追加',
'add_new_item' => '新しい出勤表を追加',
'edit_item' => '出勤表を編集',
'new_item' => '新しい出勤表',
'view_item' => '出勤表を編集',
'search_items' => '出勤表を探す',
'not_found' => '出勤表はありません',
'not_found_in_trash' => 'ゴミ箱に出勤表はありません',
'parent_item_colon' => ''
);
$args = array(
'labels' => $labels,
'public' => true,
'capability_type' => 'post',
'hierarchical' => false,
'supports' => array(
'title'
),
'register_meta_box_cb' => 'schedule_meta_box' /* カスタムフィールド */
);
register_post_type('schedule', $args);
/**
* カスタムフィールド
*/
function schedule_meta_box($post){
add_meta_box('shedule_meta', '出勤表入力域', 'schedule_meta_func', 'schedule', 'normal', 'high');
}
function schedule_meta_func($post, $box){
/**
* カスタムフィールドの値を取得する
*/
// 出勤表
$year = intval(get_post_meta($post->ID, 'year', true));
$month = intval(get_post_meta($post->ID, 'month', true));
$days = array();
for ($i=1; $i<=31; $i++) {
$key = 'day-' . $i;
$value = get_post_meta($post->ID, $key, true);
if (($value == 'present') || ($value == 'holiday') || ($value == 'close')) {
$days[$i] = $value;
} else {
// エラー処理
}
}
/*nonce セキュリティで必要 */
echo wp_nonce_field('schedule_meta', 'my_meta_nonce');
/*
* カスタムフィールド用のフォームを表示
*/
// 年
echo '<p>年 : <select name="year"><option value="-">--</option>';
for ($y=2011; $y<2020; $y++){
if ($y == $year) {
echo '<option value="' . $year . '" selected="selected">' . $year. '</option>';
} else {
echo '<option value="' . $y . '">' . $y . '</option>';
}
}
echo '</select> ';
// 月
echo '月 : <select name="month"><option value="-">--</option>';
for ($m=1; $m<13; $m++){
if ($m == $month) {
echo '<option value="' . $month . '" selected = "selected">' . $month. '</option>';
} else {
echo '<option value="' . $m . '">' . $m . '</option>';
}
}
echo '</select></p>';
// 日付
echo '<p>';
for ($i=1, $j=1; $i<=31; $i++) {
($i <= 9) ? $date = '0' . $i : $date = $i;
if ($days[$i] == 'close') {
echo $date . '日' .
'<select name="day-' . $i. '" />' .
'<option value="present">--</option>' .
'<option value="close" selected="selected">定休</option>' .
'<option value="holiday">休日</option>' .
'</select>  ';
} else if ($days[$i] == 'holiday') {
echo $date . '日' .
'<select name="day-' . $i. '" />' .
'<option value="present">--</option>' .
'<option value="close">定休</option>' .
'<option value="holiday" selected="selected">休日</option>' .
'</select>  ';
} else {
echo $date . '日' .
'<select name="day-' . $i. '" />' .
'<option value="present">--</option>' .
'<option value="close">定休</option>' .
'<option value="holiday">休日</option>' .
'</select>  ';
}
// 入力フィールドを5個ずつ表示
if ($j == 5) {
echo '<br />';
$j = 1;
} else {
$j++;
}
}
echo '</p>';
}
/*
* カスタムフィールド保存処理
*/
add_action('save_post', 'schedule_meta_update');
function schedule_meta_update($post_id){
$my_meta_nonce = isset($_POST['my_meta_nonce']) ? $_POST['my_meta_nonce'] : null;
if (!wp_verify_nonce( $my_meta_nonce, 'schedule_meta')) {
return $post_id;
}
if (defined('DOING_AUTOSAVE') && DOING_AUTOSAVE) {
return $post_id;
}
if ('schedule' == $_POST['post_type']) {
if(!current_user_can('edit_post', $post_id)) {
return $post_id;
}
} else {
return $post_id;
}
// カスタムフィールド値の取得
$year = $_POST['year'];
$month = $_POST['month'];
$days = array();
for ($i=1; $i<=31; $i++) {
$key = 'day-' . $i;
$days[$i] = $_POST[$key];
}
/*
* カスタムフィールド更新処理
*/
if($year == '') {
delete_post_meta($post_id, 'year');
}
else {
update_post_meta($post_id, 'year', $year);
}
if($month == '') {
delete_post_meta($post_id, 'month');
}
else {
update_post_meta($post_id, 'month', $month);
}
for ($i=1; $i<=31; $i++) {
$key = 'day-' . $i;
if ($days[$i] == '') {
delete_post_meta($post_id, $key);
} else {
update_post_meta($post_id, $key, $days[$i]);
}
}
}
}
[/php]
<h3>出勤表出力関数(function.php)</h3>
[php]
/*
* カレンダー出力関数
* @param $id 投稿ID
* @param $y 年 数字4桁
* @param $m 月 数字1~13
* @param $days 日付 配列 添え字[1~31] 値[空, close, holiday]
*/
function drawSchedule ($id, $y, $m, $days) {
// $y 年 $m 月
$t = mktime(0, 0, 0, $m, 1, $y); //$y年$m月1日のUNIXTIME
$w = date('w', $t); //1日の曜日(0:日~6:土)
$n = date('t', $t); //$y年$m月の日数
if ($m<10) {
$m = "0" . $m;
}
print <<<HTML
<table class="schedule">
<caption>{$y}年{$m}月</caption>
<tr>
<th class="textRed">日</th>
<th>月</th>
<th>火</th>
<th>水</th>
<th>木</th>
<th>金</th>
<th class="textBlue">土</th>
</tr>
HTML;
for($i=1-$w; $i<= $n+7; $i++){
if ((($i + $w) % 7) == 1) {
print "<tr>n";
}
// 日付が有効な場合の処理
if ((0 < $i) && ($i <= $n)) {
$value = $days[$i];
$html = "<td";
// 曜日の取得
$hizuke = mktime(0, 0, 0, $m, $i, $y); //$y年$m月$i日のUNIXTIME
$youbi = date('w', $hizuke); //1日の曜日(0:日~6:土)
// 定休日
if ($value == 'close') { // 定休日
$html .= ' class="close"';
} else if ($value == 'holiday') { // 休日
$html .= ' class="holiday"';
} else if ($youbi == 0) { // 日曜
$html .= ' class="sun"';
}
$html .= '>' . $i . '</td>';
print($html);
} else {
print "<td> </td>n";
}
if ((($i + $w ) % 7 ) == 0 ) {
print "</tr>n";
if ( $i >= $n ) {
break;
}
}
}
print "</table>n";
}
カスタムフィールドを取得してdrawSchedule関数を呼び出す。
<?php if(have_posts()):while(have_posts()):the_post() ?>
<h3>出勤表</h3>
<?php
// カスタムフィールドの値を取得
$year = get_post_meta($post->ID, 'year' , true);
$month = get_post_meta($post->ID, 'month', true);
if (!intVal($year) || !intVal($month)) {
echo '<p>只今出勤表を準備しています。</p>';
} else {
$days= array();
for ($i=1; $i<=31; $i++) {
$key = 'day-' . $i;
$days[$i] = get_post_meta($post->ID, $key, true);
}
// 出勤表出力関数呼び出し
drawSchedule($post->ID, $year, $month, $days);
}
?>
<?php endwhile;endif; ?>
table.schedule {
float: left;
margin-right: 10px;
}
td.close {
background: #ccc;
}
td.sun {
background: #ffc0cb;
}
td.holiday {
background: #ff0000;
}
» WordPressを使ってカレンダー型の出勤表を作成 : WordPress
[1] カスタム投稿タイプ用テンプレートschedule.phpから呼び出す。
[…] WordPressのカスタム投稿タイプ・カスタムフィールドを使った出勤表 : WordPress […]
改行と段落タグは自動で挿入されます。
メールアドレスは表示されません。