add meta box using function.php

hi does any one know how can i add other meta box and save the data in a different custom fields, using the code below.

<?php
function add_meta_boxes() {
    add_meta_box(
        'repeatable-fields',
        'Audio Playlist',
        'repeatable_meta_box_display',
        'post',
        'normal',
        'high');
}
add_action('admin_menu', 'add_meta_boxes');


function repeatable_meta_box_display() {
    global $custom_meta_fields, $post;
    wp_nonce_field( 'repeatable_meta_box_nonce', 'repeatable_meta_box_nonce' );
    //$repeatable_fields = get_post_meta($post->ID, $prefix, true);
?>
    <script type="text/javascript">
jQuery(document).ready(function($) {
    $('.metabox_submit').click(function(e) {
        e.preventDefault();
        $('#publish').click();
    });
    $('#add-row').on('click', function() {
        var row = $('.empty-row.screen-reader-text').clone(true);
        row.removeClass('empty-row screen-reader-text');
        row.insertBefore('#repeatable-fieldset-one tbody>tr:last');
        return false;
    });
    $('.remove-row').on('click', function() {
        $(this).parents('tr').remove();
        return false;
    });

    $('#repeatable-fieldset-one tbody').sortable({
        opacity: 0.6,
        revert: true,
        cursor: 'move',
        handle: '.sort'
    });
});
    </script>
<?php 
        $meta = get_post_meta($post->ID, 'custom_audio', true);
?>
    <table id="repeatable-fieldset-one" width="100%">
    <thead>
        <tr>
            <th width="2%"></th>
            <th width="30%">Name</th>
            <th width="60%">URL</th>
            <th width="2%"></th>
        </tr>
    </thead>
    <tbody>
    <?php

        if ( $meta ) {

            foreach ( $meta as $field ) {
?>
    <tr>
        <td><a class="button remove-row" href="#">-</a></td>
        <td><input type="text" class="widefat" name="name[]" value="<?php if($field['name'] != '') echo esc_attr( $field['name'] ); ?>" /></td>

        <td><input type="text" class="widefat" name="url[]" value="<?php if ($field['url'] != '') echo esc_attr( $field['url'] ); else echo 'http://'; ?>" /></td>
        <td><a class="sort">|||</a></td>

    </tr>
    <?php
            }
         } else {
            // show a blank one
?>
    <tr>
        <td><a class="button remove-row" href="#">-</a></td>
        <td><input type="text" class="widefat" name="name[]" /></td>


        <td><input type="text" class="widefat" name="url[]" value="http://" /></td>
<td><a class="sort">|||</a></td>

    </tr>
    <?php } ?>

    <!-- empty hidden one for jQuery -->
    <tr class="empty-row screen-reader-text">
        <td><a class="button remove-row" href="#">-</a></td>
        <td><input type="text" class="widefat" name="name[]" /></td>


        <td><input type="text" class="widefat" name="url[]" value="http://" /></td>
<td><a class="sort">|||</a></td>

    </tr>
    </tbody>
    </table>

    <p><a id="add-row" class="button" href="#">Add another</a>
    <input type="submit" class="metabox_submit" value="Save" />
    </p>
    <?php

}

add_action('save_post', 'repeatable_meta_box_save');
function repeatable_meta_box_save($post_id) {
    global $custom_meta_fields;
    if ( ! isset( $_POST['repeatable_meta_box_nonce'] ) ||
        ! wp_verify_nonce( $_POST['repeatable_meta_box_nonce'], 'repeatable_meta_box_nonce' ) )
        return;

    if (defined('DOING_AUTOSAVE') && DOING_AUTOSAVE)
        return;

    if (!current_user_can('edit_post', $post_id))
        return;

    // loop through fields and save the data
        $old = get_post_meta($post_id, 'custom_audio', true);
        $new = array();


        $names = $_POST['name'];
        $urls = $_POST['url'];

        $count = count( $names );

        for ( $i = 0; $i < $count; $i++ ) {
            if ( $names[$i] != '' ) :
                $new[$i]['name'] = stripslashes( strip_tags( $names[$i] ) );


            if ( $urls[$i] == 'http://' )
                $new[$i]['url'] = '';
            else
                $new[$i]['url'] = stripslashes( $urls[$i] ); // and however you want to sanitize
            endif;
        }

        if ( !empty( $new ) && $new != $old )
            update_post_meta( $post_id, 'custom_audio', $new );
        elseif ( empty($new) && $old )
            delete_post_meta( $post_id, 'custom_audio', $old );

} ?>

Related posts

Leave a Reply

1 comment

  1. function add_meta_boxes() {
         add_meta_box(
              'repeatable-fields',
              'Audio Playlist',
              'repeatable_meta_box_display',
              'post',
              'normal',
              'high');
    
         add_meta_box(
              'wpa-45985',
              'Wordpress Answers Metabox',
              'wpa_meta_box_display',
              'post',
              'normal',
              'high');
    
    } add_action('admin_menu', 'add_meta_boxes');
    

    just define a 2nd metabox, then define its display function and save function. it is exactly the same. EXCEPT, that if you want to save it as a different meta entry then you will change the name attribute for all the new inputs.

    for example: your first metabox has the name=”custom_audio[]”, so to save the 2nd box in a different meta entry you’d name the meta inputs differently

    <input type="text" name="wpa_45985['input1']" />
    <input type="text" name="wpa_45985['input2']" />
    

    and all your meta in this box will be saved in the meta key wpa_45985

    edit#1: adding display and save callbacks. you should add some data sanitation to the save function but this should be a good start

    function wpa_meta_box_display() {
        global $post;
        wp_nonce_field( 'wpa_45985', 'wpa_45985_nonce' );
    
    
    $meta = get_post_meta($post->ID, 'wpa_45985', true);
    
    $foo = isset($meta[foo]) ? $meta[foo] : '';
    $bar = isset($meta[bar]) ? $meta[bar] : '';
    ?>
    <input type="text" class="widefat" name="wpa_45985[foo]" value="<?php echo $foo; ?>"/>
    <input type="text" class="widefat" name="wpa_45985[bar]" value="<?php echo $bar; ?>"/>
    
    
    <?php
    
    }
    
    add_action('save_post', 'wpa_meta_box_save');
    function wpa_meta_box_save($post_id) {
        global $custom_meta_fields;
        if ( ! isset( $_POST['wpa_45985_nonce'] ) ||
            ! wp_verify_nonce( $_POST['wpa_45985_nonce'], 'wpa_45985' ) )
            return;
    
        if (defined('DOING_AUTOSAVE') && DOING_AUTOSAVE)
            return;
    
        if (!current_user_can('edit_post', $post_id))
            return;
    
        update_post_meta($post_id,'wpa_45985',$_POST['wpa_45985']);
    
    } ?>