Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- /**
- * CUSTOM POST TYPES
- */
- // Registers the new post type and taxonomy
- function txpbs_station_posttype() {
- register_post_type( 'txpbs_stations',
- array(
- 'labels' => array(
- 'name' => __( 'Member Stations' ),
- 'singular_name' => __( 'Member Station' ),
- 'add_new' => __( 'Add New Station' ),
- 'add_new_item' => __( 'Add New Station' ),
- 'edit_item' => __( 'Edit Station' ),
- 'new_item' => __( 'Add New Station' ),
- 'view_item' => __( 'View Station' ),
- 'search_items' => __( 'Search Stations' ),
- 'not_found' => __( 'No Stations found' ),
- 'not_found_in_trash' => __( 'No stations found in trash' )
- ),
- 'public' => true,
- 'supports' => array( 'title', 'editor', 'thumbnail', 'comments' ),
- 'capability_type' => 'post',
- 'rewrite' => array("slug" => "stations"), // Permalinks format
- 'menu_position' => 5,
- 'register_meta_box_cb' => 'add_stations_metaboxes'
- )
- );
- }
- add_action( 'init', 'txpbs_station_posttype' );
- // Add the Member Station Meta Boxes
- function add_stations_metaboxes() {
- add_meta_box('txpbs_station_information', 'Station Information', 'txpbs_station_information', 'txpbs_stations', 'side', 'default');
- }
- // The Event Location Metabox
- function txpbs_station_information() {
- global $post;
- // Noncename needed to verify where the data originated
- echo '<input type="hidden" name="stationmeta_noncename" id="stationmeta_noncename" value="' .
- wp_create_nonce( plugin_basename(__FILE__) ) . '" />';
- // Get the location data if its already been entered
- $callsign = get_post_meta($post->ID, '_callsign', true);
- $stationurl = get_post_meta($post->ID, '_stationurl', true);
- // Echo out the fields
- echo '<p>Enter the station call sign:</p>';
- echo '<input type="text" name="_callsign" value="' . $callsign . '" class="widefat" />';
- echo '<p>Enter the station URL (website):</p>';
- echo '<input type="text" name="_stationurl" value="' . $stationurl . '" class="widefat" />';
- }
- // Save the Metabox Data
- function txpbs_save_events_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['stationmeta_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.
- $station_meta['_callsign'] = $_POST['_callsign'];
- $station_meta['_stationurl'] = $_POST['_stationurl'];
- // Add values of $station_meta as custom fields
- foreach ($station_meta as $key => $value) { // Cycle through the $station_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', 'txpbs_save_events_meta', 1, 2); // save the custom fields
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement