0

I have a custom meta box that let's users add new fields.
If the fields are saved the values are stored in an array.

If a user wants to delete a certain value he empties the field and saves the page again.
Now the problem is that the empty field still shows up.

Can I delete the empty value from the array if the field is empty?
Ofcourse it can. I read (via Google) on php.net that it's possible with array_filter()

But I'm not succesfull in implementing this in my current code. So I hope somebody can show me how or maybe tell me a better way :-)

<script type="text/javascript">
    jQuery(document).ready(function($){
        var max_fields      = 50; //maximum input boxes allowed
        var wrapper         = $(".input_fields_brand"); //Fields wrapper
        var add_button      = $(".add_field_button_brand"); //Add button ID
        var x = 1; //initlal text box count
            $(add_button).click(function(e){ //on add input button click
                e.preventDefault();
                if(x < max_fields){ //max input box allowed
                    x++; //text box increment
                    $(wrapper).append('<div><input type="text" name="brand[]" id="brand[]"/><a href="#" class="remove_field_brand">Verwijderen</a></div>'); //add input box
                }
            });

            $(wrapper).on("click",".remove_field_brand", function(e){ //user click on remove text
                e.preventDefault(); $(this).parent('div').remove(); x--;
            })
        });
    </script>
    <?php
    $postid = get_the_ID();                         
    $brands = get_post_meta( $postid, 'brand' );
    echo '<div class="input_fields_brand">';
    echo '<button class="add_field_button_brand">Toevoegen</button>';
    for($x = 0; $x <= count($brands, COUNT_RECURSIVE) - count($brands); $x++) 
        { 
            echo '<div><input type="text" name="brand[]" id="' .$brands[0][$x]. '" value="' .$brands[0][$x]. '"></div>';
        }
    echo '</div>';
?>

I hope I made myself clear and provided enough information!

  • 1
    Check for the value var $brands[0][$x], like if ( ! empty( $brands[0][$x] ) ) { echo ... } that you only lists input fields with non empty value. – bueltge Mar 20 '15 at 12:40
  • That does work but is a very poor solution because the database get's filled with empty fields. – Interactive Mar 20 '15 at 13:12
  • 1
    Right, do the same on update_post_meta() and/or add_post_meta(). If is empty, then don't save in the database and if empty, then delete this post meta - if ( empty( $brands[0][$x] ) ) { delete_post_meta() } – bueltge Mar 20 '15 at 13:24
  • I guess I was to quick with my happiness. I think I'm doing something wrong with the saving/deleting. could you please have a look? if ( ! empty( $brands[0][$x] ) ) { update_post_meta( $post_id, 'brand', array_map( 'sanitize_text_field', $_POST[ 'brand' ] ) ); } if ( empty( $brands[0][$x] ) ) { delete_post_meta( $post_id, 'brand', array_map( 'sanitize_text_field', $_POST[ 'brand' ] ) ); } – Interactive Mar 20 '15 at 14:17
  • Why do you usearray_map(). The functions for post meta, like delete_post_meta(), works with serialized data. – bueltge Mar 23 '15 at 10:55

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Browse other questions tagged or ask your own question.