Skip to content
Read For Learn
Read For Learn
  • Database
    • Oracle
    • SQL
  • C
  • C++
  • Java
  • Java Script
  • jQuery
  • PHP
Read For Learn
  • Database
    • Oracle
    • SQL
  • C
  • C++
  • Java
  • Java Script
  • jQuery
  • PHP

Frontend Form Checkbox Update ACF Field

To be compatible with ACF you should first find out how ACF is saving checkboxes values to database. As I now this is serialized array. When we know that we must save our frontend checkboxes in the same way. The good news is you can pass only array to update_post_meta and WordPress will take care of serialization.

The problem with your code is that you probably not passing array of checkbox values. You should investigate this part: esc_attr(strip_tags($_POST['customMetaAppliances[]'])).

Two things which bother me here:

  • esc_attr and strip_tags functions are expecting string as argument not an array they may return unexpected results
  • if you want to get array of checkboxes from post request you should use field name without square brackets customMetaAppliances

I think you can replace saving part with code:

$customMetaAppliances = filter_input( INPUT_POST, 'customMetaAppliances', FILTER_SANITIZE_STRING, FILTER_REQUIRE_ARRAY );
update_field( 'customMetaAppliances', $customMetaAppliances, $post_id );

Personaly when I have installed ACF I use update_field instead of update_post_meta.

I created fully working class which will display form on the frontend after post content and save values to post ACF fields on submit.

class WPSE_287946_Form {

    /**
     * Class constructor
     */
    public function __construct() {

        $this->define_hooks();
    }

    public function save_form() {

        if( isset( $_POST['save'] ) ) { // Submit button

            $post_id     = filter_input( INPUT_POST, 'id', FILTER_SANITIZE_NUMBER_INT );
            $name        = filter_input( INPUT_POST, 'name', FILTER_SANITIZE_STRING );
            $color       = filter_input( INPUT_POST, 'color', FILTER_SANITIZE_STRING );
            $accessories = filter_input( INPUT_POST, 'accessories', FILTER_SANITIZE_STRING, FILTER_REQUIRE_ARRAY );

            update_field( 'name', $name, $post_id );
            update_field( 'color', $color, $post_id );
            update_field( 'accessories', $accessories, $post_id );

            // Redirect user because POST request on single post page will trigger 404 page.
            wp_redirect( get_permalink( $post_id ) );
        }
    }

    /**
     * Display form
     */
    public function display_form( $content ) {

        $name        = get_field( 'name' );
        $color       = get_field( 'color' );
        $accessories = get_field( 'accessories' );

        ob_start();

        ?>

        <form method="post">
            <p>
                <?php $this->display_text( 'name', 'Name', $name ); ?>
            </p>
            <p>
                <?php $this->display_select( 'color', 'Color', $this->get_available_colors(), $color ); ?>
            </p>
            <p>
                <?php $this->display_checkboxes( 'accessories', 'Accessories', $this->get_available_accessories(), $accessories ); ?>
            </p>
            <p>
                <input type="hidden" name="id" value="<?php esc_attr_e( get_the_ID() ) ?>">
                <input type="submit" name="save" value="Submit" />
            </p>
        </form>

        <?php

        $output = ob_get_contents();
        ob_end_clean();

        return $content . $output;
    }

    /**
     * Display text field
     */
    private function display_text( $name, $label, $value="" ) {
        ?>
        <label><?php esc_html_e( $label, 'wpse_287946' ) ?></label>
        <input type="text" name="<?php esc_attr_e( $name ) ?>" value="<?php esc_attr_e( $value ); ?>">
        <?php
    }

    /**
     * Display select field
     */
    private function display_select( $name, $label, $options, $value="" ) {
        ?>
        <label><?php esc_html_e( $label, 'wpse_287946' ) ?></label>
        <select name="<?php esc_attr_e( $name ) ?>">
            <?php $this->display_options( $options, $value ); ?>
        </select>
        <?php
    }

    /**
     * Display options
     */
    private function display_options( $options, $value ) {

        foreach( $options as $option_value => $option_label ):
            $selected = ( $option_value === $value ) ? ' selected' : '';
            ?>
            <option value="<?php esc_attr_e( $option_value ) ?>"<?php esc_attr_e( $selected ) ?>><?php esc_html_e( $option_label, 'wpse_287946' ) ?></option>
            <?php

        endforeach;
    }

    /**
     * Display checkboxes field
     */
    private function display_checkboxes( $name, $label, $options, $values = array() ) {

        $name .= '[]';

        ?>
        <label><?php esc_html_e( $label, 'wpse_287946' ) ?></label>
        <?php

        foreach ( $options as $option_value => $option_label ):
            $this->display_checkbox( $name, $option_label, $option_value, $values );
        endforeach;
    }

    /**
     * Display single checkbox field
     */
    private function display_checkbox( $name, $label, $available_value, $values = array() ) {
        $checked = ( in_array($available_value, $values) ) ? ' checked' : '';
        ?>
        <label>
            <input type="checkbox" name="<?php esc_attr_e( $name ) ?>" value="<?php esc_attr_e( $available_value ) ?>"<?php esc_attr_e( $checked ) ?>>
            <?php esc_html_e( $label, 'wpse_287946' ) ?>
        </label>
        <?php
    }

    /**
     * Get available colors
     */
    private function get_available_colors() {

        return array(
            'red' => 'Red',
            'blue' => 'Blue',
            'green' => 'Green',
        );
    }

    /**
     * Get available accessories
     */
    private function get_available_accessories() {

        return array(
            'case' => 'Case',
            'tempered_glass' => 'Tempered glass',
            'headphones' => 'Headphones',
        );
    }

    /**
     * Define hooks related to plugin
     */
    private function define_hooks() {

        /**
         * Add action to save form
         */
        add_action( 'wp', array( $this, 'save_form' ) );

        /**
         * Add filter to display form
         */
        add_filter( 'the_content', array( $this, 'display_form' ) );
    }
}

new WPSE_287946_Form();

Related Posts:

  1. update_post_meta and update_field ony working when saving the post
  2. ACF (Advanced Custom Fields) not updating post or postmeta values
  3. Front End User Registration – How to Save ACF Values?
  4. advanced custom fields plugin last input field in new posts
  5. acf_form() – “On Update” action [closed]
  6. Filtering multiple custom fields with WP REST API 2
  7. How to import CSV into Custom Post Type custom fields?
  8. ACF – get fields from group
  9. Front end form to create a custom post with preview
  10. How to get “Additional CSS Class” for ACF Gutenberg block
  11. Advanced Custom Fields select field : How to echo the label, not the value? [closed]
  12. advanced custom fields update_field for field type: Taxonomy
  13. ACF: get_field() returning false [closed]
  14. Generate a excerpt from an ACF-wysiwyg-field
  15. Adding custom fields to the header.php
  16. How to get_terms() only of terms matching an ACF meta value?
  17. Pods cms and “advanced custom fields” plugin
  18. Adding a body class with ACF
  19. Returning all radio button options when using Advanced Custom Fields
  20. WP REST API: Order posts by meta value (acf)?
  21. wordpress REST-API upload image to ACF
  22. Get Advanced Custom Fields values before saving [closed]
  23. Query between dates using Date Picker fields
  24. Order and group posts by acf by month and year
  25. Get the label from ACF checkbox [closed]
  26. Up/Down voting system for WordPress
  27. Advanced custom field – gallery – display one random image
  28. Reason action hook won’t work with update_post_meta from frontend form? Alternative?
  29. How to update a custom post title from a front-end form using ACF fields?
  30. Showing an ACF field in admin posts dashboard
  31. Google Places API With ACF [closed]
  32. Check for the existence of custom field
  33. Vimeo thumbnails [closed]
  34. query posts in functions.php and update a field
  35. pre_get_posts filter using numeric meta_query comparison (from dates)
  36. ACF + contact form 7
  37. Is there a way to export Advanced Custom Fields data?
  38. Copying Custom Meta Values from existing post to a duplicate post
  39. How to add a custom field to quick edit
  40. Using Advanced Custom Field (ACF) to insert meta description on each page
  41. ACF Repeater Field, need IF statement for if sub field has content
  42. Can not create fields in ACF with code
  43. Gutenberg First Block on Page Conditional?
  44. Using meta_query and custom fields within pre_get_posts to return posts within a numerical range
  45. Comparing arrays with meta_query in pre_get_posts
  46. Set default date in datepicker of an advanced custom field
  47. Advanced Custom Fields plugin : displaying a YouTube video
  48. Advanced Custom Fields – Check if multiple get_fields exist?
  49. ACF – Custom fields have dissappeared [closed]
  50. Error in Validate Field with ACF plugin in WordPress
  51. How do I add advanced custom fields / meta fields to Elasticsearch? [closed]
  52. ACF Upload Image in repeater from front-end with custom form? – add_post_meta()
  53. How to order by just the Time Field in Advanced Custom Fields
  54. Using Gutenberg parse_blocks Function With ACF Custom Blocks?
  55. Query pages for use of a flexible content layout
  56. WordPress Gutenberg, update post content programmatically
  57. ACF get field label in custom code
  58. Unable to show ACF’s Image Custom Field properly in Genesis Framework [closed]
  59. ACF repeater field with meta_query
  60. Query Posts depends on custom field inside repeater field using acf
  61. How to add custom fields in rss feed
  62. sort by date in Advanced Custom Field
  63. Get specific repeater row (via advanced custom fields) based on Meta Query with Wildcards [closed]
  64. How to customize a divs background dynamically using Advanced Custom Fields Plugin?
  65. disable (read only) a field if within a custom post type name
  66. How to update custom taxonomy meta using ACF update_field() function or any other wordpress function
  67. Replace post’s “the_content” with ACF value
  68. Minimising Database Queries when using Advanced Custom Fields
  69. ACF if / else checkbox [closed]
  70. PHP echo stripping formatting in Advanced Custom Fields [closed]
  71. Thumbnails generated from PDF in the “Media” section – how to show them in theme template?
  72. Post Object field orderby (Advanced Custom Field)
  73. How to count ACF Flexible Content Rows?
  74. ACF Plugin – Random Gallery Image with wp_get_attachment_image()
  75. How to change the page title from functions.php
  76. Sorting custom admin column by value
  77. Acf Pro repeater field returns null [closed]
  78. Query between dates using date picker filter breaks in WordPress 4.2.1
  79. How to upload multiple images on frontend to ACF gallery using update_field
  80. ACF: Only get first row of a Repeater Field
  81. Add user custom fields and make editable on frontend
  82. Insert Commas into ACF number field
  83. How I can check if get_theme_mod is in header or a template part
  84. Get ACF value after update user using save_post
  85. Using pre_get_posts for meta value of LIKE comparison on ACF repeater sub field
  86. Inconsistent behavior from number_format
  87. WP Rest endpoint with custom post type and ACF Fields
  88. random p tag in advanced custom fields?
  89. Advanced custom fields: Customise date picker’s start date (need to choose year 1500 onwards) [closed]
  90. How can I track and output when a field is updated? (currently using Advanced Custom Fields)
  91. Display post in order of ACF checkbox?
  92. ACF: How to get the full field name (meta_key) by a field key?
  93. ACF Custom validation message not showing up
  94. How to write PHP array to render JSON-LD Markup for Job Postings, with ACF
  95. ACF Advanced Custom Fields | Help me grab the fields on my WooCommerce homepage [closed]
  96. Is it safe and good practice to use do_shortcode to escape?
  97. Post edit – Media Library – Only get images from current post
  98. Output comma with get_field in Advanced Custom Fields [closed]
  99. Gutenberg on an ACF options page
  100. Update post meta value as date difference between two fields
Categories advanced-custom-fields Tags advanced-custom-fields, front-end, post-meta
Same database; different WordPress
Creating custom admin panel pages without making a plugin?

Recommended Hostings

Cloudways: Realize Your Website's Potential With Flexible & Affordable Hosting. 24/7/365 Support, Managed Security, Automated Backups, and 24/7 Real-time Monitoring.

FastComet: Fast SSD Hosting, Free Migration, Hack-Free Security, 24/7 Super Fast Support, 45 Day Money Back Guarantee.

Recent Added Topics

  • Bug in translation system: load_theme_textdomain() returns true, files are available and accessible but the language defaults to english
  • Custom Elementor controls not appearing in the widget Advanced tab using injection hooks
  • Get the name of the template/*html file used
  • Trying to Add Paging to Single Post Page
  • Sharing media files between live and staging servers
  • How to display the description of a custom post type in the dashboard?
  • Critical error on image display
  • Copying WP data and files into new install?
  • How to determine the DirectAdmin WordPress backup date?
  • How to get list of ALL tables in the database?
© 2026 Read For Learn
  • Database
    • Oracle
    • SQL
  • algorithm
  • asp.net
  • assembly
  • binary
  • c#
  • Git
  • hex
  • HTML
  • iOS
  • language angnostic
  • math
  • matlab
  • Tips & Trick
  • Tools
  • windows
  • C
  • C++
  • Java
  • javascript
  • Python
  • R
  • Java Script
  • jQuery
  • PHP
  • WordPress