Meta box for custom sidebar

function ter_create_page_meta_box(){ new TerPageMetaBox(); }

if(is_admin()){
add_action('load-post.php','ter_create_page_meta_box');
add_action('load-post-new.php','ter_create_page_meta_box');
}

class TerPageMetaBox{
public function __construct(){
add_action('add_meta_boxes',array($this,'add_meta_box'));
add_action('save_post',array($this,'save'));
}

public function add_meta_box($post_type){
$post_types = array('page','locations','services','resources');
if(in_array($post_type,$post_types)) add_meta_box('ter_page_meta_box',__('Custom Sidebar HTML','terra'),array($this,'render_meta_box_content'),$post_type,'advanced','high');
}

public function save($post_id){
$post_types = array('page','locations','services','resources');
if(defined('DOING_AUTOSAVE') && DOING_AUTOSAVE) return;
if(!in_array($_POST['post_type'],$post_types)) return $post_id;
if(!wp_verify_nonce($_POST['ter_custom_sidebar'],plugin_basename(__FILE__))) return $post_id;
if(!current_user_can('edit_post',$post_id)) return $post_id;
update_post_meta($post_id,'ter_custom_sidebar_html',$_POST['ter_custom_sidebar_html']);
}

public function render_meta_box_content($post){
wp_nonce_field(plugin_basename(__FILE__),'ter_custom_sidebar');
echo '<label>Sidebar HTML:</label><br>';
echo '<textarea style="width:90%" name="ter_custom_sidebar_html">' . esc_attr(get_post_meta($post->ID,'ter_custom_sidebar_html',1)) . '</textarea>';
}
}

0 Comments

You must be logged in to post a comment.