Created
July 17, 2015 18:40
-
-
Save joecue/7d2339e4e9a8b16ab0a4 to your computer and use it in GitHub Desktop.
Announcement Custom Field (Meta Box) and Custom Post Type Example
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?php | |
/* | |
Plugin Name: Announcement Demo Plugin | |
Description: Site specific code changes for example.com | |
*/ | |
/* Start Adding Functions Below this Line */ | |
// Register Custom Post Type | |
function custom_post_type() { | |
$labels = array( | |
'name' => _x( 'Announcments', 'Announcements', 'text_domain' ), | |
'singular_name' => _x( 'Announcment', 'Announcment', 'text_domain' ), | |
'menu_name' => __( 'Announcments', 'text_domain' ), | |
'name_admin_bar' => __( 'Announcments', 'text_domain' ), | |
'parent_item_colon' => __( 'Parent Item:', 'text_domain' ), | |
'all_items' => __( 'All Items', 'text_domain' ), | |
'add_new_item' => __( 'Add New Item', 'text_domain' ), | |
'add_new' => __( 'Add New', 'text_domain' ), | |
'new_item' => __( 'New Item', 'text_domain' ), | |
'edit_item' => __( 'Edit Item', 'text_domain' ), | |
'update_item' => __( 'Update Item', 'text_domain' ), | |
'view_item' => __( 'View Item', 'text_domain' ), | |
'search_items' => __( 'Search Item', 'text_domain' ), | |
'not_found' => __( 'Not found', 'text_domain' ), | |
'not_found_in_trash' => __( 'Not found in Trash', 'text_domain' ), | |
); | |
$args = array( | |
'label' => __( 'announcments', 'text_domain' ), | |
'description' => __( 'Announcment', 'text_domain' ), | |
'labels' => $labels, | |
'supports' => array( ), | |
'taxonomies' => array( 'category', 'post_tag' ), | |
'hierarchical' => false, | |
'public' => true, | |
'show_ui' => true, | |
'show_in_menu' => true, | |
'menu_position' => 5, | |
'show_in_admin_bar' => true, | |
'show_in_nav_menus' => true, | |
'can_export' => true, | |
'has_archive' => true, | |
'exclude_from_search' => false, | |
'publicly_queryable' => true, | |
'capability_type' => 'post', | |
); | |
register_post_type( 'announcements', $args ); | |
} | |
// Hook into the 'init' action | |
add_action( 'init', 'custom_post_type', 0 ); | |
/** | |
* Adds a box to the main column on the Custom Post Type Announcements edit screens. | |
*/ | |
function announcement_add_meta_box() { | |
$screens = array( 'announcements' ); | |
foreach ( $screens as $screen ) { | |
add_meta_box( | |
'announcement_sectionid', | |
__( 'Announcement Dates', 'announcement_textdomain' ), | |
'announcement_meta_box_callback', | |
$screen | |
); | |
} | |
} | |
add_action( 'add_meta_boxes', 'announcement_add_meta_box' ); | |
/** | |
* Prints the box content. | |
* | |
* @param WP_Post $post The object for the current post/page. | |
*/ | |
function announcement_meta_box_callback( $post ) { | |
// Add a nonce field so we can check for it later. | |
wp_nonce_field( 'announcement_save_meta_box_data', 'announcement_meta_box_nonce' ); | |
/* | |
* Use get_post_meta() to retrieve an existing value | |
* from the database and use the value for the form. | |
*/ | |
$value = get_post_meta( $post->ID, '_my_meta_value_key', true ); | |
echo '<label for="announcement_end_date">'; | |
_e( 'End Date:', 'announcement_textdomain' ); | |
echo '</label> '; | |
echo '<input type="text" id="announcement_end_date" name="announcement_end_date" value="' . esc_attr( $value ) . '" size="25" /><br />'; | |
} | |
/** | |
* When the post is saved, saves our custom data. | |
* | |
* @param int $post_id The ID of the post being saved. | |
*/ | |
function announcement_save_meta_box_data( $post_id ) { | |
/* | |
* We need to verify this came from our screen and with proper authorization, | |
* because the save_post action can be triggered at other times. | |
*/ | |
// Check if our nonce is set. | |
if ( ! isset( $_POST['announcement_meta_box_nonce'] ) ) { | |
return; | |
} | |
// Verify that the nonce is valid. | |
if ( ! wp_verify_nonce( $_POST['announcement_meta_box_nonce'], 'announcement_save_meta_box_data' ) ) { | |
return; | |
} | |
// If this is an autosave, our form has not been submitted, so we don't want to do anything. | |
if ( defined( 'DOING_AUTOSAVE' ) && DOING_AUTOSAVE ) { | |
return; | |
} | |
// Check the user's permissions. | |
if ( isset( $_POST['post_type'] ) && 'page' == $_POST['post_type'] ) { | |
if ( ! current_user_can( 'edit_page', $post_id ) ) { | |
return; | |
} | |
} else { | |
if ( ! current_user_can( 'edit_post', $post_id ) ) { | |
return; | |
} | |
} | |
/* OK, it's safe for us to save the data now. */ | |
// Make sure that it is set. | |
if ( ! isset( $_POST['announcement_end_date'] ) ) { | |
return; | |
} | |
// Sanitize user input. | |
$my_data = sanitize_text_field( $_POST['announcement_end_date'] ); | |
// Update the meta field in the database. | |
update_post_meta( $post_id, '_my_meta_value_key', $my_data ); | |
} | |
add_action( 'save_post', 'announcement_save_meta_box_data' ); | |
/* Stop Adding Functions Below this Line */ | |
?> |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment