Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- <?php
- /* ############################################### */
- // Kod w header.php
- $meta = stref_ban_get_meta_on_page('stref_ban_field');
- echo 'Meta: ' . $meta . '<br>';
- if ($meta == '1') {
- echo 'Tresc 1';
- } elseif ($meta == '2') {
- echo 'Tresc 2';
- }
- /* ############################################### */
- // Kod w functions.php
- function stref_ban_get_meta_on_page( $value ) {
- global $wp_query;
- $post_id = $wp_query->post->ID;
- $field = get_post_meta( $post_id, $value, true );
- if ( ! empty( $field ) ) {
- return is_array( $field ) ? stripslashes_deep( $field ) : stripslashes( wp_kses_decode_entities( $field ) );
- } else {
- return false;
- }
- }
- function stref_ban_get_meta( $value ) {
- global $post;
- $field = get_post_meta( $post->ID, $value, true );
- if ( ! empty( $field ) ) {
- return is_array( $field ) ? stripslashes_deep( $field ) : stripslashes( wp_kses_decode_entities( $field ) );
- } else {
- return false;
- }
- }
- function stref_ban_add() {
- $screens = ['post', 'page'];
- foreach ($screens as $screen) {
- add_meta_box(
- 'stref_ban_id', // Unique ID
- __( 'Strefa banerowa', 'szablon-kc' ), // Box title
- 'stref_ban_html', // Content callback, must be of type callable
- $screen, // Post type
- 'normal', // Context
- 'default' // Priority
- );
- }
- }
- add_action( 'add_meta_boxes', 'stref_ban_add' );
- function stref_ban_html( $post) {
- wp_nonce_field( '_stref_ban_nonce', 'stref_ban_nonce' );
- $value = stref_ban_get_meta( 'stref_ban_field' ); ?>
- <p>
- <label for="stref_ban_field"><?php _e( 'Wybierz strefe banerową', 'szablon-kc' ); ?></label><br>
- <select name="stref_ban_field" id="stref_ban_field">
- <option value=""><?php _e( 'Domyślna', 'szablon-kc' ); ?></option>
- <option value="1" <?php selected($value, '1'); ?>><?php _e( 'Opcja 1', 'szablon-kc' ); ?></option>
- <option value="2" <?php selected($value, '2'); ?>><?php _e( 'Opcja 2', 'szablon-kc' ); ?></option>
- <option value="3" <?php selected($value, '3'); ?>><?php _e( 'Opcja 3', 'szablon-kc' ); ?></option>
- </select>
- </p><?php
- }
- function stref_ban_save( $post_id ) {
- if ( defined( 'DOING_AUTOSAVE' ) && DOING_AUTOSAVE ) return;
- if ( ! isset( $_POST['stref_ban_nonce'] ) || ! wp_verify_nonce( $_POST['stref_ban_nonce'], '_stref_ban_nonce' ) ) return;
- if ( ! current_user_can( 'edit_post', $post_id ) ) return;
- if ( isset( $_POST['stref_ban_field'] ) )
- update_post_meta( $post_id, 'stref_ban_field', esc_attr( $_POST['stref_ban_field'] ) );
- }
- add_action( 'save_post', 'stref_ban_save' );
- /*
- Usage: stref_ban_get_meta( 'stref_ban_field' )
- */
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement