Advertisement
krzxsiek

METABOX 2

Jan 31st, 2019
177
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
PHP 2.80 KB | None | 0 0
  1. <?php
  2.  
  3. /* ############################################### */
  4. // Kod w header.php
  5. $meta = stref_ban_get_meta_on_page('stref_ban_field');
  6. echo 'Meta: ' . $meta . '<br>';
  7. if ($meta == '1') {
  8.    echo 'Tresc 1';
  9. } elseif ($meta == '2') {
  10.    echo 'Tresc 2';
  11. }
  12. /* ############################################### */
  13. // Kod w functions.php
  14. function stref_ban_get_meta_on_page( $value ) {
  15.     global $wp_query;
  16.     $post_id = $wp_query->post->ID;
  17.  
  18.     $field = get_post_meta( $post_id, $value, true );
  19.     if ( ! empty( $field ) ) {
  20.         return is_array( $field ) ? stripslashes_deep( $field ) : stripslashes( wp_kses_decode_entities( $field ) );
  21.     } else {
  22.         return false;
  23.     }
  24. }
  25.  
  26. function stref_ban_get_meta( $value ) {
  27.     global $post;
  28.  
  29.     $field = get_post_meta( $post->ID, $value, true );
  30.     if ( ! empty( $field ) ) {
  31.         return is_array( $field ) ? stripslashes_deep( $field ) : stripslashes( wp_kses_decode_entities( $field ) );
  32.     } else {
  33.         return false;
  34.     }
  35. }
  36.  
  37. function stref_ban_add() {
  38.     $screens = ['post', 'page'];
  39.     foreach ($screens as $screen) {
  40.         add_meta_box(
  41.             'stref_ban_id',                         // Unique ID
  42.             __( 'Strefa banerowa', 'szablon-kc' ),  // Box title
  43.             'stref_ban_html',   // Content callback, must be of type callable
  44.             $screen,            // Post type
  45.             'normal',           // Context
  46.             'default'           // Priority
  47.         );
  48.     }
  49. }
  50. add_action( 'add_meta_boxes', 'stref_ban_add' );
  51.  
  52. function stref_ban_html( $post) {
  53.     wp_nonce_field( '_stref_ban_nonce', 'stref_ban_nonce' );
  54.     $value = stref_ban_get_meta( 'stref_ban_field' ); ?>
  55.  
  56.     <p>
  57.         <label for="stref_ban_field"><?php _e( 'Wybierz strefe banerową', 'szablon-kc' ); ?></label><br>
  58.         <select name="stref_ban_field" id="stref_ban_field">
  59.             <option value=""><?php _e( 'Domyślna', 'szablon-kc' ); ?></option>
  60.             <option value="1" <?php selected($value, '1'); ?>><?php _e( 'Opcja 1', 'szablon-kc' ); ?></option>
  61.             <option value="2" <?php selected($value, '2'); ?>><?php _e( 'Opcja 2', 'szablon-kc' ); ?></option>
  62.             <option value="3" <?php selected($value, '3'); ?>><?php _e( 'Opcja 3', 'szablon-kc' ); ?></option>
  63.         </select>
  64.     </p><?php
  65. }
  66.  
  67. function stref_ban_save( $post_id ) {
  68.     if ( defined( 'DOING_AUTOSAVE' ) && DOING_AUTOSAVE ) return;
  69.     if ( ! isset( $_POST['stref_ban_nonce'] ) || ! wp_verify_nonce( $_POST['stref_ban_nonce'], '_stref_ban_nonce' ) ) return;
  70.     if ( ! current_user_can( 'edit_post', $post_id ) ) return;
  71.  
  72.     if ( isset( $_POST['stref_ban_field'] ) )
  73.         update_post_meta( $post_id, 'stref_ban_field', esc_attr( $_POST['stref_ban_field'] ) );
  74. }
  75. add_action( 'save_post', 'stref_ban_save' );
  76.  
  77. /*
  78.     Usage: stref_ban_get_meta( 'stref_ban_field' )
  79. */
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement