Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- function register_story_post_type()
- {
- $labels = array(
- 'name' => __('Story'),
- 'singular_name' => __('Story'),
- 'add_new' => __('Add story'),
- 'all_items'=> __('All stories'),
- 'add_new_item' => __('Add story'),
- 'edit_item' => __('Edit story'),
- 'new_item' => __('New story'),
- 'view_item' => __('View story'),
- 'search_items' => __('Search stories'),
- 'not_found' => __('No stories found'),
- 'not_found_in_trash' => __('No stories found in trash'),
- 'parent_item_colon' => __('Parent story')
- );
- $args = array(
- 'label' => 'Stories',
- 'labels' => $labels,
- 'public' => true,
- 'has_archive' => true,
- 'rewrite' => false,
- 'publicly_queryable' => true,
- 'query_var' => true,
- 'hierarchical' => false,
- 'capability_type' => 'post',
- 'supports' => array(
- 'title',
- 'editor',
- 'excerpt',
- 'thumbnail',
- 'comments',
- 'author',
- 'revisions'
- )
- );
- register_post_type('story', $args);
- }
- add_action('init','register_story_post_type');
- /* custom post type rewrite rules */
- global $wp_rewrite;
- $story_structure = ‘/mynews/stories/%year%/%monthnum%/%story%';
- $wp_rewrite->add_rewrite_tag("%story%", '([^/]+)',"story=");
- $wp_rewrite->add_permastruct('story',$story_structure,false);
- // Add filter to plugin init function
- add_filter('post_type_link', 'story_permalink', 10, 3);
- function story_permalink($permalink, $post_id, $leavename) {
- $post = get_post($post_id);
- $rewritecode = array(
- '%year%',
- '%month%',
- '%monthnum%',
- '%day%',
- '%hour%',
- '%minute%',
- '%second%',
- $leavename? '' : '%postname%',
- '%post_id%',
- '%category%',
- '%author%',
- $leavename? '' : '%pagename%',
- );
- if (get_post_type($post_id) == 'story' && '' != $permalink && !in_array($post->post_status, array('draft', 'pending', 'auto-draft')) ) {
- $unixtime = strtotime($post->post_date);
- $category = '';
- if ( strpos($permalink, '%category%') !== false ) {
- $cats = get_the_category($post->ID);
- if ( $cats ) {
- usort($cats, '_usort_terms_by_ID'); // order by ID
- $category = $cats[0]->slug;
- if ( $parent = $cats[0]->parent )
- $category = get_category_parents($parent, false, '/', true) . $category;
- }
- // show default category in permalinks, without
- // having to assign it explicitly
- if ( empty($category) ) {
- $default_category = get_category( get_option( 'default_category' ) );
- $category = is_wp_error( $default_category ) ? '' : $default_category->slug;
- }
- }
- $author = '';
- if ( strpos($permalink, '%author%') !== false ) {
- $authordata = get_userdata($post->post_author);
- $author = $authordata->user_nicename;
- }
- $date = explode(" ",date('Y M m d H i s', $unixtime));
- $rewritereplace =
- array(
- $date[0],
- $date[1],
- $date[2],
- $date[3],
- $date[4],
- $date[5],
- $date[6],
- $post->post_name,
- $post->ID,
- $category,
- $author,
- $post->post_name,
- );
- $permalink = str_replace($rewritecode, $rewritereplace, $permalink);
- } else { // if they're not using the fancy permalink option
- }
- return $permalink;
- }
- /* set custom messages for post */
- function set_messages($messages) {
- global $post, $post_ID;
- $post_type = get_post_type( $post_ID );
- $obj = get_post_type_object($post_type);
- $singular = $obj->labels->singular_name;
- $messages[$post_type] = array(
- 0 => '', // Unused. Messages start at index 1.
- 1 => sprintf( __($singular.' updated. <a href="%s">View '.strtolower($singular).'</a>'), esc_url( get_permalink($post_ID) ) ),
- 2 => __('Custom field updated.'),
- 3 => __('Custom field deleted.'),
- 4 => __($singular.' updated.'),
- 5 => isset($_GET['revision']) ? sprintf( __($singular.' restored to revision from %s'), wp_post_revision_title( (int) $_GET['revision'], false ) ) : false,
- 6 => sprintf( __($singular.' published. <a href="%s">View '.strtolower($singular).'</a>'), esc_url( get_permalink($post_ID) ) ),
- 7 => __('Page saved.'),
- 8 => sprintf( __($singular.' submitted. <a target="_blank" href="%s">Preview '.strtolower($singular).'</a>'), esc_url( add_query_arg( 'preview', 'true', get_permalink($post_ID) ) ) ),
- 9 => sprintf( __($singular.' scheduled for: <strong>%1$s</strong>. <a target="_blank" href="%2$s">Preview '.strtolower($singular).'</a>'), date_i18n( __( 'M j, Y @ G:i' ), strtotime( $post->post_date ) ), esc_url( get_permalink($post_ID) ) ),
- 10 => sprintf( __($singular.' draft updated. <a target="_blank" href="%s">Preview '.strtolower($singular).'</a>'), esc_url( add_query_arg( 'preview', 'true', get_permalink($post_ID) ) ) ),
- );
- return $messages;
- }
- add_filter('post_updated_messages', 'set_messages' );
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement