2026年9月19日
2026年9月19日
WordPressのbulk_actionsカスタムアクションを追加する
はじめに
WordPressでは、投稿や固定ページなどに対して一括操作を行う機能としてbulk_actionsが提供されています。この機能は管理画面から複数のアイテムを一度に処理する際に非常に便利です。しかし、デフォルトで用意されているアクションだけでは特定のニーズに対応できないことがあります。
本記事では、WordPressのbulk_actions機能に独自のカスタムアクションを追加する方法について説明します。具体的にはPHPコードを使用して新しいbulk_actionsメニュー項目を追加し、その実行結果として必要な処理を行うための手順を紹介します。
症状・背景
このテーマが必要になる主な場面:
- <特定の投稿タイプに対する一括操作が必要な場合>
- <カスタムプラグインを通じて提供する一括操作機能がある場合>
- <既存のbulk_actionsを拡張したい場合>
- <管理者権限を持つユーザーに対してより細かい管理ツールを提供したい場合>
手順・設定方法
ステップ1: 基本操作のサブタイトル
# フィルターを実装して新規 bulk_action を登録します。
add_filter( 'bulk_actions-edit_your_custom_post_type', 'your_custom_bulk_actions' );
function your_custom_bulk_actions($actions) {
$custom_action = 'my-custom-action';
// 新しい bulk action の追加
$actions[$custom_action] = __( 'カスタムアクション', 'textdomain' );
return $actions;
}
ステップ2: 主要オプション/設定
# アクションハンドラを定義します。
add_action( 'pre_post_update', 'handle_custom_bulk_actions', 10, 3 );
function handle_custom_bulk_actions($post_id, $post, $update) {
if ( !isset($_REQUEST['action']) || $_REQUEST['action'] !== 'my-custom-action' ) return;
// リクエストパラメータをチェック
if ( empty($_REQUEST['post']) ) wp_die('投稿が指定されていません');
foreach( (array) $_REQUEST['post'] as $post_id ) {
$post = get_post($post_id);
if ('your_custom_post_type' !== $post->post_type) continue;
// 実際の処理をここに記述
update_post_meta($post_id, 'custom_field', 'custom_value');
}
}
ステップ3: 応用/組み合わせ
# フィルターハンドラ内でカスタム bulk_action を追加します。
add_filter( 'bulk_actions-edit_your_custom_post_type', 'your_custom_bulk_actions' );
function your_custom_bulk_actions($actions) {
$custom_action = 'my-custom-action';
// 新しい bulk action の追加
$actions[$custom_action] = __( 'カスタムアクション', 'textdomain' );
return $actions;
}
# アクションハンドラを定義します。
add_filter( 'pre_post_update', 'handle_custom_bulk_actions', 10, 3 );
function handle_custom_bulk_actions($post_id, $post, $update) {
if ( !isset($_REQUEST['action']) || $_REQUEST['action'] !== 'my-custom-action' ) return;
// リクエストパラメータをチェック
if ( empty($_REQUEST['post']) ) wp_die('投稿が指定されていません');
foreach( (array) $_REQUEST['post'] as $post_id ) {
$post = get_post($post_id);
if ('your_custom_post_type' !== $post->post_type) continue;
// 実際の処理をここに記述
update_post_meta($post_id, 'custom_field', 'custom_value');
}
}
ステップ4: 実践/トラブルシュート/監視
# アクションハンドラ内でログ出力を追加します。
add_filter( 'pre_post_update', 'handle_custom_bulk_actions', 10, 3 );
function handle_custom_bulk_actions($post_id, $post, $update) {
if ( !isset($_REQUEST['action']) || $_REQUEST['action'] !== 'my-custom-action' ) return;
// リクエストパラメータをチェック
if ( empty($_REQUEST['post']) ) wp_die('投稿が指定されていません');
foreach( (array) $_REQUEST['post'] as $post_id ) {
$post = get_post($post_id);
if ('your_custom_post_type' !== $post->post_type) continue;
// 実際の処理をここに記述
update_post_meta($post_id, 'custom_field', 'custom_value');
// ログ出力
error_log("Custom action performed on post ID: {$post_id}");
}
}
注意事項
- <実践的な注意点1>: bulk_actionsの追加時には、適切な権限チェックを行い、管理者ユーザーのみが利用できるようにすること。
- <実践的な注意点2>: ユーザーインターフェースを改善するためには、カスタムbulk_actionsの説明文や指示文も翻訳ファイルに登録しておきましょう。
- <セキュリティ上の注意>: bulk actionsを通じて行われる操作は、特にデータ削除などの危険なアクションについてはユーザーに対して警告を表示し、確認プロンプトを設けるべきです。
- <パフォーマンス/運用上の注意>: ユーザーが大量のアイテムに対してbulk actionを行う場合、処理時間が長くなる可能性があるため、適切に最適化を行うことが重要です。
まとめ
1. フィルターハンドラを追加: 新しい bulk_action を登録するためには関連するフィルターを使用します。
2. アクションハンドラの定義: フィルターハンドラで設定した bulk action の実際の処理を記述します。
3. ユーザーインターフェースの改善: 定義した bulk_action について、説明文や指示文を翻訳ファイルに登録してわかりやすい操作を提供します。
4. 安全対策の実装: データ削除などの危険なアクションについてはユーザーに対して警告を表示し、確認プロンプトを設けます。
5. パフォーマンス最適化: 大量のアイテムに対する bulk action の処理時間を短縮するため、適切に最適化を行います。
関連記事: