Save Dynamic Fields in Meta Box on Wordpress

1

I'm developing a theme for my site in Wordpress and I came across a huge question. I tried searching on several sites, even searched in English, but found nothing of the kind.

Basically is the following, I created a custom type of post to publish tutorials and in it I created a meta box customized, where I fill in with the URL of the video and the useful links of the class. I created an input where I put the link of the class and created a structure where I can add as many useful links for the class I want, by default comes a field, but I created a jquery, where you can add as many fields as you want. The problem is at the time of saving this, the video link I managed to save from good, but the useful links I do not know how. Here is the code

            add_action( 'add_meta_boxes', 'tutoriais_meta_box_add' );

            function tutoriais_meta_box_add() {
                add_meta_box( 'tutoriais-meta-box', 'Informacoes do Tutorial', 'tutoriais_meta_box_cb', 'tutoriais', 'normal', 'high' ); 
            }

            function tutoriais_meta_box_cb(){
                $values = get_post_custom( $post->ID );
                $video_url = isset( $values['video_url'] ) ? esc_attr( $values['video_url'][0] ) : '';
                wp_nonce_field( 'my_meta_box_nonce', 'meta_box_nonce' );
                ?>

                    <h2>URL do Video</h2>
                    <p>
                        <input type="text" name="video_url" id="video_url" value="<?php echo $video_url;?>" />
                    </p>

                    <h2>Links Uteis</h2>
                    <script type="text/javascript">
                        jQuery(function() {
                            var addDiv = jQuery('#addinput');
                            var i = jQuery('#addinput p').size() + 1;

                            jQuery('#addNew').live('click', function() {
                                jQuery('<p><label for="name'+i+'"> Nome </label><input type="text" id="name'+i+'" name="name" value="" placeholder="Link '+i+'" /><label for="link'+i+'"> Link </label><input type="text" id="link'+i+'" name="link'+i+'" value="" placeholder="Link '+i+'" /><label for="tipo'+i+'"> Tipo </label><select id="tipo'+i+'" name="tipo'+i+'"><option value="download">Download</option><option value="acesso">Acesso</option></select><a href="#" id="remNew">Remover Link</a></p>').appendTo(addDiv);
                                i++;
                                return false;
                            });

                            jQuery('#remNew').live('click', function() {
                                if( i > 2 ) {
                                    jQuery(this).parents('p').remove();
                                    i--;
                                }
                                return false;
                            });
                        });
                    </script>

            <div id="addinput">
            <p>
            <label for="name">Nome</label>
            <input type="text" id="name1" name="name1" value="" placeholder="Link 1" />
            <label for="link1">Link</label>
            <input type="text" id="link1" name="link1" value="" placeholder="Link 1" />
            <label for="tipo1">Tipo</label>
            <select id="tipo1" name="tipo1">
                <option value="download">Download</option>
                <option value="acesso">Acesso</option>
            </select>
            <a href="#" id="addNew">Adicionar Link</a>
            </p>
            </div>
            <input type="hidden" name="info_tutoriais">
                <?php
            }

            add_action( 'save_post', 'tutoriais_meta_box_save');

            function tutoriais_meta_box_save( $post_id ){
                if(defined( 'DOING_AUTOSAVE' ) && DOING_AUTOSAVE) return;

                if(!isset( $_POST['meta_box_nonce'] ) || !wp_verify_nonce( $_POST['meta_box_nonce'], 'my_meta_box_nonce' )) return;

                if(!current_user_can( 'edit_post' )) return;

                $allowed = array(
                'a' => array(
                'href' => array()
                )
                );

                if(isset($_POST['info_tutoriais'])){
                    update_post_meta( $post_id, 'video_url', wp_kses( $_POST['video_url'], $allowed ) );    
                }
            }
    
asked by anonymous 19.01.2017 / 01:34

1 answer

0

Instead of numbering the inputs of the links, you can use name="links []" So you will receive an array in POST, So you can save this array to a single meta post using implode () or even json_encode (). When you use the saved data, you can use the inverse functions json_decode () or explode ()

    
30.01.2017 / 12:06