Skip to content

Instantly share code, notes, and snippets.

@mor10
Created July 12, 2016 21:58
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save mor10/0a5f70c1bd2241856e1cbff32cb1d007 to your computer and use it in GitHub Desktop.
Save mor10/0a5f70c1bd2241856e1cbff32cb1d007 to your computer and use it in GitHub Desktop.
Create plugin to introduce custom Lectures post type
<?php
/*
Plugin Name: WPCampus Functionality
Plugin URI: https://2016.wpcampus.org/schedule/wordpress-masterclass/
Description: Adds custom post types and taxonomies
Version: 1.0.0
Author: Morten Rand-Hendriksen
Author URI: https://lynda.com/mor10
License: GPL2
License URI: https://www.gnu.org/licenses/gpl-2.0.html
Domain Path: /languages
Text Domain: wpcampus-cpt
*/
/**
* Register a lectures post type.
*
* @link http://codex.wordpress.org/Function_Reference/register_post_type
*/
function wpcampuscpt_lectures_init() {
$labels = array(
'name' => _x( 'Lectures', 'post type general name', 'wpcampus-cpt' ),
'singular_name' => _x( 'Lecture', 'post type singular name', 'wpcampus-cpt' ),
'menu_name' => _x( 'Lectures', 'admin menu', 'wpcampus-cpt' ),
'name_admin_bar' => _x( 'Lecture', 'add new on admin bar', 'wpcampus-cpt' ),
'add_new' => _x( 'Add New', 'lecture', 'wpcampus-cpt' ),
'add_new_item' => __( 'Add New Lecture', 'wpcampus-cpt' ),
'new_item' => __( 'New Lecture', 'wpcampus-cpt' ),
'edit_item' => __( 'Edit Lecture', 'wpcampus-cpt' ),
'view_item' => __( 'View Lecture', 'wpcampus-cpt' ),
'all_items' => __( 'All Lectures', 'wpcampus-cpt' ),
'search_items' => __( 'Search Lectures', 'wpcampus-cpt' ),
'parent_item_colon' => __( 'Parent Lectures:', 'wpcampus-cpt' ),
'not_found' => __( 'No lectures found.', 'wpcampus-cpt' ),
'not_found_in_trash' => __( 'No lectures found in Trash.', 'wpcampus-cpt' )
);
$args = array(
'labels' => $labels,
'description' => __( 'Description.', 'wpcampus-cpt' ),
'public' => true,
'publicly_queryable' => true,
'show_ui' => true,
'show_in_menu' => true,
'query_var' => true,
'rewrite' => array( 'slug' => 'lecture' ),
'capability_type' => 'post',
'has_archive' => true,
'hierarchical' => false,
'menu_position' => null,
'supports' => array( 'title', 'editor', 'author', 'thumbnail', 'excerpt', 'comments' ),
'taxonomies' => array('category', 'post_tag' )
);
register_post_type( 'lecture', $args );
}
add_action( 'init', 'wpcampuscpt_lectures_init' );
/**
* Lecture update messages.
*
* See /wp-admin/edit-form-advanced.php
*
* @param array $messages Existing post update messages.
*
* @return array Amended post update messages with new CPT update messages.
*/
function wpcampuscpt_lecture_updated_messages( $messages ) {
$post = get_post();
$post_type = get_post_type( $post );
$post_type_object = get_post_type_object( $post_type );
$messages['lecture'] = array(
0 => '', // Unused. Messages start at index 1.
1 => __( 'Lecture updated.', 'wpcampus-cpt' ),
2 => __( 'Custom field updated.', 'wpcampus-cpt' ),
3 => __( 'Custom field deleted.', 'wpcampus-cpt' ),
4 => __( 'Lecture updated.', 'wpcampus-cpt' ),
/* translators: %s: date and time of the revision */
5 => isset( $_GET['revision'] ) ? sprintf( __( 'Lecture restored to revision from %s', 'wpcampus-cpt' ), wp_post_revision_title( (int) $_GET['revision'], false ) ) : false,
6 => __( 'Lecture published.', 'wpcampus-cpt' ),
7 => __( 'Lecture saved.', 'wpcampus-cpt' ),
8 => __( 'Lecture submitted.', 'wpcampus-cpt' ),
9 => sprintf(
__( 'Lecture scheduled for: <strong>%1$s</strong>.', 'wpcampus-cpt' ),
// translators: Publish box date format, see http://php.net/date
date_i18n( __( 'M j, Y @ G:i', 'wpcampus-cpt' ), strtotime( $post->post_date ) )
),
10 => __( 'Lecture draft updated.', 'wpcampus-cpt' )
);
if ( $post_type_object->publicly_queryable ) {
$permalink = get_permalink( $post->ID );
$view_link = sprintf( ' <a href="%s">%s</a>', esc_url( $permalink ), __( 'View lecture', 'wpcampus-cpt' ) );
$messages[ $post_type ][1] .= $view_link;
$messages[ $post_type ][6] .= $view_link;
$messages[ $post_type ][9] .= $view_link;
$preview_permalink = add_query_arg( 'preview', 'true', $permalink );
$preview_link = sprintf( ' <a target="_blank" href="%s">%s</a>', esc_url( $preview_permalink ), __( 'Preview lecture', 'wpcampus-cpt' ) );
$messages[ $post_type ][8] .= $preview_link;
$messages[ $post_type ][10] .= $preview_link;
}
return $messages;
}
add_filter( 'post_updated_messages', 'wpcampuscpt_lecture_updated_messages' );
/**
* Flush rewrite rules to make custom ULRs active
*/
function wpcampuscpt_rewrite_flush() {
wpcampuscpt_lectures_init(); //
flush_rewrite_rules();
}
register_activation_hook( __FILE__, 'wpcampuscpt_rewrite_flush' );
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment