'post_options' ,'label' => esc_html__('Post Options', 'blogone') ,'post_type' => 'post' ), array( 'id' => 'post_gallery' ,'label' => esc_html__('Post Gallery', 'blogone') ,'post_type' => 'post' ,'context' => 'side' ,'priority' => 'low' ) ); $this->add_meta_box($datas); } function add_meta_box( $datas ){ foreach( $datas as $data ){ $context = 'normal'; $priority = 'high'; if( isset($data['context']) ){ $context = $data['context']; } if( isset($data['priority']) ){ $priority = $data['priority']; } add_meta_box($data['id'], $data['label'], array($this, 'meta_box_callback'), $data['post_type'], $context, $priority, array('file_name'=>$data['id'])); } } function save_meta_boxes( $post_id ){ if( defined('DOING_AUTOSAVE') && DOING_AUTOSAVE ){ return; } if( wp_is_post_revision($post_id) ){ return; } if( isset($_POST['post_type']) ){ if ( 'page' == $_POST['post_type'] ) { if ( !current_user_can('edit_page', $post_id) ) { return $post_id; } } else { if ( !current_user_can('edit_post', $post_id) ) { return $post_id; } } } foreach( $_POST as $key => $value ){ if( strpos($key, 'blogone_') !== false ){ update_post_meta($post_id, $key, $value); } } } function meta_box_callback( $post, $para ){ $file_name = isset($para['args']['file_name'])?$para['args']['file_name']:''; $file = $file_name.'.php'; $options = array(); include $file; $options = apply_filters('blogone_metabox_options_'.$file_name, $options); $this->generate_field_html($options); } function generate_field_html( $options ){ global $post; $defaults = array( 'id' => '' ,'label' => '' ,'desc' => '' ,'type' => 'text' ,'options' => array() /* Use for select box */ ,'default' => '' ); foreach( $options as $option ){ $option = wp_parse_args($option, $defaults); if( $option['id'] == '' ) continue; $post_meta_value = get_post_meta($post->ID, 'blogone_'.$option['id'], true); if( $post_meta_value == '' ) $post_meta_value = $option['default']; $html = ''; switch( $option['type'] ){ case 'text': $html .= '
'; break; case 'select': $select_post = isset($option['select_post']) ? $option['select_post'] : ''; $html .= ''; break; case 'multi_select': wp_enqueue_script('selectWoo'); if( $post_meta_value ){ $post_meta_value = explode(',', $post_meta_value); } if( !is_array($post_meta_value) ){ $post_meta_value = array(); } $html .= ''; break; case 'textarea': $html .= ''; break; case 'editor': $html .= ''; break; case 'upload': $post_meta_value = trim($post_meta_value); $html .= ''; break; case 'heading': $html .= ''; break; case 'gallery': $attachment_ids = array(); if( $post_meta_value != '' ){ $attachment_ids = explode(',', $post_meta_value); } $html .= ''; break; case 'colorpicker': $html .= ''; break; case 'table': if( $post_meta_value ){ $post_meta_value = json_decode( $post_meta_value, true ); } if( !is_array($post_meta_value) ){ $post_meta_value = array( array('', ''), array('', '') ); } $number_col = count($post_meta_value[0]); $html .= ''; break; default: break; } echo trim($html); } } } new Blogone_Metaboxes(); ?>