Advertisement
Guest User

WordPress custom post types - functions.php

a guest
Jan 6th, 2011
297
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
PHP 3.71 KB | None | 0 0
  1. /**
  2.  * CUSTOM POST TYPES
  3.  */
  4.  
  5.  
  6. // Registers the new post type and taxonomy
  7.  
  8. function txpbs_station_posttype() {
  9.     register_post_type( 'txpbs_stations',
  10.         array(
  11.             'labels' => array(
  12.                 'name' => __( 'Member Stations' ),
  13.                 'singular_name' => __( 'Member Station' ),
  14.                 'add_new' => __( 'Add New Station' ),
  15.                 'add_new_item' => __( 'Add New Station' ),
  16.                 'edit_item' => __( 'Edit Station' ),
  17.                 'new_item' => __( 'Add New Station' ),
  18.                 'view_item' => __( 'View Station' ),
  19.                 'search_items' => __( 'Search Stations' ),
  20.                 'not_found' => __( 'No Stations found' ),
  21.                 'not_found_in_trash' => __( 'No stations found in trash' )
  22.             ),
  23.             'public' => true,
  24.             'supports' => array( 'title', 'editor', 'thumbnail', 'comments' ),
  25.             'capability_type' => 'post',
  26.             'rewrite' => array("slug" => "stations"), // Permalinks format
  27.             'menu_position' => 5,
  28.             'register_meta_box_cb' => 'add_stations_metaboxes'
  29.         )
  30.     );
  31. }
  32. add_action( 'init', 'txpbs_station_posttype' );
  33.  
  34. // Add the Member Station Meta Boxes
  35.  
  36. function add_stations_metaboxes() {
  37.     add_meta_box('txpbs_station_information', 'Station Information', 'txpbs_station_information', 'txpbs_stations', 'side', 'default');
  38. }
  39.  
  40. // The Event Location Metabox
  41.  
  42. function txpbs_station_information() {
  43.     global $post;
  44.  
  45.     // Noncename needed to verify where the data originated
  46.     echo '<input type="hidden" name="stationmeta_noncename" id="stationmeta_noncename" value="' .
  47.     wp_create_nonce( plugin_basename(__FILE__) ) . '" />';
  48.  
  49.     // Get the location data if its already been entered
  50.     $callsign = get_post_meta($post->ID, '_callsign', true);
  51.     $stationurl = get_post_meta($post->ID, '_stationurl', true);
  52.  
  53.     // Echo out the fields
  54.     echo '<p>Enter the station call sign:</p>';
  55.     echo '<input type="text" name="_callsign" value="' . $callsign  . '" class="widefat" />';
  56.     echo '<p>Enter the station URL (website):</p>';
  57.     echo '<input type="text" name="_stationurl" value="' . $stationurl  . '" class="widefat" />';
  58.  
  59. }
  60.  
  61. // Save the Metabox Data
  62.  
  63. function txpbs_save_events_meta($post_id, $post) {
  64.  
  65.     // verify this came from the our screen and with proper authorization,
  66.     // because save_post can be triggered at other times
  67.     if ( !wp_verify_nonce( $_POST['stationmeta_noncename'], plugin_basename(__FILE__) )) {
  68.     return $post->ID;
  69.     }
  70.  
  71.     // Is the user allowed to edit the post or page?
  72.     if ( !current_user_can( 'edit_post', $post->ID ))
  73.         return $post->ID;
  74.  
  75.     // OK, we're authenticated: we need to find and save the data
  76.     // We'll put it into an array to make it easier to loop though.
  77.  
  78.     $station_meta['_callsign'] = $_POST['_callsign'];
  79.     $station_meta['_stationurl'] = $_POST['_stationurl'];
  80.  
  81.     // Add values of $station_meta as custom fields
  82.  
  83.     foreach ($station_meta as $key => $value) { // Cycle through the $station_meta array!
  84.         if( $post->post_type == 'revision' ) return; // Don't store custom data twice
  85.         $value = implode(',', (array)$value); // If $value is an array, make it a CSV (unlikely)
  86.         if(get_post_meta($post->ID, $key, FALSE)) { // If the custom field already has a value
  87.             update_post_meta($post->ID, $key, $value);
  88.         } else { // If the custom field doesn't have a value
  89.             add_post_meta($post->ID, $key, $value);
  90.         }
  91.         if(!$value) delete_post_meta($post->ID, $key); // Delete if blank
  92.     }
  93.  
  94. }
  95.  
  96. add_action('save_post', 'txpbs_save_events_meta', 1, 2); // save the custom fields
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement