banner-meta.php 1.7 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253
  1. <?php
  2. function page_options_meta() {
  3. add_meta_box( 'page_option', esc_html__( 'Page Options', 'taskereasy' ), 'page_option_meta', 'page', 'normal', 'high' );
  4. }
  5. add_action( 'add_meta_boxes', 'page_options_meta' );
  6. /**
  7. * Outputs the content of the meta box
  8. */
  9. function page_option_meta( $post ) {
  10. wp_nonce_field( basename( __FILE__ ), 'pageopt_nonce' );
  11. $pagopt_stored_meta = get_post_meta( $post->ID );
  12. ?>
  13. <p>
  14. <span class="pageopt-row-title"><?php _e( 'Check if hide page banner: ', 'taskereasy' )?></span>
  15. <div class="pageopt-row-content">
  16. <label for="featured-checkbox">
  17. <input type="checkbox" name="page-banner" id="page-banner" value="yes" <?php if ( isset ( $pagopt_stored_meta['page-banner'] ) ) checked( $pagopt_stored_meta['page-banner'][0], 'yes' ); ?> />
  18. <?php _e( 'Hide Page Banner', 'taskereasy' )?>
  19. </label>
  20. </div>
  21. </p>
  22. <?php
  23. }
  24. /**
  25. * Saves the custom meta input
  26. */
  27. function pageopt_meta_save( $post_id ) {
  28. // Checks save status - overcome autosave, etc.
  29. $is_autosave = wp_is_post_autosave( $post_id );
  30. $is_revision = wp_is_post_revision( $post_id );
  31. $is_valid_nonce = ( isset( $_POST[ 'pageopt_nonce' ] ) && wp_verify_nonce( $_POST[ 'pageopt_nonce' ], basename( __FILE__ ) ) ) ? 'true' : 'false';
  32. // Exits script depending on save status
  33. if ( $is_autosave || $is_revision || !$is_valid_nonce ) {
  34. return;
  35. }
  36. // Checks for input and saves - save checked as yes and unchecked at no
  37. if( isset( $_POST[ 'page-banner' ] ) ) {
  38. update_post_meta( $post_id, 'page-banner', 'yes' );
  39. } else {
  40. update_post_meta( $post_id, 'page-banner', 'no' );
  41. }
  42. }
  43. add_action( 'save_post', 'pageopt_meta_save' );