概要
フィルタ wpforms_post_submissions_post_args
は、WPForms プラグインが新しい投稿を作成する前のデータをカスタマイズするために使用されます。このフィルタを利用することで、投稿データやカスタムフィールドの設定、その他の投稿関連のプロパティを変更することが可能です。一般的に、以下のような機能を実装する際にこのフィルタが利用されます。
- 新しい投稿にカスタムメタデータを追加
- 投稿のステータスを変更(例:下書き、公開)
- 特定の投稿タイプに対してのみデータを適用
- 投稿のタイトルやコンテンツを変更
- 投稿の公開日や更新日をカスタマイズ
- ユーザーに基づいて異なる投稿設定を適用
構文
add_filter('wpforms_post_submissions_post_args', 'custom_post_args', 10, 3);
function custom_post_args($post_args, $form_data, $form_id) {
// ここで$post_argsをカスタマイズする
return $post_args;
}
パラメータ
$post_args
:作成される投稿の引数を含む配列。$form_data
:送信されたフォームデータを含む配列。$form_id
:フォームのID。
戻り値
- カスタマイズされた投稿の引数を含む配列。
使用可能なプラグインバージョン
WPForms バージョン 1.0 以上
使用可能なワードプレスのバージョン
WordPress バージョン 4.9 以上
この関数のアクションでの使用可能性
アクション | 使用可能性 |
---|---|
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: カスタムメタデータを追加
add_filter('wpforms_post_submissions_post_args', 'add_custom_meta', 10, 3);
function add_custom_meta($post_args, $form_data, $form_id) {
$post_args['meta_input'] = array(
'custom_meta' => 'カスタムメタデータの値',
);
return $post_args;
}
このコードは、新しい投稿にカスタムメタデータを追加します。
サンプルコード 2: 投稿のステータスを下書きに変更
add_filter('wpforms_post_submissions_post_args', 'change_post_status_to_draft', 10, 3);
function change_post_status_to_draft($post_args, $form_data, $form_id) {
$post_args['post_status'] = 'draft';
return $post_args;
}
このコードは、新しい投稿のステータスを「下書き」に変更します。
サンプルコード 3: 投稿タイプをカスタム投稿タイプに設定
add_filter('wpforms_post_submissions_post_args', 'set_custom_post_type', 10, 3);
function set_custom_post_type($post_args, $form_data, $form_id) {
$post_args['post_type'] = 'custom_post_type';
return $post_args;
}
このコードは、新規作成される投稿のタイプを「カスタム投稿タイプ」に設定します。
サンプルコード 4: 投稿のタイトルをカスタマイズ
add_filter('wpforms_post_submissions_post_args', 'customize_post_title', 10, 3);
function customize_post_title($post_args, $form_data, $form_id) {
$post_args['post_title'] = 'カスタマイズされた投稿のタイトル';
return $post_args;
}
このコードは、新しく作成される投稿のタイトルをカスタマイズします。
サンプルコード 5: 投稿の公開日を変更
add_filter('wpforms_post_submissions_post_args', 'change_post_publish_date', 10, 3);
function change_post_publish_date($post_args, $form_data, $form_id) {
$post_args['post_date'] = '2023-10-01 00:00:00';
$post_args['post_date_gmt'] = '2023-10-01 00:00:00';
return $post_args;
}
このコードは、新規作成される投稿の公開日を指定された日付に変更します。