Support

Account

Home Forums General Issues ACF to manage data in custom database tables

Solving

ACF to manage data in custom database tables

  • I’m working on a platform where users can create events for teachers and shool classes, teachers can enlist to those events and fill out feedback forms etc. For performance reasons I want to store these data in custom db tables. But it would be very comfortable to manage fields for the front end forms with ACF field groups.
    Unfortunately I cannot find hints on how to use ACF to create/edit data other than “standard WordPress data” such as posts, users etc. Is ACF actually designed for this? I know I could process the post data with hooks such as “cf/save_post”, but that seems to be an ugly workaround and data validation would be a mess. Is there a more straightforward way?

  • acf frontend form itself has validation before its submitted. use acf/save_post filter to fire an additional action to save the data in your custom Database.
    it would be stored in default custom fields also though..

    i would do it something like:

    setup database table ‘custom_events’ beforehand ofc

    function insert_event(array $args = []) {
        
        $defaults = [
            'event_name' => null,
            'time' => date('Y-m-d H:i:s'),
        ];
    
        $values = wp_parse_args($args, $defaults);
    
        if ( is_null($values['event_name']) ) {
            return new \WP_Error("Please provide an event name");
        }
    
        global $wpdb;
        $table_name = 'custom_events';
        $table_name = $wpdb->prefix . $table_name;
    
        $wpdb->insert($table_name, $values);
    
        return $wpdb->insert_id;
    }
    
    add_action('yourprefix/events/insert', 'insert_event' );
    
    function my_acf_save_post( $post_id ) {
    
        // Get newly saved values.
        $values = get_fields( $post_id );
    
        // Make sure $values acf fields and your custom database fields match their names
        do_action('yourprefix/events/insert', [ $values ]);
    
    }
    add_action('acf/save_post', 'my_acf_save_post');

    you would need an update action aswell and also error handling its just the direction i would to it.

    infact i do it same on some projects i maintain WP Post Data but additional data in custom tables

  • Thanks a lot laju. That’s about what I was thinking to enter or edit those data. But this approach enters a post and some postmeta on every form submit. I could delete the post after submission but this is quite nasty and iterates the post counter on every form submission. Is there a way to circumvent this?

  • hm i think ACF is in the middle between using WP posts and a bit of custom coding. If you want to do something complete custom i would not even use ACF for the form and validation of user input and build a little submission ajax/php form by yourself.

  • ACF is not designed to use custom DB tables.

    There is a 3rd party plugin available for this, the free version is here https://wordpress.org/plugins/acf-to-custom-database-tables/, if you are using ACF Pro you’ll likely need the Pro version of that plugin.

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

You must be logged in to reply to this topic.