WordPress can hold and display many different types of content. Internally, these are all stored in the same place, in the
wp_posts table. These are differentiated by a column called post_type
Custom Types
A Custom Type is a Post Type you define.
Adding a custom type to WordPress is done via the
register_post_type function. This function allows you to define the post type and how it operates within WordPress.
Here's a basic example of adding a custom post type name as Care Agency:
//Add New Custom Type
add_action('init','care_agency');
//Register custom post type
function care_agency()
{
$arg=array('labels'=>array(
'name'=>'Care Agencies', //Define Title of custom type
'singular_name'=>'Care Agency'),////Define Singular Title of custom type
'public'=>true, ////Define custom type access label
'capability_type'=>'page',
'menu_position'=>2,////Define Position of custom type
'support'=>array('editor',
'thumbnail',
'page-attributes'
)
);
register_post_type('care_agency',$arg);
}
How Add New Custom Meta box and custom fields
For Add new Meta box name as SEO Information
add_action( 'add_meta_boxes', 'add_seo_meta_box' );
/**
* Adds the SEO meta box to the page screen
*/
function add_seo_meta_box()
{
global $meta_box;
add_meta_box($meta_box['id'], $meta_box['title'], 'meta_show_box', $meta_box['page'], $meta_box['context'], $meta_box['priority']);
}
//Define SEO Meta box Fields
$prefix = 'seo_';
$meta_box = array(
'id' => 'my-meta-box',
'title' => 'SEO Information',
'page' => '',
'context' => 'normal',
'priority' => 'high',
'fields' => array(
array(
'name' => 'Meta Title',
'desc' => 'enter meta title',
'id' => $prefix . 'title',
'type' => 'text',
'std' => ''
),
array(
'name' => 'Meta Keyword',
'desc' => 'enter meta keywords',
'id' => $prefix . 'keywords',
'type' => 'text',
'std' => ''
),
array(
'name' => 'Meta Description',
'id' => $prefix . 'description',
'desc' => 'enter meta description',
'type' => 'textarea',
'std' => ''
)
)
);
//Display SEO Meta Box
function meta_show_box()
{
global $meta_box, $post;
// Use nonce for verification
echo '<input type="hidden" name="mytheme_meta_box_nonce" value="', wp_create_nonce(basename(__FILE__)), '" />';
echo '';
foreach ($meta_box['fields'] as $field) {
// get current post meta data
$meta = get_post_meta($post->ID, $field['id'], true);
echo '<p>',
'<label for="', $field['id'], '">', $field['name'], '</label>','';
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 />', $field['desc'];
break;
case 'textarea':
echo '<textarea name="', $field['id'], '" id="', $field['id'], '" cols="60" rows="4" style="width:97%">', $meta ? $meta : $field['std'], '</textarea>', '<br />', $field['desc'];
break;
'</p>';
}
}
}
//Save SEO Meta Box fields Value
add_action( 'save_post', 'save_seo_meta_box' );
function save_seo_meta_box($post_id) {
global $meta_box;
// verify nonce
if (!wp_verify_nonce($_POST['mytheme_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'] || 'care_agency' == $_POST['post_type'] || 'post' == $_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 ($meta_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);
}
}
}
How dispaly SEO Meta box details in header of the Site
//Add Seo Details in header
add_action('wp_head','add_metavalue_header',5);
function add_metavalue_header()
{
global $meta_box, $post;
echo "<meta name='title' value='".get_post_meta($post->ID,'seo_title',true)."'>";
echo "<meta name='keywords' value='".get_post_meta($post->ID,'seo_keywords',true)."'>";
echo "<meta name='description' value='".get_post_meta($post->ID,'seo_description',true)."'>";
}
This is why I keep coming back to this website. I can’t believe how much I missed since last time!