<?php
/**
* @package Blog_Posts
* @version 0.1
*/
/*
Plugin Name: DD Custom Post
Plugin URI: none
Description: This is a custom post type base/boilerplate
Author: Dan Dvoracek
Version: 0.1
Author URI: http://www.dandvoracek.com
*/
add_action('init', 'custompost_register');
function custompost_register() {
// For more details on the following: http://codex.wordpress.org/Function_Reference/register_post_type
$labels = array(
/*By including the context in the pot file translators can translate the two strings differently.*/
/*https://codex.wordpress.org/Function_Reference/_x*/
'name' => _x('Custom Post', 'post type general name'),
'singular_name' => _x('Custom Item', 'post type singular name'),
'add_new' => _x('Add New', 'custom item'),
'add_new_item' => __('Add New Custom Item'),
'edit_item' => __('Edit Custom Item'),
'new_item' => __('New Custom Item'),
'view_item' => __('View Custom Item'),
'search_items' => __('Search Custom Items'),
'not_found' => __('Nothing found'),
'not_found_in_trash' => __('Nothing found in Trash')
// ,'parent_item_colon' => ''
);
$args = array(
'labels' => $labels,
'public' => true, // – should the posts be shown in the admin UI
'publicly_queryable' => true, // - whether queries can be performed on the front end as part of parse_request()
'show_ui' => true,// – should we display an admin panel for this custom post type
'query_var' => true, // - the query_var key for this post type
// 'menu_icon' => get_stylesheet_directory_uri() . '/someIcon.png', // – a custom icon for the admin panel
'rewrite' => true, // – rewrites permalinks using the slug ‘portfolio’
'capability_type' => 'post', // – WordPress will treat this as a ‘post’ for read, edit, and delete capabilities
'hierarchical' => false, // – is it hierarchical, like pages
'menu_position' => null, // - position in the menu order the post type should appear. show_in_menu must be true.
'supports' => array('title','editor','thumbnail') // – which items do we want to display on the add/edit post page
);
// Register the post type object;
// The name should only contain lowercase letters and the underscore character, and not be more than 32 characters long, otherwise taxonomy issues
// https://wordpress.org/support/topic/custom-taxonomy-doesnt-appear-in-the-custom-menu
// http://codex.wordpress.org/Function_Reference/register_taxonomy
register_post_type( 'custom' , $args );
// The first item here is the taxonomy name, Custom Label. The second(Custom) is the name of the object type we’re applying it to see line just above
// Doc: http://codex.wordpress.org/Function_Reference/register_taxonomy
register_taxonomy("news", array("custom"), array('public'=>true, "hierarchical" => true, "label" => "custom labels", "singular_label" => "custom label", "rewrite" => true, 'show_ui' => true));
}
/*-- Adding custom fields --*/
/* --- In order to add more fields and different types of field ---
- http://mikejolley.com/2012/12/21/using-the-new-wordpress-3-5-media-uploader-in-plugins/
- https://codestag.com/how-to-use-wordpress-3-5-media-uploader-in-theme-options/
- http://codex.wordpress.org/Function_Reference/add_meta_box
- http://wordpress.stackexchange.com/questions/135471/metabox-repeating-fields-radio-buttons-not-saving-correctly/135513#135513
- https://github.com/nkdeck07/Wordpress-Plugin--Media-Upload-Skeleton
*/
add_action("admin_init", "admin_init");
function admin_init(){
add_meta_box("year_completed-meta", "Year Completed", "year_completed", "custom", "side", "low");
add_meta_box("custom_meta", "Others & More", "custom_meta", "custom", "normal", "low");
}
function year_completed(){
global $post;
$custom = get_post_custom($post->ID);
$year_completed = $custom["year_completed"][0];
?>
<label>Year:</label>
<input name="year_completed" value="<?php echo $year_completed; ?>" />
<?php
}
function custom_meta() {
global $post;
$custom = get_post_custom($post->ID);
$custom_field_1 = $custom["custom_field_1"][0];
$custom_field_2 = $custom["custom_field_2"][0];
$custom_field_3 = $custom["custom_field_3"][0];
?>
<p><label>First field:</label><br />
<textarea cols="50" rows="5" name="custom_field_1"><?php echo $custom_field_1; ?></textarea></p>
<p><label>Second Field:</label><br />
<textarea cols="50" rows="5" name="custom_field_2"><?php echo $custom_field_2; ?></textarea></p>
<p><label>Third Field:</label><br />
<textarea cols="50" rows="5" name="custom_field_3"><?php echo $custom_field_3; ?></textarea></p>
<?php
}
// Save the info
add_action('save_post', 'save_details');
function save_details(){
global $post;
// Make sure we reuse and save all the values triggered in year_completed() and custom_meta()
update_post_meta($post->ID, "year_completed", $_POST["year_completed"]);
update_post_meta($post->ID, "custom_field_1", $_POST["custom_field_1"]);
update_post_meta($post->ID, "custom_field_2", $_POST["custom_field_2"]);
update_post_meta($post->ID, "custom_field_3", $_POST["custom_field_3"]);
}
// send infos to page in order to display
add_action("manage_posts_custom_column", "custom_custom_columns");
add_filter("manage_edit-custom_columns", "custom_edit_columns");
// Define Custom POst list display/infos
function custom_edit_columns($columns){
$columns = array(
"cb" => "<input type='checkbox' />",
"title" => "Custom Title",
"description" => "Description",
"year" => "Year Completed",
"news" => "Some News",
);
return $columns;
}
function custom_custom_columns($column){
global $post;
switch ($column) {
case "description":
the_excerpt();
break;
case "year":
$custom = get_post_custom();
echo $custom["year_completed"][0];
break;
case "news":
echo get_the_term_list($post->ID, 'News', '', ', ','');
break;
}
}
Little snippet for any basic post type. Also added some custom fields.
TODO: add media upload to "boilerplate"
TODO: add media upload to "boilerplate"
Be the first to comment
You can use [html][/html], [css][/css], [php][/php] and more to embed the code. Urls are automatically hyperlinked. Line breaks and paragraphs are automatically generated.