eduardo-marcolino / acf-fields-in-custom-table

Stores ACF custom fields in a custom table instead of WordPress' core meta tables
27 stars 12 forks source link

Advanced Custom Fields PRO #11

Closed adulovic closed 3 years ago

adulovic commented 3 years ago

WordPress v5.7.1 Advanced Custom Fields PRO v5.9.5 Advanced Forms v1.8.0 Sample table fields used: text, email, radio button, date time picker

Enabling ACF: Fields in Custom Tables creates a table with the prefix wpacf, But it does not insert any data sent by the forms. (Seems like the hook acf/update_field_group is working, but the acf/save_post isn't.)

Is this a know issue with the PRO version? Am I missing some important step? How can I debug?

eduardo-marcolino commented 3 years ago

I didn't test it with ACF PRO yet. How are you saving the data? Using the regular wp-admin interface ?

adulovic commented 3 years ago

users submitting data through forms on the frontend

eduardo-marcolino commented 3 years ago

Oh Right. At this moment the plugin only add a hook to the acf/save_post action. I don't know if you are using a plugin like ACF Frontend or doing the implementation by yourself but it's probably using the update_field function which will not trigger the acf/save_post action.

adulovic commented 3 years ago

Am just doing cleanup duty after another programmer, seems like a vanilla install. Thanks for the tip, I'll look into that and report back if solved (in case anyone else has the same issue).

adulovic commented 3 years ago

So, did a quick check and each form has its own submit function and they all use update_field. As an example, there is a function send_states($parameters) {...}. I tried adding the line add_action('acf/save_post', 'send_states'); before (and after) the function, but it still won't save into the custom table. I also tried them with priority 5 and priority 20, no luck... Any suggestions on what else I could try?

eduardo-marcolino commented 3 years ago

I didn't test it but i'll do something like add a hook to the acf/update_value filter. That's triggered right before storing to database.

Then you can check for the table name and if its enabled or not in the acfict_table_name and acfict_enabled keys from $field parameter.

After that you can update the table using wpdb replace method for example:

<?php

add_filter('acf/update_value', function( $value, $post_id, $field, $original ) 
{
  if ( $field[self::SETTINGS_ENABLED] && $field['name'] ) 
  {
    global $wpdb;

    $column_name = acfict_sanitize_keyword( $field['name'] );
    $column_value = apply_filters( 'acfict_sanitize_'.$field['type'], $value, $field );
    $wpdb->replace(  'wp_acf_'. $field[ACF_FICT::SETTINGS_TABLE_NAME], [ $column_name => $column_value]  );
  }

  return $value;

}, 10, 4);

Again, i didn't test it. I hope it can help you. Let me know if it works ;)

adulovic commented 3 years ago

Tried your suggestion but it didn't help me much as the previous programmer didn't comment any of his code so it was a struggle to understand his logic. As I was in a hurry, I just created new forms and tables directly and the client is happy with that. Thanks for the help anyway

victoriusnet commented 3 years ago

Hi,

WP 5.7.2 - fresh install PHP 7.4.1 MariaDB 10.4.10 ACF PRO 5.9.5

Custom Fields menu -> Add new:

image

Warning: _() expects exactly 1 parameter, 2 given in C:\Users\Viktor\Local Sites\hurghada\app\public\wp-content\plugins\acf-fields-in-custom-table\acf-fields-in-custom-table.php on line 108

1 | 0.0002 | 368064 | {main}( ) | ...\post.php:0 2 | 2.2539 | 4195264 | require( 'C:\Users\Viktor\Local Sites\hurghada\app\public\wp-admin\edit-form-advanced.php' ) | ...\post.php:206 3 | 2.3354 | 4367368 | do_meta_boxes( ) | ...\edit-form-advanced.php:688 4 | 2.3577 | 4458240 | ACF_FICT->render_metabox( ) | ...\template.php:1389 5 | 2.3650 | 4458368 |  ( ) | ...\acf-fields-in-custom-table.php:108