プラグインAdvanced custom fields(ACF)のacf/update_fieldアクションの使用方法・解説

概要

acf/update_field アクションは、Advanced Custom Fields (ACF) プラグインにおいて、特定のカスタムフィールドが更新されるたびにトリガーされるイベントです。このアクションを使用することで、保存する前に $field 設定をフィルタリングしたり、特定の条件に基づいてフィールドの値を処理したりすることが可能です。よく使われるシナリオには以下のようなものがあります。

  1. フィールドの値をチェックし、特定の条件を満たさない場合にエラーメッセージを表示する。
  2. カスタムフィールドの値を自動的に変換する(例:特定の形式にフォーマットする)。
  3. 特定のユーザーに対してフィールドのアクセスを制限する。
  4. フィールド更新時に関連するコンテンツを同時に更新する。
  5. 特定のメタデータやフィールド値を記録やログに保存する。
  6. 外部APIとの連携を行い、フィールドエントリに基づいてデータを送信または取得する。

構文

add_action('acf/update_field', 'your_function_name', 10, 4);

パラメータ

  • $value (mixed): 保存されるフィールドの値。
  • $post_id (int): 更新が行われた投稿のID。
  • $field (array): 更新されるカスタムフィールドの設定。
  • $options (array): オプションの引数(例:ACFのバージョン、ユーザーなど)。

戻り値

このアクション自体は何も返しませんが、フィルタリングされた $field を利用して、カスタムフィールドの値を操作することが可能です。

ACFのバージョン

このアクションは、ACF プラグインの全バージョンで使用可能ですが、特定の機能はバージョンによって異なる場合があります。

WordPressのバージョン

WordPress 4.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: フィールド値のバリデーション

以下のサンプルは、特定の条件に従ってカスタムフィールドの値を検証し、無効な場合はエラーメッセージを表示する例です。

add_action('acf/update_field', 'my_custom_field_validation', 10, 4);
function my_custom_field_validation($value, $post_id, $field, $options) {
    if ($field['name'] === 'my_field' && $value < 0) {
        acf_add_validation_error($field['key'], '値は0以上でなければなりません。');
    }
}

引用元: https://www.advancedcustomfields.com/resources/acf-validation/

サンプル2: 値の自動変換

このコードは、カスタムフィールドの更新時に値を特定の形式に自動的に変換します。

add_action('acf/update_field', 'auto_convert_value', 10, 4);
function auto_convert_value($value, $post_id, $field, $options) {
    if ($field['name'] === 'price') {
        return number_format($value, 2); // Priceを小数点以下2桁にフォーマット
    }
    return $value;
}

引用元: https://www.advancedcustomfields.com/resources/acf-function-reference/

サンプル3: ユーザーによるアクセス制限

このサンプルは、特定のユーザーのみがカスタムフィールドを編集できるようにするものです。

add_action('acf/update_field', 'restrict_field_access', 10, 4);
function restrict_field_access($value, $post_id, $field, $options) {
    if (!current_user_can('edit_others_posts')) {
        return new WP_Error('access_denied', 'あなたにはこのフィールドを編集する権限がありません。');
    }
    return $value;
}

引用元: https://www.advancedcustomfields.com/resources/using-acf-with-user-roles/

サンプル4: 同時更新

このコードは、カスタムフィールドが更新されると、関連する別のフィールドも同時に更新する例です。

add_action('acf/update_field', 'update_related_field', 10, 4);
function update_related_field($value, $post_id, $field, $options) {
    if ($field['name'] === 'main_field') {
        update_field('related_field', $value, $post_id);
    }
    return $value;
}

引用元: https://www.advancedcustomfields.com/resources/updating-other-fields/

サンプル5: APIとの連携

このサンプルは、カスタムフィールドが更新されると、外部APIにデータを送信する例です。

add_action('acf/update_field', 'send_data_to_api', 10, 4);
function send_data_to_api($value, $post_id, $field, $options) {
    $response = wp_remote_post('https://api.example.com/update', array(
        'body' => json_encode(array('field' => $value)),
        'headers' => array('Content-Type' => 'application/json')
    ));
    return $value;
}

引用元: https://developer.wordpress.org/plugins/http-api/

この関数について質問する


上の計算式の答えを入力してください