Wordpress custom fields metabox creation without plugins

Sometimes we need to add a lot of specific custom fields to the post (may be even depending on post category). Standard wordpress custom field metabox doesn't look very friendly. Of course you can use some plugin, like Custom Field Template, but you should consider that any plugin is an extension which is maintained by one or couple authors. You will always depend on it. More over plugins add database tables, there are a lot of security reasons for doing everything in wordpress way, using just core functionality.

Percentage of wordpress core functionality usage like our brain percentage is very and very low. So let's gradually enhance it.

We can add given below peace of code into functions.php file which is situated in current theme folder.

<?php 
function add_custom_init() {
	add_meta_box("custom-meta", "Title", "address_fields_init", "post", "normal", "low");
}

function address_fields_init(){
  global $post;
  $custom = get_post_custom($post->ID);
  $custom1 = get_post_meta($post->ID);
  $cat_id = get_the_category($post->ID);
  $cat_id = $cat_id[0]->cat_ID;
  // here we generate any html we need depending on category id
  if($cat_id == 8) {
	  echo "
	  <table>
		<tbody>
			<tr>
				<th style='text-align:right;' scope='row'><label for='streetname'>First title</label></th>
				<td><input type='text' size='40' name='First_title' value='".$custom['First_title']['0']."'></td>
			</tr>
			<tr>
				<th style='text-align:right;' scope='row'><label for='streetname'>Second_title</label></th>
				<td><input type='text' size='40' name='Second_title' value='".$custom['Second_title']['0']."'></td>
			</tr>
		</tbody>
	  </table>";
	} elseif ($cat_id == 7 OR $cat_id == 12) {
		echo "
		<table>
			<tbody>
				<tr>
					<th style='text-align:right;' scope='row'><label for='streetname'>Another first title</label></th>
					<td><input type='text' size='40' name='Another_first_title' value='".$custom['Another_first_title']['0']."'></td>
				</tr>
				<tr>
					<th style='text-align:right;' scope='row'><label for='streetname'>Another second title</label></th>
					<td><input type='text' size='40' name='Another_second_title' value='".$custom['Another_second_title']['0']."'></td>
				</tr>
			</tbody>
		</table>"; 
	} else {
	echo "
	  <table>
		<tbody>
			<tr>
				<th style='text-align:right;' scope='row'><label for='streetname'>First title</label></th>
				<td><input type='text' size='40' name='First_title' value='".$custom['First_title']['0']."'></td>
			</tr>
			<tr>
				<th style='text-align:right;' scope='row'><label for='streetname'>Second_title</label></th>
				<td><input type='text' size='40' name='Second_title' value='".$custom['Second_title']['0']."'></td>
			</tr>
			<tr>
				<th style='text-align:right;' scope='row'><label for='streetname'>Another first title</label></th>
				<td><input type='text' size='40' name='Another_first_title' value='".$custom['Another_first_title']['0']."'></td>
			</tr>
			<tr>
				<th style='text-align:right;' scope='row'><label for='streetname'>Another second title</label></th>
				<td><input type='text' size='40' name='Another_second_title' value='".$custom['Another_second_title']['0']."'></td>
			</tr>
		</tbody>
	  </table>";
	
	} 
}
add_action('save_post', 'save_address_fields');
function save_address_fields(){
  if (!defined('DOING_AUTOSAVE') && !DOING_AUTOSAVE)
    return $postID;
  if ( !current_user_can( 'edit_page', $post_id ) )
    return;
	global $post;
	update_post_meta($post->ID, "First_title", $_POST['First_title']);
	update_post_meta($post->ID, "Second_title", $_POST['Second_title']);
	update_post_meta($post->ID, "Another_first_title", $_POST['Another_first_title']);
	update_post_meta($post->ID, "Another_second_title", $_POST['Another_second_title']);
}  
Rating: 
6 out of 10 based on 1 ratings.