Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- /* ------------------------------------------------------------------*/
- /* Register Custom Post Type || RESORTS */
- /* ------------------------------------------------------------------*/
- function resort() {
- $labels = array(
- 'name' => _x( 'Resorts', 'Post Type General Name', 'resort' ),
- 'singular_name' => _x( 'Resort', 'Post Type Singular Name', 'resort' ),
- 'menu_name' => __( 'Resorts', 'resort' ),
- 'parent_item_colon' => __( 'Parent Resort:', 'resort' ),
- 'all_items' => __( 'All Resorts', 'resort' ),
- 'view_item' => __( 'View Resort', 'resort' ),
- 'add_new_item' => __( 'Add New Resort', 'resort' ),
- 'add_new' => __( 'New Resort', 'resort' ),
- 'edit_item' => __( 'Edit Resort', 'resort' ),
- 'update_item' => __( 'Update Resort', 'resort' ),
- 'search_items' => __( 'Search resorts', 'resort' ),
- 'not_found' => __( 'No resorts found', 'resort' ),
- 'not_found_in_trash' => __( 'No resorts found in Trash', 'resort' ),
- );
- $args = array(
- 'label' => __( 'resort', 'resort' ),
- 'description' => __( 'Resort information pages', 'resort' ),
- 'labels' => $labels,
- 'supports' => array('title','editor','page-attributes'),
- 'taxonomies' => array( ),
- 'hierarchical' => true,
- 'public' => true,
- 'show_ui' => true,
- 'show_in_menu' => true,
- 'show_in_nav_menus' => true,
- 'show_in_admin_bar' => true,
- 'menu_position' => 5,
- 'register_meta_box_cb' => 'add_resort_metaboxes',
- 'menu_icon' => '',
- 'can_export' => true,
- 'has_archive' => true,
- 'exclude_from_search' => false,
- 'publicly_queryable' => true,
- 'capability_type' => 'page',
- );
- register_post_type( 'resort', $args );
- }
- // Hook into the 'init' action
- add_action( 'init', 'resort', 0 );
- // Add the Resort Meta Boxes
- function add_resort_metaboxes() {
- add_meta_box('wpt_resorts_info', 'API KEY', 'wpt_resorts_info', 'resort', 'side', 'high');
- }
- // The testimonial Metabox
- function wpt_resorts_info() {
- global $post;
- // Noncename needed to verify where the data originated
- echo '<input type="hidden" name="resortmeta_noncename" id="resortmeta_noncename" value="' .
- wp_create_nonce( plugin_basename(__FILE__) ) . '" />';
- // Get the location data if its already been entered
- $weatherfeed = get_post_meta($post->ID, 'weatherfeed', true);
- // Echo out the field
- echo '<p>Enter API KEY</p>';
- echo '<input type="text" name="weatherfeed" value="' . $weatherfeed . '" class="widefat" />';
- }
- // Save the Metabox Data
- function wpt_save_resorts_meta($post_id, $post) {
- // verify this came from the our screen and with proper authorization,
- // because save_post can be triggered at other times
- if ( !wp_verify_nonce( $_POST['resortmeta_noncename'], plugin_basename(__FILE__) )) {
- return $post->ID;
- }
- // Is the user allowed to edit the post or page?
- if ( !current_user_can( 'edit_post', $post->ID ))
- return $post->ID;
- // OK, we're authenticated: we need to find and save the data
- // We'll put it into an array to make it easier to loop though.
- $resorts_meta['weatherfeed'] = $_POST['weatherfeed'];
- // Add values of $testimonials_meta as custom fields
- foreach ($resorts_meta as $key => $value) { // Cycle through the $events_meta array!
- if( $post->post_type == 'revision' ) return; // Don't store custom data twice
- $value = implode(',', (array)$value); // If $value is an array, make it a CSV (unlikely)
- if(get_post_meta($post->ID, $key, FALSE)) { // If the custom field already has a value
- update_post_meta($post->ID, $key, $value);
- } else { // If the custom field doesn't have a value
- add_post_meta($post->ID, $key, $value);
- }
- if(!$value) delete_post_meta($post->ID, $key); // Delete if blank
- }
- }
- add_action('save_post', 'wpt_save_resorts_meta', 1, 2); // save the custom fields
- function vipx_remove_cpt_slug( $post_link, $post, $leavename ) {
- if ( 'resort' != $post->post_type || 'publish' != $post->post_status ) {
- return $post_link;
- }
- $post_link = str_replace( '/' . $post->post_type . '/', '/', $post_link );
- return $post_link;
- }
- add_filter( 'post_type_link', 'vipx_remove_cpt_slug', 10, 3 );
- function vipx_parse_request_tricksy( $query ) {
- // Only noop the main query
- if ( ! $query->is_main_query() )
- return;
- // Only noop our very specific rewrite rule match
- if ( 2 != count( $query->query ) || ! isset( $query->query['page'] ) ) {
- return;
- }
- // 'name' will be set if post permalinks are just post_name, otherwise the page rule will match
- if ( ! empty( $query->query['name'] ) ) {
- $query->set( 'post_type', array( 'post', 'resort', 'page' ) );
- }
- }
- add_action( 'pre_get_posts', 'vipx_parse_request_tricksy' );
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement