ID, '_namespace', true ); // Get the saved values ?>
ID; } // Verify user has permission to edit post if ( !current_user_can( 'edit_post', $post->ID )) { return $post->ID; } // Check that our custom fields are being passed along // This is the `name` value array. We can grab all // of the fields and their values at once. if ( !isset( $_POST['_namespace_custom_metabox'] ) ) { return $post->ID; } /** * Sanitize the submitted data * This keeps malicious code out of our database. * `wp_filter_post_kses` strips our dangerous server values * and allows through anything you can include a post. */ $sanitized = wp_filter_post_kses( $_POST['_namespace_custom_metabox'] ); // Save our submissions to the database update_post_meta( $post->ID, '_namespace', $sanitized ); } add_action( 'save_post', '_namespace_save_metabox', 1, 2 ); // // Save a copy to our revision history // This is optional, and potentially undesireable for certain data types. // Restoring a a post to an old version will also update the metabox. // /** * Save events data to revisions * @param Number $post_id The post ID */ function _namespace_save_revisions( $post_id ) { // Check if it's a revision $parent_id = wp_is_post_revision( $post_id ); // If is revision if ( $parent_id ) { // Get the saved data $parent = get_post( $parent_id ); $details = get_post_meta( $parent->ID, '_namespace', true ); // If data exists and is an array, add to revision if ( !empty( $details ) ) { add_metadata( 'post', $post_id, '_namespace', $details ); } } } add_action( 'save_post', '_namespace_save_revisions' ); /** * Restore events data with post revisions * @param Number $post_id The post ID * @param Number $revision_id The revision ID */ function _namespace_restore_revisions( $post_id, $revision_id ) { // Variables $post = get_post( $post_id ); // The post $revision = get_post( $revision_id ); // The revision $details = get_metadata( 'post', $revision->ID, '_namespace', true ); // The historic version // Replace our saved data with the old version update_post_meta( $post_id, '_namespace', $details ); } add_action( 'wp_restore_post_revision', '_namespace_restore_revisions', 10, 2 ); /** * Get the data to display on the revisions page * @param Array $fields The fields * @return Array The fields */ function _namespace_get_revisions_fields( $fields ) { // Set a title $fields['_namespace'] = 'Some Item'; return $fields; } add_filter( '_wp_post_revision_fields', '_namespace_get_revisions_fields' ); /** * Display the data on the revisions page * @param String|Array $value The field value * @param Array $field The field */ function _namespace_display_revisions_fields( $value, $field ) { global $revision; return get_metadata( 'post', $revision->ID, $field, true ); } add_filter( '_wp_post_revision_field_my_meta', '_namespace_display_revisions_fields', 10, 2 );