概要
tec_events_custom_tables_v1_update_post_before
アクションは、The Events Calendar プラグイン内でイベント情報がデータベースに保存される直前に実行されるフックです。このフックを使用することで、イベントの投稿処理をカスタマイズしたり、特定の条件に基づいてデータを変更したりすることが可能です。このアクションは、投稿データを操作する際の柔軟性を提供するため、特定の機能実装時によく利用されます。
よく使われる機能
- イベント情報のバリデーション
- カスタムメタデータの追加
- フィールド値の変更
- 特定のユーザー権限の確認
- 機能のロジックに基づいたカスタム処理
- イベントのカスタムステータス設定
構文
add_action('tec_events_custom_tables_v1_update_post_before', 'your_custom_function', 10, 2);
パラメータ
$post_data
(配列): 保存される投稿のデータ。$post_id
(整数): 保存される投稿のID。
戻り値
特に戻り値はなく、フック内でデータを変更することで直接の影響を与える。
使用可能なプラグインおよびバージョン
- プラグイン: The Events Calendar
- バージョン: 5.0以上
- ワードプレスバージョン: 5.0以上
この関数のアクションでの使用可能性
アクション | 使用例 |
---|---|
mu_plugin_loaded | |
registered_post_type | |
plugins_loaded | |
wp_roles_init | |
setup_theme | |
after_setup_theme | |
set_current_user | |
init | |
register_sidebar | |
wp_loaded | |
send_headers | |
parse_query | |
pre_get_posts | |
wp | |
template_redirect | |
get_header | |
wp_head |
サンプルコード
サンプルコード1
function custom_event_meta($post_data, $post_id) {
if (isset($post_data['custom_field'])) {
$post_data['custom_field'] = sanitize_text_field($post_data['custom_field']);
}
}
add_action('tec_events_custom_tables_v1_update_post_before', 'custom_event_meta', 10, 2);
説明: イベント投稿前に、カスタムフィールドの値をサニタイズして不正なデータが保存されないようにします。
サンプルコード2
function set_event_status($post_data, $post_id) {
if ($post_data['event_date'] < current_time('mysql')) {
$post_data['post_status'] = 'draft';
}
}
add_action('tec_events_custom_tables_v1_update_post_before', 'set_event_status', 10, 2);
説明: イベントの日付が過去の場合、投稿ステータスをドラフトに変更します。
サンプルコード3
function log_event_update($post_data, $post_id) {
error_log('Event updated: ' . $post_id);
}
add_action('tec_events_custom_tables_v1_update_post_before', 'log_event_update', 10, 2);
説明: イベントが更新されるたびに、そのイベントIDをエラーログに記録します。
サンプルコード4
function validate_event_dates($post_data, $post_id) {
if (strtotime($post_data['event_start_date']) >= strtotime($post_data['event_end_date'])) {
wp_die('End date must be after start date.');
}
}
add_action('tec_events_custom_tables_v1_update_post_before', 'validate_event_dates', 10, 2);
説明: イベントの開始日が終了日と同じかそれ以前である場合、エラーメッセージを表示して投稿を中止します。
サンプルコード5
function auto_set_event_category($post_data, $post_id) {
wp_set_post_terms($post_id, 'default-category', 'tribe_events_cat');
}
add_action('tec_events_custom_tables_v1_update_post_before', 'auto_set_event_category', 10, 2);
説明: 投稿保存前に、デフォルトのカテゴリーをイベントに自動で設定します。