acf number auto increment code example

Example: acf auto increment field

<?php
function wfp_documents_setup_id_incr()
{

    // Check if user has rights to set it up and ACF is enabled.
    if (current_user_can('administrator') && function_exists('get_field')):

        // Initial value
        // === YOU NEED TO UPDATE HERE ===
        // Replace <code>custom_invoice_id</code> with your desired id name.
        add_option('custom_invoice_id', '0001', '', 'yes');

        /**
         * Wrapper to get the id (if i would need to add something to it)
         * @return mixed|void – stored next available id
         */
        function wfp_get_current_invoice_id()
        {
            return get_option('custom_invoice_id');
        }

        /**
         * Count up the id by one and update the globally stored id
         */
        function wfp_increase_invoice_id()
        {
            $curr_invoice_id    = get_option('custom_invoice_id');
            $next_invoice_id    = intval($curr_invoice_id) + 1;
            update_option('custom_invoice_id', $next_invoice_id);
        }

        /**
         * Populate the acf field when loading it.
         */
        function wfp_auto_get_current_document_id($value, $post_id, $field)
        {
            // If the custom field already has a value in it, just return this one (we don't want to overwrite it every single time)
            if ($value !== null && $value !== "") {
                return $value;
            }

            // If the field is empty, get the currently stored next available id and fill it in the field.
            $value = wfp_get_current_invoice_id();
            return $value;
        }

        // Use ACF hooks to populate the field on load
        // ==== YOU NEED TO UPDATE HERE ====
        // Replace <code>invoice_id</code> with the name of your field.
        add_filter('acf/load_value/name=invoice_id', 'wfp_auto_get_current_document_id', 10, 3);

        /**
         * Registers function to check if the globally stored id needs to be updated after a post is saved.
         */
        function wfp_acf_save_post($post_id)
        {
            // Check if the post had any ACF to begin with.
            if (!isset($_POST['acf'])) {
                return;
            }

            $fields = $_POST['acf'];

            // Only move to the next id when any ACF fields were added to that post, otherwise this might be an accident and would skip an id.
            if ($_POST['_acf_changed'] == 0) {
                return;
            }

            // Next we need to find the field out id is stored in
            foreach ($fields as $field_key => $value) {
                $field_object = get_field_object($field_key);

                /**
                 * If we found our field and the value of that field is the same as our currently "next available id" –
                 * we need to increase this id, so the next post doesn't use the same id.
                 */
                if ($field_object['name'] == "invoice_id"
                    && wfp_get_current_invoice_id() == $value) {
                    wfp_increase_invoice_id();

                    return;
                }
            }
        }

        // Use a hook to run this function every time a post is saved.
        add_action('acf/save_post', 'wfp_acf_save_post', 20);

        /**
         * The code below just displays the currently stored next id on an acf-options-page,
         * so it's easy to see which id you're on. The field is disabled to prevent easy tinkering with the id.
         */
        function wfp_load_current_document_ids_settingspage($value, $postid, $field)
        {
            if ($field['name'] == "document_ids-group_current_invoice_id") {
                return wfp_get_current_invoice_id();
            }
            return $value;
        }

        function wfp_disable_acf_field($field)
        {
            $field['disabled'] = 1;

            return $field;
        }

        add_filter('acf/load_field/name=current_invoice_id', 'wfp_disable_acf_field', 10, 3);
        add_filter('acf/load_value/name=current_invoice_id', 'wfp_load_current_document_ids_settingspage', 10, 3);

    endif;
}

add_action('init', 'wfp_documents_setup_id_incr');
?>

Tags:

Php Example