Custom Fields
Re-Envisioned
Support MB Frontend Submission Custom Field and Post Edit Screen Field

  • Creator
    Topic
  • #32293
    Resolved Nicholas Cox
    Participant

    Hi

    Ihave created the following custom field, it uses 'Flatpickr' JS library. The form field works ok and inputs the date value and saves it to the database successfully.

    My issue is that on the post edit screen this field is blank when going to edit a post? so i assume that the value is not being loaded from the database? how do i get hold of the database value and populate the input value? I assume i need to know how metabox populates other fields on the post edit screen?

    
    public static function html( $meta, $field ) {
        return sprintf(
            $html = '<input id="' . $targetName .'-' . $fieldType . '" type="text" name="%s" value="%s">
                    <script>
                    document.addEventListener("DOMContentLoaded", function(event) {
                        const fp = flatpickr("#calendar", 
                        {
                            dateFormat: "Y-m-d H:i",
                            altInput: true, 
                            altFormat: "d-m-Y"
                        });
                    })
                    </script>
                    ',
            $field['field_name'],
            $field['id'],
            $meta
        );
    }
    

    Thanks

Viewing 2 replies - 1 through 2 (of 2 total)
  • Author
    Replies
  • #32318
    Long Nguyen
    Moderator

    Hi,

    I see there are two variable placeholders in your code %s

    <input id="' . $targetName .'-' . $fieldType . '" type="text" name="%s" value="%s">
    

    If you do not need to use the field ID, please remove this variable from your code.

        return sprintf(
            $html = '<input id="' . $targetName .'-' . $fieldType . '" type="text" name="%s" value="%s">
                   ...
                    ',
            $field['field_name'],
            $meta
        );
    #32322
    Nicholas Cox
    Participant

    that worked great thanks!

Viewing 2 replies - 1 through 2 (of 2 total)
  • You must be logged in to reply to this topic.