Advertisement
Guest User

Untitled

a guest
Aug 19th, 2014
258
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 5.02 KB | None | 0 0
  1. /**
  2. * Add a "deck" (aka subhead) meta box to post page(s) and position it
  3. * under the title.
  4. *
  5. * @todo Move to class.
  6. * @see http://codex.wordpress.org/Function_Reference/add_meta_box
  7. * @see http://wordpress.org/extend/ideas/topic/add-meta-box-to-multiple-post-types
  8. * @see https://github.com/Horttcore/WordPress-Subtitle
  9. * @see http://codex.wordpress.org/Function_Reference/wp_nonce_field
  10. */
  11.  
  12. # Adds a box to the main column on the Post and Page edit screens:
  13. function foo_deck($post_type) {
  14.  
  15. # Allowed post types to show meta box:
  16. $post_types = array('post', 'page');
  17.  
  18. if (in_array($post_type, $post_types)) {
  19.  
  20. # Add a meta box to the administrative interface:
  21. add_meta_box(
  22. 'foo-deck-meta-box', // HTML 'id' attribute of the edit screen section.
  23. 'Deck', // Title of the edit screen section, visible to user.
  24. 'foo_deck_meta_box', // Function that prints out the HTML for the edit screen section.
  25. $post_type, // The type of Write screen on which to show the edit screen section.
  26. 'advanced', // The part of the page where the edit screen section should be shown.
  27. 'high' // The priority within the context where the boxes should show.
  28. );
  29.  
  30. }
  31.  
  32. }
  33.  
  34. # Callback that prints the box content:
  35. function foo_deck_meta_box($post) {
  36.  
  37. # Use `get_post_meta()` to retrieve an existing value from the database and use the value for the form:
  38. $deck = get_post_meta($post->ID, '_deck', true);
  39.  
  40. # Form field to display:
  41. ?>
  42.  
  43. <label class="screen-reader-text" for="foo_deck">Deck</label>
  44. <input id="foo_deck" type="text" autocomplete="off" value="<?=esc_attr($deck)?>" name="foo_deck" placeholder="Deck">
  45.  
  46. <?php
  47.  
  48. # Display the nonce hidden form field:
  49. wp_nonce_field(
  50. plugin_basename(__FILE__), // Action name.
  51. 'foo_deck_meta_box' // Nonce name.
  52. );
  53.  
  54. }
  55.  
  56. /**
  57. * @see http://wordpress.stackexchange.com/a/16267/32387
  58. */
  59.  
  60. # Save our custom data when the post is saved:
  61. function foo_deck_save_postdata($post_id) {
  62.  
  63. # Is the current user is authorised to do this action?
  64. if ((($_POST['post_type'] === 'page') && current_user_can('edit_page', $post_id) || current_user_can('edit_post', $post_id))) { // If it's a page, OR, if it's a post, can the user edit it?
  65.  
  66. # Stop WP from clearing custom fields on autosave:
  67. if ((( ! defined('DOING_AUTOSAVE')) || ( ! DOING_AUTOSAVE)) && (( ! defined('DOING_AJAX')) || ( ! DOING_AJAX))) {
  68.  
  69. # Nonce verification:
  70. if (wp_verify_nonce($_POST['foo_deck_meta_box'], plugin_basename(__FILE__))) {
  71.  
  72. # Get the posted deck:
  73. $deck = sanitize_text_field($_POST['foo_deck']);
  74.  
  75. # Add, update or delete?
  76. if ($deck !== '') {
  77.  
  78. # Deck exists, so add OR update it:
  79. add_post_meta($post_id, '_deck', $deck, true) OR update_post_meta($post_id, '_deck', $deck);
  80.  
  81. } else {
  82.  
  83. # Deck empty or removed:
  84. delete_post_meta($post_id, '_deck');
  85.  
  86. }
  87.  
  88. }
  89.  
  90. }
  91.  
  92. }
  93.  
  94. }
  95.  
  96. # Get the deck:
  97. function foo_get_deck($post_id = FALSE) {
  98.  
  99. $post_id = ($post_id) ? $post_id : get_the_ID();
  100.  
  101. return apply_filters('foo_the_deck', get_post_meta($post_id, '_deck', TRUE));
  102.  
  103. }
  104.  
  105. # Display deck (this will feel better when OOP):
  106. function foo_the_deck() {
  107.  
  108. echo foo_get_deck(get_the_ID());
  109.  
  110. }
  111.  
  112. # Conditional checker:
  113. function foo_has_subtitle($post_id = FALSE) {
  114.  
  115. if (foo_get_deck($post_id)) return TRUE;
  116.  
  117. }
  118.  
  119. # Define the custom box:
  120. add_action('add_meta_boxes', 'foo_deck');
  121. # Do something with the data entered:
  122. add_action('save_post', 'foo_deck_save_postdata');
  123.  
  124. /**
  125. * @see http://wordpress.stackexchange.com/questions/36600
  126. * @see http://wordpress.stackexchange.com/questions/94530/
  127. */
  128.  
  129. # Now move advanced meta boxes after the title:
  130. function foo_move_deck() {
  131.  
  132. # Get the globals:
  133. global $post, $wp_meta_boxes;
  134.  
  135. # Output the "advanced" meta boxes:
  136. do_meta_boxes(get_current_screen(), 'advanced', $post);
  137.  
  138. # Remove the initial "advanced" meta boxes:
  139. unset($wp_meta_boxes['post']['advanced']);
  140.  
  141. }
  142.  
  143. add_action('edit_form_after_title', 'foo_move_deck');
  144.  
  145. function foo_deck( $post_type ) {
  146. if ( in_array( $post_type, array( 'post', 'page' ) ) ) {
  147. add_meta_box(
  148. 'contact_details_meta',
  149. 'Contact Details',
  150. 'contact_details_meta',
  151. $post_type,
  152. 'test', // change to something other then normal, advanced or side
  153. 'high'
  154. );
  155. }
  156.  
  157. add_action('add_meta_boxes', 'foo_deck');
  158.  
  159. function foo_move_deck() {
  160. # Get the globals:
  161. global $post, $wp_meta_boxes;
  162.  
  163. # Output the "advanced" meta boxes:
  164. do_meta_boxes( get_current_screen(), 'test', $post );
  165.  
  166. # Remove the initial "advanced" meta boxes:
  167. unset($wp_meta_boxes['post']['test']);
  168. }
  169.  
  170. add_action('edit_form_after_title', 'foo_move_deck');
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement