WordPress.org

Ready to get started?Download WordPress

Forums

Make a table for custom meta box (1 post)

  1. MrMACHOMAN84
    Member
    Posted 1 year ago #

    Hello, I've made a meta box for my posts, now I would like to know how I can make a simple table out of the information.

    Here's the metabox code:

    $prefix = 'anime_';
    
    $anime_box = array(
        'id' => 'anime-meta-box',
        'title' => 'Anime Details',
        'page' => 'post',
        'context' => 'normal',
        'priority' => 'high',
        'fields' => array(
    		array(
                'name' => 'Name',
                'desc' => 'Add the name of the Anime in either English or Japanese(Romanji).',
                'id' => $prefix . 'anime_name',
                'type' => 'text',
                'std' => ''
            ),
            array(
                'name' => 'Genre',
                'desc' => 'Is it a thriller, action/adventure, etc...',
                'id' => $prefix . 'anime_genre',
                'type' => 'text',
                'std' => ''
            ),
    		array(
                'name' => 'Directed by',
                'desc' => 'Name of director(s).',
                'id' => $prefix . 'anime_director',
                'type' => 'text',
                'std' => ''
            ),
            array(
                'name' => 'Music by',
                'desc' => 'Name of composer(s)',
    			'id' => $prefix . 'anime_music',
                'type' => 'text',
                'std' => ''
            ),
    		array(
                'name' => 'Studio',
                'desc' => 'Studio which owns the anime.',
                'id' => $prefix . 'anime_studio',
                'type' => 'text',
                'std' => ''
            ),
             array(
                'name' => 'Licensed by',
                'desc' => 'Name of both American and Japanese license holders.',
    			'id' => $prefix . 'anime_license',
                'type' => 'text',
                'std' => ''
    		),
    		array(
                'name' => 'Network(s)',
                'desc' => 'Networks which air the show in both Japan and the United States.',
                'id' => $prefix . 'anime_network',
                'type' => 'text',
                'std' => ''
            ),
             array(
                'name' => 'Original run',
                'desc' => 'Date of when the anime first aired and when it stopped.',
    			'id' => $prefix . 'anime_run',
                'type' => 'text',
                'std' => ''
    		),
    		array(
                'name' => 'Episodes',
                'desc' => 'Number of episodes.',
                'id' => $prefix . 'anime_episodes',
                'type' => 'text',
                'std' => ''
            ),
    
        )
    );
    
    add_action('admin_menu', 'anime_add_box');
    
    // Add meta box
    function anime_add_box() {
    	global $anime_box;
    
    	add_meta_box($anime_box['id'], $anime_box['title'], 'anime_show_box', $anime_box['page'], $anime_box['context'], $anime_box['priority']);
    }
    
    // Callback function to show fields in meta box
    function anime_show_box() {
    	global $anime_box, $post;
    
    	// Use nonce for verification
    	echo '<input type="hidden" name="anime_meta_box_nonce" value="', wp_create_nonce(basename(__FILE__)), '" />';
    
    	echo '<table class="form-table">';
    
    	foreach ($anime_box['fields'] as $field) {
    		// get current post meta data
    		$meta = get_post_meta($post->ID, $field['id'], true);
    
    		echo '<tr>',
    				'<th style="width:20%"><label for="', $field['id'], '"><strong>', $field['name'], ':</strong></label></th>',
    				'<td>';
    		switch ($field['type']) {
    			case 'text':
    				echo '<input type="text" name="', $field['id'], '" id="', $field['id'], '" value="', $meta ? $meta : $field['std'], '" size="30" style="width:97%" />',
    					'<br /><small>', $field['desc'],'</small>';
    				break;
    
    		}
    		echo 	'<td>',
    			'</tr>';
    	}
    
    	echo '</table>';
    }
    
    add_action('save_post', 'anime_save_data');
    
    // Save data from meta box
    function anime_save_data($post_id) {
    	global $anime_box;
    
    	// verify nonce
    	if (!wp_verify_nonce($_POST['anime_meta_box_nonce'], basename(__FILE__))) {
    		return $post_id;
    	}
    
    	// check autosave
    	if (defined('DOING_AUTOSAVE') && DOING_AUTOSAVE) {
    		return $post_id;
    	}
    
    	// check permissions
    	if ('page' == $_POST['post_type']) {
    		if (!current_user_can('edit_page', $post_id)) {
    			return $post_id;
    		}
    	} elseif (!current_user_can('edit_post', $post_id)) {
    		return $post_id;
    	}
    
    	foreach ($anime_box['fields'] as $field) {
    		$old = get_post_meta($post_id, $field['id'], true);
    		$new = $_POST[$field['id']];
    
    		if ($new && $new != $old) {
    			update_post_meta($post_id, $field['id'], $new);
    		} elseif ('' == $new && $old) {
    			delete_post_meta($post_id, $field['id'], $old);
    		}
    	}
    }

Topic Closed

This topic has been closed to new replies.

About this Topic