Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
Snippet to establish a WordPress custom post type in vanilla PHP
<?php
/**
* Plugin Name: MITlib Content Model
*
* @package mitlib-content-model
* @version 0.0.1
*/
/**
* Register content types
*/
function alert_definition() {
$labels = array(
'name' => _x( 'Alerts', 'Post Type General Name', 'text_domain' ),
'singular_name' => _x( 'Alert', 'Post Type Singular Name', 'text_domain' ),
'menu_name' => __( 'Alerts', 'text_domain' ),
'name_admin_bar' => __( 'Alert', 'text_domain' ),
'parent_item_colon' => __( 'Maihaugen', 'text_domain' ),
'all_items' => __( 'All Alerts', 'text_domain' ),
'add_new_item' => __( 'Add Alert', 'text_domain' ),
'add_new' => __( 'New Alert', 'text_domain' ),
'new_item' => __( 'New Alert', 'text_domain' ),
'edit_item' => __( 'Edit Alert', 'text_domain' ),
'update_item' => __( 'Update Alert', 'text_domain' ),
'view_item' => __( 'View Alert', 'text_domain' ),
'search_items' => __( 'Search Alerts', 'text_domain' ),
'not_found' => __( 'No Alerts found', 'text_domain' ),
'not_found_in_trash' => __( 'No Alerts found in Trash', 'text_domain' ),
);
$args = array(
'label' => 'Alert',
'description' => 'Alert',
'labels' => $labels,
'supports' => array( 'title', 'editor' ),
'hierarchical' => false,
'public' => true,
'show_ui' => true,
'show_in_menu' => true,
'show_in_rest' => true,
'menu_position' => 5,
'menu_icon' => 'dashicons-format-gallery',
'show_in_admin_bar' => true,
'show_in_nav_menus' => true,
'can_export' => true,
'has_archive' => true,
'exclude_from_search' => false,
'publicly_queryable' => true,
'capability_type' => 'post',
'rewrite' => array(
'slug' => 'alert',
),
);
register_post_type( 'alerts', $args );
}
add_action( 'init', 'alert_definition', 0 );
/**
* Defines start and end date fields (simple text for now)
*/
add_action( 'admin_init', 'admin_init' );
function admin_init() {
add_meta_box("alert_startdate-meta", "Start date", "alert_dates", "alerts", "side", "low");
}
function alert_dates(){
global $post;
$custom = get_post_custom($post->ID);
$alert_startdate = $custom["alert_startdate"][0];
$alert_enddate = $custom["alert_enddate"][0];
?>
<label>Start date:</label>
<input name="alert_startdate" value="<?php echo $alert_startdate; ?>" />
<label>End date:</label>
<input name="alert_enddate" value="<?php echo $alert_enddate; ?>" />
<?php
}
add_action( 'save_post', 'save_details' );
function save_details() {
global $post;
update_post_meta( $post->ID, 'alert_startdate', $_POST['alert_startdate'] );
update_post_meta( $post->ID, 'alert_enddate', $_POST['alert_enddate'] );
}
/**
* Modifies content list to use custom fields
*/
add_action( 'manage_posts_custom_column', 'alerts_custom_columns' );
add_filter( 'manage_edit-alerts_columns', 'alerts_edit_columns' );
function alerts_edit_columns( $columns ) {
$columns = array(
"cb" => '<input type="checkbox" />',
"title" => 'Alert Headline',
"started" => 'Start Date',
"ended" => 'End Date',
);
return $columns;
}
function alerts_custom_columns( $column ) {
global $post;
switch ( $column ) {
case "ended":
$custom = get_post_custom();
echo $custom["alert_enddate"][0];
break;
case "started":
$custom = get_post_custom();
echo $custom["alert_startdate"][0];
break;
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
You can’t perform that action at this time.