Last active
December 14, 2015 20:09
-
-
Save joshdavenport/5141974 to your computer and use it in GitHub Desktop.
Example of how to use the Custom Metaboxes and Fields framework for WordPress with pages, hiding the WYSIWYG for pages with managed templates.
Better examples of the types of fields are shown here: https://github.com/jaredatch/Custom-Metaboxes-and-Fields-for-WordPress/blob/master/example-functions.php
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| <?php | |
| /** | |
| * Remove wysiwyg for pages that have theme managed templates | |
| */ | |
| add_action('admin_init', 'fafpp_remove_editor_init'); | |
| function fafpp_remove_editor_init() { | |
| if(isset($_GET['post'])) { | |
| $post_id = $_GET['post']; | |
| $page_template = get_post_meta($post_id, '_wp_page_template', true); | |
| $page_template = ($page_template == 'default' ? false : $page_template); | |
| if(get_post_type($post_id) == 'page' && $page_template ) { | |
| remove_post_type_support('page', 'editor'); | |
| } | |
| } | |
| } | |
| /** | |
| * Initialize the metabox class. | |
| */ | |
| add_action( 'init', 'faf_initialize_cmb_meta_boxes', 9999 ); | |
| function faf_initialize_cmb_meta_boxes() { | |
| if ( ! class_exists( 'cmb_Meta_Box' ) ) | |
| require_once dirname(__FILE__) . '/framework/metabox/init.php'; | |
| } | |
| /** | |
| * Meta boxes | |
| */ | |
| add_filter( 'cmb_meta_boxes', 'faf_metaboxes' ); | |
| function faf_metaboxes( array $meta_boxes ) { | |
| // Start with an underscore to hide fields from custom fields list | |
| $prefix = '_faf_'; | |
| /** | |
| * Standard posts (only used for news) | |
| */ | |
| $meta_boxes[] = array( | |
| 'id' => 'post_meta', | |
| 'title' => 'Post Meta', | |
| 'pages' => array( 'post', ), // Post type | |
| 'context' => 'normal', | |
| 'priority' => 'high', | |
| 'show_names' => true, // Show field names on the left | |
| 'fields' => array( | |
| array( | |
| 'name' => 'Hide date?', | |
| 'id' => $prefix . 'post_hide_date', | |
| 'type' => 'checkbox', | |
| ), | |
| ), | |
| ); | |
| $galleries = array(array('name' => 'Select a gallery', 'value' => '-1')); | |
| global $nggdb; | |
| if($nggdb) { | |
| foreach($nggdb->find_all_galleries('gid', 'DESC', false, false ) as $gallery) { | |
| array_push($galleries, array('name' => $gallery->title, 'value' => $gallery->name)); | |
| } | |
| } | |
| /** | |
| * Home page | |
| */ | |
| $meta_boxes[] = array( | |
| 'id' => 'home_meta', | |
| 'title' => 'Home Meta', | |
| 'pages' => array( 'page', ), // Post type | |
| 'show_on' => array('key' => 'page-template', 'value' => array('page-home.php')), | |
| 'context' => 'normal', | |
| 'priority' => 'high', | |
| 'show_names' => true, // Show field names on the left | |
| 'fields' => array( | |
| array( | |
| 'name' => 'Featured Images', | |
| 'type' => 'title', | |
| ), | |
| array( | |
| 'name' => 'Gallery for Slider', | |
| 'id' => $prefix . 'home_slider', | |
| 'type' => 'select', | |
| 'options' => $galleries | |
| ), | |
| array( | |
| 'name' => 'Featured Image 1', | |
| 'id' => $prefix . 'home_hero1', | |
| 'desc' => 'Optimum size: 885px (w) x 305px (h)', | |
| 'type' => 'file', | |
| 'save_id' => false, // save ID using true | |
| 'allow' => array( 'attachment' ) // limit to just attachments with array( 'attachment' ) | |
| ), | |
| array( | |
| 'name' => 'Featured Image 1 Link URL', | |
| 'id' => $prefix . 'home_hero1_url', | |
| 'type' => 'text', | |
| ), | |
| array( | |
| 'name' => 'Featured Image 2', | |
| 'id' => $prefix . 'home_hero2', | |
| 'desc' => 'Optimum size: 440px (w) x 232px (h)', | |
| 'type' => 'file', | |
| 'save_id' => false, // save ID using true | |
| 'allow' => array( 'attachment' ) // limit to just attachments with array( 'attachment' ) | |
| ), | |
| array( | |
| 'name' => 'Featured Image 2 Link URL', | |
| 'id' => $prefix . 'home_hero2_url', | |
| 'type' => 'text', | |
| ), | |
| array( | |
| 'name' => 'Featured Image 3', | |
| 'id' => $prefix . 'home_hero3', | |
| 'desc' => 'Optimum size: 440px (w) x 232px (h)', | |
| 'type' => 'file', | |
| 'save_id' => false, // save ID using true | |
| 'allow' => array( 'attachment' ) // limit to just attachments with array( 'attachment' ) | |
| ), | |
| array( | |
| 'name' => 'Featured Image 3 Link URL', | |
| 'id' => $prefix . 'home_hero3_url', | |
| 'type' => 'text', | |
| ), | |
| ), | |
| ); | |
| /** | |
| * About page | |
| */ | |
| $meta_boxes[] = array( | |
| 'id' => 'about_meta', | |
| 'title' => 'About Meta', | |
| 'pages' => array( 'page', ), // Post type | |
| 'show_on' => array('key' => 'page-template', 'value' => array('page-about.php')), | |
| 'context' => 'normal', | |
| 'priority' => 'high', | |
| 'show_names' => true, // Show field names on the left | |
| 'fields' => array( | |
| array( | |
| 'name' => 'Team Photo', | |
| 'id' => $prefix . 'about_team_photo', | |
| 'desc' => 'Optimum size: 1200px (w) x 232px (h)', | |
| 'type' => 'file', | |
| 'save_id' => false, // save ID using true | |
| 'allow' => array( 'attachment' ) // limit to just attachments with array( 'attachment' ) | |
| ), | |
| array( | |
| 'name' => 'Our History', | |
| 'id' => $prefix . 'about_history', | |
| 'type' => 'wysiwyg', | |
| ), | |
| array( | |
| 'name' => 'Meet the Team', | |
| 'id' => $prefix . 'about_meet_team', | |
| 'type' => 'wysiwyg', | |
| ), | |
| ) | |
| ); | |
| // build quform list array | |
| $quforms = array(array('name' => 'Select a form', 'value' => '-1')); | |
| if(function_exists('iphorm_get_all_forms')) { | |
| foreach(iphorm_get_all_forms() as $form) { | |
| array_push($quforms, array('name' => $form['name'], 'value' => $form['id'])); | |
| } | |
| } | |
| /** | |
| * Contact page | |
| */ | |
| $meta_boxes[] = array( | |
| 'id' => 'contact_meta', | |
| 'title' => 'Contact Meta', | |
| 'pages' => array( 'page', ), // Post type | |
| 'show_on' => array('key' => 'page-template', 'value' => array('page-contact.php')), | |
| 'context' => 'normal', | |
| 'priority' => 'high', | |
| 'show_names' => true, // Show field names on the left | |
| 'fields' => array( | |
| array( | |
| 'name' => 'Form', | |
| 'id' => $prefix . 'contact_form', | |
| 'type' => 'select', | |
| 'options' => $quforms | |
| ), | |
| array( | |
| 'name' => 'Form Title', | |
| 'id' => $prefix . 'contact_form_title', | |
| 'type' => 'text', | |
| ), | |
| array( | |
| 'name' => 'Left hand content', | |
| 'id' => $prefix . 'contact_content', | |
| 'type' => 'wysiwyg', | |
| 'options' => array( | |
| 'media_buttons' => false, | |
| ), | |
| ), | |
| array( | |
| 'name' => 'Right Hand Image', | |
| 'id' => $prefix . 'contact_rh_image', | |
| 'desc' => 'Optimum size: 218px (w) x 566px (h)', | |
| 'type' => 'file', | |
| 'save_id' => false, // save ID using true | |
| 'allow' => array( 'attachment' ) // limit to just attachments with array( 'attachment' ) | |
| ), | |
| ), | |
| ); | |
| /** | |
| * About page | |
| */ | |
| $meta_boxes[] = array( | |
| 'id' => 'testimonial_meta', | |
| 'title' => 'Testimonial Meta', | |
| 'pages' => array( 'testimonial', ), // Post type | |
| 'context' => 'normal', | |
| 'priority' => 'high', | |
| 'show_names' => true, // Show field names on the left | |
| 'fields' => array( | |
| array( | |
| 'name' => 'Name', | |
| 'id' => $prefix . 'testimonial_name', | |
| 'type' => 'text', | |
| ), | |
| array( | |
| 'name' => 'Talking about...', | |
| 'id' => $prefix . 'testimonial_about', | |
| 'type' => 'text', | |
| ), | |
| array( | |
| 'name' => 'Testimonial', | |
| 'id' => $prefix . 'testimonial_content', | |
| 'type' => 'text', | |
| ), | |
| ), | |
| ); | |
| /** | |
| * Apply for a Job page | |
| */ | |
| $meta_boxes[] = array( | |
| 'id' => 'job_apply_meta', | |
| 'title' => 'Apply for a Job Meta', | |
| 'pages' => array( 'page', ), // Post type | |
| 'show_on' => array('key' => 'page-template', 'value' => array('page-job-apply.php')), | |
| 'context' => 'normal', | |
| 'priority' => 'high', | |
| 'show_names' => true, // Show field names on the left | |
| 'fields' => array( | |
| array( | |
| 'name' => 'Form', | |
| 'type' => 'title', | |
| ), | |
| array( | |
| 'name' => 'Form', | |
| 'id' => $prefix . 'job_form', | |
| 'type' => 'select', | |
| 'options' => $quforms | |
| ), | |
| array( | |
| 'name' => 'Form Title', | |
| 'id' => $prefix . 'job_form_title', | |
| 'type' => 'text', | |
| ), | |
| array( | |
| 'name' => 'Content', | |
| 'type' => 'title', | |
| ), | |
| array( | |
| 'name' => 'Left hand content', | |
| 'id' => $prefix . 'workforus_content', | |
| 'type' => 'wysiwyg', | |
| 'options' => array( | |
| 'media_buttons' => false, | |
| ), | |
| ), | |
| ), | |
| ); | |
| /** | |
| * Join the club | |
| */ | |
| $meta_boxes[] = array( | |
| 'id' => 'join_club_meta', | |
| 'title' => 'Join the Club Meta', | |
| 'pages' => array( 'page', ), // Post type | |
| 'show_on' => array('key' => 'page-template', 'value' => array('page-join-club.php')), | |
| 'context' => 'normal', | |
| 'priority' => 'high', | |
| 'show_names' => true, // Show field names on the left | |
| 'fields' => array( | |
| array( | |
| 'name' => 'Intro Image', | |
| 'id' => $prefix . 'club_intro_image', | |
| 'desc' => 'Optimum size: 395px (w) x 263px (h)', | |
| 'type' => 'file', | |
| 'save_id' => false, // save ID using true | |
| 'allow' => array( 'attachment' ) // limit to just attachments with array( 'attachment' ) | |
| ), | |
| array( | |
| 'name' => 'Intro', | |
| 'id' => $prefix . 'club_intro_text', | |
| 'type' => 'wysiwyg', | |
| 'std' => faf_default_club_intro_text(), | |
| ), | |
| array( | |
| 'name' => 'Form', | |
| 'type' => 'title', | |
| ), | |
| array( | |
| 'name' => 'Form Submission Success Message', | |
| 'id' => $prefix . 'club_form_sucess_message', | |
| 'type' => 'wysiwyg', | |
| 'std' => faf_default_club_thankyou_text(), | |
| ), | |
| array( | |
| 'name' => 'Previous Offer Examples', | |
| 'type' => 'title', | |
| ), | |
| array( | |
| 'name' => 'Show Previous Offer 1?', | |
| 'id' => $prefix . 'club_po_1_show', | |
| 'type' => 'checkbox', | |
| ), | |
| array( | |
| 'name' => 'Previous Offer 1', | |
| 'id' => $prefix . 'club_po_1', | |
| 'type' => 'text', | |
| ), | |
| array( | |
| 'name' => 'Show Previous Offer 2?', | |
| 'id' => $prefix . 'club_po_2_show', | |
| 'type' => 'checkbox', | |
| ), | |
| array( | |
| 'name' => 'Previous Offer 2', | |
| 'id' => $prefix . 'club_po_2', | |
| 'type' => 'text', | |
| ), | |
| array( | |
| 'name' => 'Show Previous Offer 3?', | |
| 'id' => $prefix . 'club_po_3_show', | |
| 'type' => 'checkbox', | |
| ), | |
| array( | |
| 'name' => 'Previous Offer 3', | |
| 'id' => $prefix . 'club_po_3', | |
| 'type' => 'text', | |
| ), | |
| ) | |
| ); | |
| return $meta_boxes; | |
| } |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment