Advertisement
Guest User

WordPress custom post type with custom permalink

a guest
Mar 5th, 2014
170
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
PHP 6.16 KB | None | 0 0
  1.         function register_story_post_type()
  2.         {
  3.                 $labels = array(
  4.                         'name' => __('Story'),
  5.                         'singular_name' => __('Story'),
  6.                         'add_new' => __('Add story'),
  7.                         'all_items'=> __('All stories'),
  8.                         'add_new_item' => __('Add story'),
  9.                         'edit_item' => __('Edit story'),
  10.                         'new_item' => __('New story'),
  11.                         'view_item' => __('View story'),
  12.                         'search_items' => __('Search stories'),
  13.                         'not_found' => __('No stories found'),
  14.                         'not_found_in_trash' => __('No stories found in trash'),
  15.                         'parent_item_colon' => __('Parent story')
  16.                 );
  17.  
  18.                 $args = array(
  19.                         'label' => 'Stories',
  20.                         'labels' => $labels,
  21.                         'public' => true,
  22.                         'has_archive' => true,
  23.                         'rewrite' => false,
  24.                         'publicly_queryable' => true,
  25.                         'query_var' => true,
  26.                         'hierarchical' => false,
  27.                         'capability_type' => 'post',
  28.                         'supports' => array(
  29.                                 'title',
  30.                                 'editor',
  31.                                 'excerpt',
  32.                                 'thumbnail',
  33.                                 'comments',
  34.                                 'author',
  35.                                 'revisions'
  36.                         )
  37.                 );
  38.                 register_post_type('story', $args);
  39.         }
  40.         add_action('init','register_story_post_type');
  41.  
  42.         /* custom post type rewrite rules */
  43.  
  44.  
  45.         global $wp_rewrite;
  46.         $story_structure =/mynews/stories/%year%/%monthnum%/%story%';
  47.        $wp_rewrite->add_rewrite_tag("%story%", '([^/]+)',"story=");
  48.        $wp_rewrite->add_permastruct('story',$story_structure,false);
  49.  
  50.  
  51.        // Add filter to plugin init function
  52.        add_filter('post_type_link', 'story_permalink', 10, 3);
  53.  
  54.  
  55. function story_permalink($permalink, $post_id, $leavename) {
  56.    $post = get_post($post_id);
  57.    $rewritecode = array(
  58.        '%year%',
  59.        '%month%',
  60.        '%monthnum%',
  61.        '%day%',
  62.        '%hour%',
  63.        '%minute%',
  64.        '%second%',
  65.        $leavename? '' : '%postname%',
  66.        '%post_id%',
  67.        '%category%',
  68.        '%author%',
  69.        $leavename? '' : '%pagename%',
  70.    );
  71.  
  72.    if (get_post_type($post_id) == 'story' && '' != $permalink && !in_array($post->post_status, array('draft', 'pending', 'auto-draft')) ) {
  73.        $unixtime = strtotime($post->post_date);
  74.  
  75.        $category = '';
  76.        if ( strpos($permalink, '%category%') !== false ) {
  77.            $cats = get_the_category($post->ID);
  78.            if ( $cats ) {
  79.                usort($cats, '_usort_terms_by_ID'); // order by ID
  80.                $category = $cats[0]->slug;
  81.                if ( $parent = $cats[0]->parent )
  82.                    $category = get_category_parents($parent, false, '/', true) . $category;
  83.            }
  84.            // show default category in permalinks, without
  85.            // having to assign it explicitly
  86.            if ( empty($category) ) {
  87.                $default_category = get_category( get_option( 'default_category' ) );
  88.                $category = is_wp_error( $default_category ) ? '' : $default_category->slug;
  89.            }
  90.        }
  91.  
  92.        $author = '';
  93.        if ( strpos($permalink, '%author%') !== false ) {
  94.            $authordata = get_userdata($post->post_author);
  95.            $author = $authordata->user_nicename;
  96.        }
  97.  
  98.        $date = explode(" ",date('Y M m d H i s', $unixtime));
  99.        $rewritereplace =
  100.        array(
  101.            $date[0],
  102.            $date[1],
  103.            $date[2],
  104.            $date[3],
  105.            $date[4],
  106.            $date[5],
  107.            $date[6],
  108.            $post->post_name,
  109.            $post->ID,
  110.            $category,
  111.            $author,
  112.            $post->post_name,
  113.        );
  114.        $permalink = str_replace($rewritecode, $rewritereplace, $permalink);
  115.    } else { // if they're not using the fancy permalink option
  116.     }
  117.     return $permalink;
  118. }
  119.  
  120. /* set custom messages for post */
  121.         function set_messages($messages) {
  122.                 global $post, $post_ID;
  123.                 $post_type = get_post_type( $post_ID );
  124.  
  125.                 $obj = get_post_type_object($post_type);
  126.                 $singular = $obj->labels->singular_name;
  127.  
  128.                 $messages[$post_type] = array(
  129.                 0 => '', // Unused. Messages start at index 1.
  130.                 1 => sprintf( __($singular.' updated. <a href="%s">View '.strtolower($singular).'</a>'), esc_url( get_permalink($post_ID) ) ),
  131.                 2 => __('Custom field updated.'),
  132.                 3 => __('Custom field deleted.'),
  133.                 4 => __($singular.' updated.'),
  134.                 5 => isset($_GET['revision']) ? sprintf( __($singular.' restored to revision from %s'), wp_post_revision_title( (int) $_GET['revision'], false ) ) : false,
  135.                 6 => sprintf( __($singular.' published. <a href="%s">View '.strtolower($singular).'</a>'), esc_url( get_permalink($post_ID) ) ),
  136.                 7 => __('Page saved.'),
  137.                 8 => sprintf( __($singular.' submitted. <a target="_blank" href="%s">Preview '.strtolower($singular).'</a>'), esc_url( add_query_arg( 'preview', 'true', get_permalink($post_ID) ) ) ),
  138.                 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) ) ),
  139.                 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) ) ) ),
  140.                 );
  141.                 return $messages;
  142.         }
  143.  
  144.         add_filter('post_updated_messages', 'set_messages' );
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement