Menu
06/02 2015

WordPress Custom Post based on a template

Having been busying myself on the redesign of this site over the last week or so I invaded the custom post functionality of WordPress to try and make the process a little more organised and straightforward.

There'll always be an element of variable copy and paste tedium but found that it can at least be structured in a way that can have you CMSing with confidence. Firstly, folder structure. A good way of seperating all the things wordpress can do is to keep similar functions together so I used the traditional [cci lang="php"]functions.php[/cci] file as a kind of Bootstrap to import and include and kick things off. As far as the custom posts go I created an array and added it into my [cci lang="php"]$webegg[/cci] variable (a variable which holds lots of other information too).

  
$webegg['include_post_types'] = array('page', 'youtube');

Using this array, I then loop through each value to automatically include the correct code for that partucular post type. The 'page' post type allows us to include as many customised post types as we want & this is what we will focus on in this article.

  
if(isset($webegg['include_post_types']) && is_array($webegg['include_post_types']) && count($webegg['include_post_types'])) {
  foreach($webegg['include_post_types'] as $cm_cptype) {
    require get_template_directory().'/includes-functions/post-types/'.$cm_cptype.'/'.$cm_cptype.'.php';
  }
}

Pretty self explanatory really (hopefully). Firstly it checks if the post types are there and then loops through each to include the custom post type code in the functions file. I'll ask you to ignore the youtube one for this artcle as we will focus on what is going on inside the pages.

  
add_action('add_meta_boxes', 'cm_pt_page_register_meta_boxes');
function cm_pt_page_register_meta_boxes() {

  // Get globals
  global $wpdb;

  // Get page template
  $post_id = $_GET['post'] ? $_GET['post'] : $_POST['post_ID'];
  $template_file = get_post_meta($post_id, '_wp_page_template', true);

  // Get the post
  $post = get_post($post_id);
}

// move the blurb meta box above the main aditor using a custom context that doesn't conflict with other plugins/post types
// 'edit_form_after_title' is a wordpress hook
add_action('edit_form_after_title', function() {
  global $post, $wp_meta_boxes;
  do_meta_boxes(get_current_screen(), 'beforemaineditor', $post);
  unset($wp_meta_boxes[get_post_type($post)]['beforemaineditor']);
});

// Pages: all
add_meta_box('pt-page-headblurb', 'Blurb', 'cm_pt_page_meta_box_headblurb', 'page', 'beforemaineditor', 'default');
add_meta_box('pt-page-asidepanel', 'Hidden Side Panel', 'cm_pt_page_meta_box_asidepanel', 'page', 'normal', 'default');

// Pages: all but homepage
if($template_file!='page-homepage.php') {
  add_meta_box('pt-page-headerbg', 'Page Header Background', 'cm_pt_page_meta_box_headerbg', 'page', 'side', 'default');
}

// Override menu order if 0
if(!is_numeric($post->menu_order) || $post->menu_orderquery($wpdb->prepare("UPDATE $wpdb->posts SET menu_order = 999 WHERE id = %d ", $post->ID));
}
}

/**
* Meta box callback - asidepanel
*/
function cm_pt_page_meta_box_asidepanel() {
  cm_include_meta_box('page', 'asidepanel');
}
/**
* Meta box callback - headblurb
*/
function cm_pt_page_meta_box_headblurb() {
  cm_include_meta_box('page', 'headblurb');
}

/**
* Meta box callback - homeintro
*/
function cm_pt_page_meta_box_homeintro() {
  cm_include_meta_box('page', 'homeintro');
}

/**
* On save of posts with this post type
*/
add_action('save_post', 'cm_pt_page_save_post');
function cm_pt_page_save_post() {

  // Get some global variables
  global $post;

  // Handle autosave
  if(defined('DOING_AUTOSAVE') && DOING_AUTOSAVE) {
    return $post->ID;
  }

  // If we're saving a page post
  elseif($post->post_type=='page') {
    // Get the page template
    $template_file = get_post_meta($post->ID, '_wp_page_template', true);
  }
  // Pages: all
  update_post_meta($post->ID, "navigation-hide-all", $_POST["page-navigation-hide-all"]);
  update_post_meta($post->ID, "headblurb", $_POST["page-headblurb"]);
  update_post_meta($post->ID, "asidepanel", $_POST["page-asidepanel"]);

  // Pages: all but homepage
  if($template_file!='page-homepage.php') {
    update_post_meta($post->ID, "headerbg", $_POST["page-headerbg"]);
  }
 }
}

Leave a Reply

Your email address will not be published. Required fields are marked *

This article is in the php category. Here are some other related articles also in this category.