inc/post-meta.php
/**
     * Creates a new custom meta box in the New 'post_type' page.
     *
     * @param string $title
     * @param array $form_fields Associated array that contains the label of the input, and the desired input type. 'Title' => 'text'
     */
Creates a new custom meta box in the New 'post_type' page.
@param string $title
@param array $form_fields Associated array that contains the label of the input, and the desired input type.
Creates a new custom meta box in the New ' post_type ' page . @param string $title @param array $form_fields Associated array that contains the label of the input and the desired input type .
function add_meta_box($title, $form_fields = array())
    {
        $post_type_name = $this->post_type_name;
        add_action('post_edit_form_tag', function()
            {
                echo ' enctype="multipart/form-data"';
            });
        $this->admin_init(function() use($title, $form_fields, $post_type_name)
            {
                add_meta_box(
                    strtolower(str_replace(' ', '_', $title)), 
                    $title, 
                    function($post, $data)
                    { 
                        global $post;
                        wp_nonce_field(plugin_basename(__FILE__), 'jw_nonce');
                        $inputs = $data['args'][0];
                        $meta = get_post_custom($post->ID);
                        foreach ($inputs as $name => $type) {
                            $id_name = $data['id'] . '_' . strtolower(str_replace(' ', '_', $name));
                            if (is_array($inputs[$name])) {
                                if (strtolower($inputs[$name][0]) === 'select') {
                                    $select = "<select name='$id_name' class='widefat'>";
                                    foreach ($inputs[$name][1] as $option) {
                                        if (isset($meta[$id_name]) && $meta[$id_name][0] == $option) {
                                            $set_selected = "selected='selected'";
                                        } else $set_selected = '';
                                        $select .= "<option value='$option' $set_selected> $option </option>";
                                    }
                                    $select .= "</select>";
                                    array_push($_SESSION['taxonomy_data'], $id_name);
                                }
                            }
                            $value = isset($meta[$id_name][0]) ? $meta[$id_name][0] : '';
                            $checked = ($type == 'checkbox' && !empty($value) ? 'checked' : '');
                            array_push($_SESSION['taxonomy_data'], $id_name);
                            $lookup = array(
                                "text" => "<input type='text' name='$id_name' value='$value' class='widefat' />",
                                "textarea" => "<textarea name='$id_name' class='widefat' rows='10'>$value</textarea>",
                                "checkbox" => "<input type='checkbox' name='$id_name' value='$name' $checked />",
                                "select" => isset($select) ? $select : '',
                                "file" => "<input type='file' name='$id_name' id='$id_name' />"
                            );
                            ?>
                            <p>
                                <label><?php echo ucwords($name) . ':'; ?></label>
                                <?php echo $lookup[is_array($type) ? $type[0] : $type]; ?>
                            </p>
                            <p>
                                <?php
                                    $file = get_post_meta($post->ID, $id_name, true);
                                    if ( $type === 'file' ) {
                                        $file = get_post_meta($post->ID, $id_name, true);
                                        $file_type = wp_check_filetype($file);
                                        $image_types = array('jpeg', 'jpg', 'bmp', 'gif', 'png');
                                        if ( isset($file) ) {
                                            if ( in_array($file_type['ext'], $image_types) ) {
                                                echo "<img src='$file' alt='' style='max-width: 400px;' />";
                                            } else {
                                                echo "<a href='$file'>$file</a>";
                                            }
                                        }
                                    }
                                ?>
                            </p>
                            <?php
                        }
                    },
                    $post_type_name, 
                    'normal', 
                    'default', 
                    array($form_fields) 
                ); 
            });
    }
function add_meta_box ( $ title , $ form_fields = array ( ) ) { $ post_type_name = $ this -> post_type_name ; add_action ( 'post_edit_form_tag' , function ( ) { echo ' enctype="multipart/form-data"' ; } ) ; $ this -> admin_init ( function ( ) use ( $ title , $ form_fields , $ post_type_name ) { add_meta_box ( strtolower ( str_replace ( ' ' , '_' , $ title ) ) , $ title , function ( $ post , $ data ) { global $ post ; wp_nonce_field ( plugin_basename ( __FILE__ ) , 'jw_nonce' ) ; $ inputs = $ data [ 'args' ] [ 0 ] ; $ meta = get_post_custom ( $ post -> ID ) ; foreach ( $ inputs as $ name => $ type ) { $ id_name = $ data [ 'id' ] . '_' . strtolower ( str_replace ( ' ' , '_' , $ name ) ) ; if ( is_array ( $ inputs [ $ name ] ) ) { if ( strtolower ( $ inputs [ $ name ] [ 0 ] ) === 'select' ) { $ select = " <select name=' $ id_name ' class='widefat'> " ; foreach ( $ inputs [ $ name ] [ 1 ] as $ option ) { if ( isset ( $ meta [ $ id_name ] ) && $ meta [ $ id_name ] [ 0 ] == $ option ) { $ set_selected = " selected='selected' " ; } else $ set_selected = '' ; $ select .= " <option value=' $ option '  $ set_selected >  $ option  </option> " ; } $ select .= " </select> " ; array_push ( $ _SESSION [ 'taxonomy_data' ] , $ id_name ) ; } } $ value = isset ( $ meta [ $ id_name ] [ 0 ] ) ? $ meta [ $ id_name ] [ 0 ] : '' ; $ checked = ( $ type == 'checkbox' && ! empty ( $ value ) ? 'checked' : '' ) ; array_push ( $ _SESSION [ 'taxonomy_data' ] , $ id_name ) ; $ lookup = array ( " text " => " <input type='text' name=' $ id_name ' value=' $ value ' class='widefat' /> " , " textarea " => " <textarea name=' $ id_name ' class='widefat' rows='10'> $ value </textarea> " , " checkbox " => " <input type='checkbox' name=' $ id_name ' value=' $ name '  $ checked  /> " , " select " => isset ( $ select ) ? $ select : '' , " file " => " <input type='file' name=' $ id_name ' id=' $ id_name ' /> " ) ; ?> <p>
                                <label> <?php echo ucwords ( $ name ) . ':' ; ?> </label>
                                 <?php echo $ lookup [ is_array ( $ type ) ? $ type [ 0 ] : $ type ] ; ?> </p>
                           
                            <p>

                                 <?php $ file = get_post_meta ( $ post -> ID , $ id_name , true ) ; if ( $ type === 'file' ) { $ file = get_post_meta ( $ post -> ID , $ id_name , true ) ; $ file_type = wp_check_filetype ( $ file ) ; $ image_types = array ( 'jpeg' , 'jpg' , 'bmp' , 'gif' , 'png' ) ; if ( isset ( $ file ) ) { if ( in_array ( $ file_type [ 'ext' ] , $ image_types ) ) { echo " <img src=' $ file ' alt='' style='max-width: 400px;' /> " ; } else { echo " <a href=' $ file '> $ file </a> " ; } } } ?> </p>

                             <?php } } , $ post_type_name , 'normal' , 'default' , array ( $ form_fields ) ) ; } ) ; }
Creates a new custom meta box in the New 'post_type' page.
Creates a new custom meta box in the New ' post_type ' page .
// end update_edit_form // At WordPress' admin_init action, add any applicable metaboxes. // id // title // function that displays the form fields // List of all the specified form fields // Get the saved field values // For each form field specified, we need to create the necessary markup // $name = Label, $type = the type of input to create #'Happiness Info' in 'Snippet Info' box becomes # snippet_info_happiness_level // then it must be a select or file upload // $inputs[$name][0] = type of input // filter through them, and create options // if what's stored in the db is equal to the // current value in the foreach, that should // be the selected one // Attempt to set the value of the input, based on what's saved in the db. // Sorta sloppy. I need a way to access all these form fields later on. // I had trouble finding an easy way to pass these values around, so I'm // storing it in a session. Fix eventually. // TODO - Add the other input types. // If a file was uploaded, display it below the input. // display the image // associated post type // location/context. normal, side, etc. // priority level // optional passed arguments. // end add_meta_box