Advertisement
Guest User

Untitled

a guest
Apr 26th, 2015
213
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.56 KB | None | 0 0
  1. <?php
  2.  
  3. /**
  4. *
  5. */
  6. class Custom_Meta_Boxes{
  7.  
  8. public function __construct(){
  9.  
  10. add_action( 'add_meta_boxes', array( $this, 'iam_add_meta_box' ) );
  11. add_action( 'save_post', array( $this, 'iam_save_meta_box_data' ) );
  12.  
  13. }
  14.  
  15. /**
  16. * Adds a meta box to the post editing screen
  17. */
  18. public function iam_add_meta_box(){
  19.  
  20. add_meta_box(
  21. 'custom_meta_box',
  22. __( 'Meta Box Title', 'iamtheme' ),
  23. array( $this, 'iam_display_custom_meta_box' ),
  24. 'post',
  25. 'normal',
  26. 'high'
  27. );
  28.  
  29. }
  30.  
  31. /**
  32. * Render Meta Box content.
  33. */
  34. public function iam_display_custom_meta_box() {
  35.  
  36. $html = '';
  37.  
  38. // Add an nonce field so we can check for it later.
  39. wp_nonce_field( 'iam_nonce_check', 'iam_nonce_check_value' );
  40.  
  41. $html = '<label for="link-text" class="prfx-row-title">Link: </label>';
  42. $html .= '<input type="text" name="link-text" id="link-text" value="' . get_post_meta( get_the_ID(), 'link-text', true ) . '" placeholder="Enter your link here." />';
  43.  
  44. echo $html;
  45. }
  46.  
  47. /**
  48. * Save the meta when the post is saved.
  49. */
  50. public function iam_save_meta_box_data( $post_id ){
  51.  
  52. var_dump( $post_id );
  53.  
  54. if ( $this->iam_user_can_save( $post_id, 'iam_nonce_check_value' ) ){
  55.  
  56. // Checks for input and sanitizes/saves if needed
  57. if( isset( $_POST[ 'link-text' ] ) && 0 < count( strlen( trim( $_POST['link-text'] ) ) ) ) {
  58.  
  59. update_post_meta( $post_id, 'link-text', sanitize_text_field( $_POST[ 'link-text' ] ) );
  60.  
  61. }
  62.  
  63. }
  64.  
  65. }
  66.  
  67. /**
  68. * Determines whether or not the current user has the ability to save meta
  69. * data associated with this post.
  70. *
  71. * @param int $post_id The ID of the post being save
  72. * @param bool Whether or not the user has the ability to save this post.
  73. */
  74. public function iam_user_can_save( $post_id, $nonce ){
  75.  
  76. var_dump( $post_id );
  77.  
  78. // Checks save status
  79. $is_autosave = wp_is_post_autosave( $post_id );
  80. $is_revision = wp_is_post_revision( $post_id );
  81. $is_valid_nonce = ( isset( $_POST[ $nonce ] ) && wp_verify_nonce( $_POST[ $nonce ], 'iam_nonce_check' ) ) ? 'true' : 'false';
  82.  
  83. // Return true if the user is able to save; otherwise, false.
  84. if ( $is_autosave || $is_revision || !$is_valid_nonce ) {
  85. return;
  86. }
  87.  
  88. }
  89.  
  90. }
  91.  
  92. // Instantiate theme
  93. if ( class_exists( 'Custom_Meta_Boxes' ) ){
  94. $i_am = new Custom_Meta_Boxes();
  95. }
  96.  
  97. ?>
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement