Support

Account

Home Forums Backend Issues (wp-admin) Persisting ACF Custom Field Type Value to a Database

Helping

Persisting ACF Custom Field Type Value to a Database

  • Using the ACF Starter Kit, I created a custom field type labeled “Plugin Host” (field name is ‘ph’), which is associated with a custom post type I created. I read through the ACF documentation but remain uncertain as how best to persist an instance of the custom field type’s value in the database set up for my WordPress web site. Here is some of the relevant code in question:

    // Create a default 'value' key/value pair within the constructor function
    $this->defaults = array(
      'value' => array(
                   'host' => 'uvi',
                   'version' => '1.0'
                 );
    );
    //Set up the GUI for displaying the custom field in a post's editor
    function render_field( $field ) {
      $field_value_keys = array_keys($field['value']);
    
      $host_key = $field_value_keys[0];
      $host_value = $field['value']['host'];
      $version_key = $field_value_keys[1];
      $version_value = $field['value']['version'];
    
      ?>
      <label
        for="plugin-<?php echo esc_attr($host_key); ?>">
        Plugin Host
      </label>
      <input
        type="text"
        id="plugin-<?php echo esc_attr($host_key); ?>"
        name="plugin-<?php echo esc_attr($host_key); ?>"
        value="<?php echo esc_attr($host_value) ?>"
      />
    
      <label
        for="plugin-version-<?php echo esc_attr($version_key); ?>">
        Plugin Host Version
      </label>
      <input
        type="text"
        id="plugin-<?php echo esc_attr($version_key); ?>"
        name="plugin-host-<?php echo esc_attr($version_key); ?>"
        value="<?php echo esc_attr($version_value) ?>"
      />
      <?php
    }

    The custom field type’s class, which extends the ‘acf_field’ class, includes both ‘update_field’ and ‘update_value’ methods, and each method includes the following comments, respectively:

    “This filter is applied to the $field before it is saved to the database”
    “This filter is applied to the $value before it is saved in the db”

    Since they are described as filters, I assume it means these functions are designed for use with the ACF WordPress filters ‘acf/update_field’ (see: https://www.advancedcustomfields.com/resources/acf-update_field/) and ‘acf/update_value’ (see: https://www.advancedcustomfields.com/resources/acf-update_value/).

    For the purpose of this post, I am interested only in updating the custom field type’s value in the underlying database’s postmeta table. If I am to use the ‘acf/field_value’ filter, then I assume that in this case it makes the most sense to use the ‘add_filter’ filter as follows:

    add_filter('acf/update_value/type=ph', 'my_acf_update_value_cb_func', 10, 4);

    Unfortunately, I am at a loss on how to implement this functionality. Here is what I have in mind, and I would appreciate your feedback for this approach.

    First, I assume the add_filter’s ‘acf/update_value’ hook is triggered after clicking the ‘update’ button in a post’s editor. Is this correct? Second, I assume the hook’s function will pass the $value modified value captured in the HTML tag for the custom field’s value in the post editor, the $post_id from the current post where the custom field value is contained, the $field value from the custom field’s identifier (either the ‘id’, the ‘key’ or the ‘name’), and the $original value as the custom field’s value prior to its modification. If so, then I thought about defining the callback function like this:

    function my_acf_update_value_cb_func($value, $post_id, $field, $original) {
      if(!empty($value)) {
        $result = '';
    
        // $ph_field is defined elsewhere using ACF's 'get_field' function
        $result = $ph_field->update_value($value, $post_id, $field);
    
        return $result;
      }
    }

    The ‘ph’ custom field class’ ‘update_value’ method would be defined as follows:

    function update_value($value, $post_id, $field) {
      $result = false;
    
      if(update_post_meta($post_id, 'value', $value)) {
        $result = true;
      }
    
      return $result;
    }

    In the interest of separation of concerns as a coding practice, the custom post editor’s ‘ph’ HTML render, the ‘acf/update_field’ WordPress filter and the ‘ph’ class’ ‘update_value’ method act, respectively, as the view, controller and model of an MVC design pattern.

    I have seen several posts here and elsewhere that advocate for persisting a custom field’s instance to a JSON file, but doing so only would require serializing to JSON within the ‘update_value’ method. For the purpose of this post, I am specifically interested in understanding how best to persist a custom field’s value to a database.

    I think the code I presented makes sense in principle, but I am not sure if the implementation is accurate. Also, I deliberately ignored validation, although this would be addressed by invoking the ‘ph’ class’ ‘validate_value’ method from within the ‘update_value’ method.

    Your I appreciate your recommendations. Thank you.

  • When you say “ACF Starter Kit”, if you are talking about the ACF Field Type Template then some explanation.

    There is not need to actually create filters for the methods that are included in your new field type. These filters and actions will be automatically called by ACF because all of these actions are added by ACF for every field type.

    ACF also uses the same filters that are supplied for use to use to do what it does.

    For example, the acf/update_field hook not only calls custom filters added by others in themes but also runs these filters associated with each field type.

    I’m not exactly sure what your looking for, but there is not need for you to actually update the post meta with a value in your field. ACF will do this automatically.

    $original_value will not contain the old value from the DB, this will contain the value that was originally submitted while $value will contain the results of any other acf/update_value filters that have acted on the submitted value.

Viewing 2 posts - 1 through 2 (of 2 total)

You must be logged in to reply to this topic.