Guest User

cpt-slug

a guest
Apr 29th, 2014
167
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. /* ------------------------------------------------------------------*/
  2. /* Register Custom Post Type || RESORTS */
  3. /* ------------------------------------------------------------------*/
  4. function resort() {
  5.  
  6. $labels = array(
  7. 'name' => _x( 'Resorts', 'Post Type General Name', 'resort' ),
  8. 'singular_name' => _x( 'Resort', 'Post Type Singular Name', 'resort' ),
  9. 'menu_name' => __( 'Resorts', 'resort' ),
  10. 'parent_item_colon' => __( 'Parent Resort:', 'resort' ),
  11. 'all_items' => __( 'All Resorts', 'resort' ),
  12. 'view_item' => __( 'View Resort', 'resort' ),
  13. 'add_new_item' => __( 'Add New Resort', 'resort' ),
  14. 'add_new' => __( 'New Resort', 'resort' ),
  15. 'edit_item' => __( 'Edit Resort', 'resort' ),
  16. 'update_item' => __( 'Update Resort', 'resort' ),
  17. 'search_items' => __( 'Search resorts', 'resort' ),
  18. 'not_found' => __( 'No resorts found', 'resort' ),
  19. 'not_found_in_trash' => __( 'No resorts found in Trash', 'resort' ),
  20. );
  21. $args = array(
  22. 'label' => __( 'resort', 'resort' ),
  23. 'description' => __( 'Resort information pages', 'resort' ),
  24. 'labels' => $labels,
  25. 'supports' => array('title','editor','page-attributes'),
  26. 'taxonomies' => array( ),
  27. 'hierarchical' => true,
  28. 'public' => true,
  29. 'show_ui' => true,
  30. 'show_in_menu' => true,
  31. 'show_in_nav_menus' => true,
  32. 'show_in_admin_bar' => true,
  33. 'menu_position' => 5,
  34. 'register_meta_box_cb' => 'add_resort_metaboxes',
  35. 'menu_icon' => '',
  36. 'can_export' => true,
  37. 'has_archive' => true,
  38. 'exclude_from_search' => false,
  39. 'publicly_queryable' => true,
  40. 'capability_type' => 'page',
  41. );
  42. register_post_type( 'resort', $args );
  43.  
  44. }
  45.  
  46. // Hook into the 'init' action
  47. add_action( 'init', 'resort', 0 );
  48.  
  49.  
  50. // Add the Resort Meta Boxes
  51. function add_resort_metaboxes() {
  52. add_meta_box('wpt_resorts_info', 'API KEY', 'wpt_resorts_info', 'resort', 'side', 'high');
  53. }
  54.  
  55.  
  56. // The testimonial Metabox
  57. function wpt_resorts_info() {
  58. global $post;
  59.  
  60. // Noncename needed to verify where the data originated
  61. echo '<input type="hidden" name="resortmeta_noncename" id="resortmeta_noncename" value="' .
  62. wp_create_nonce( plugin_basename(__FILE__) ) . '" />';
  63.  
  64. // Get the location data if its already been entered
  65. $weatherfeed = get_post_meta($post->ID, 'weatherfeed', true);
  66.  
  67.  
  68. // Echo out the field
  69. echo '<p>Enter API KEY</p>';
  70. echo '<input type="text" name="weatherfeed" value="' . $weatherfeed . '" class="widefat" />';
  71. }
  72.  
  73. // Save the Metabox Data
  74.  
  75. function wpt_save_resorts_meta($post_id, $post) {
  76.  
  77. // verify this came from the our screen and with proper authorization,
  78. // because save_post can be triggered at other times
  79. if ( !wp_verify_nonce( $_POST['resortmeta_noncename'], plugin_basename(__FILE__) )) {
  80. return $post->ID;
  81. }
  82.  
  83. // Is the user allowed to edit the post or page?
  84. if ( !current_user_can( 'edit_post', $post->ID ))
  85. return $post->ID;
  86.  
  87. // OK, we're authenticated: we need to find and save the data
  88. // We'll put it into an array to make it easier to loop though.
  89.  
  90. $resorts_meta['weatherfeed'] = $_POST['weatherfeed'];
  91.  
  92. // Add values of $testimonials_meta as custom fields
  93.  
  94. foreach ($resorts_meta as $key => $value) { // Cycle through the $events_meta array!
  95. if( $post->post_type == 'revision' ) return; // Don't store custom data twice
  96. $value = implode(',', (array)$value); // If $value is an array, make it a CSV (unlikely)
  97. if(get_post_meta($post->ID, $key, FALSE)) { // If the custom field already has a value
  98. update_post_meta($post->ID, $key, $value);
  99. } else { // If the custom field doesn't have a value
  100. add_post_meta($post->ID, $key, $value);
  101. }
  102. if(!$value) delete_post_meta($post->ID, $key); // Delete if blank
  103. }
  104.  
  105. }
  106.  
  107. add_action('save_post', 'wpt_save_resorts_meta', 1, 2); // save the custom fields
  108.  
  109.  
  110. function vipx_remove_cpt_slug( $post_link, $post, $leavename ) {
  111.  
  112. if ( 'resort' != $post->post_type || 'publish' != $post->post_status ) {
  113. return $post_link;
  114. }
  115.  
  116. $post_link = str_replace( '/' . $post->post_type . '/', '/', $post_link );
  117.  
  118. return $post_link;
  119. }
  120. add_filter( 'post_type_link', 'vipx_remove_cpt_slug', 10, 3 );
  121.  
  122.  
  123. function vipx_parse_request_tricksy( $query ) {
  124.  
  125. // Only noop the main query
  126. if ( ! $query->is_main_query() )
  127. return;
  128.  
  129. // Only noop our very specific rewrite rule match
  130. if ( 2 != count( $query->query ) || ! isset( $query->query['page'] ) ) {
  131. return;
  132. }
  133.  
  134. // 'name' will be set if post permalinks are just post_name, otherwise the page rule will match
  135. if ( ! empty( $query->query['name'] ) ) {
  136. $query->set( 'post_type', array( 'post', 'resort', 'page' ) );
  137. }
  138. }
  139. add_action( 'pre_get_posts', 'vipx_parse_request_tricksy' );
RAW Paste Data