diff --git a/README.md b/README.md old mode 100644 new mode 100755 index d6f0e45..abc3b85 --- a/README.md +++ b/README.md @@ -12,72 +12,85 @@ include('dev7_meta_box_framework.php'); function add_custom_meta_boxes() { $meta_box = array( - 'id' => 'dev7_page_settings', // Meta box ID - 'title' => 'Page Settings', // Meta box title - 'pages' => array('post', 'page'), // Post types this meta box should be shown on - 'context' => 'normal', // Meta box context - 'priority' => 'high', // Meta box priority - 'fields' => array( + 'id' => 'dev7_page_settings', // Meta box ID + 'title' => 'Page Settings', // Meta box title + 'pages' => array('post', 'page'), // Post types this meta box should be shown on + 'context' => 'normal', // Meta box context + 'priority' => 'high', // Meta box priority + 'fields' => array( array( - 'id' => 'text', - 'title' => 'Text', - 'desc' => 'This is a description.', - 'type' => 'text', - 'std' => 'This is std' - ), - array( - 'id' => 'textarea', - 'title' => 'Textarea', - 'desc' => 'This is a description.', - 'type' => 'textarea', - 'std' => 'This is std' - ), - array( - 'id' => 'select', - 'title' => 'Select', - 'desc' => 'This is a description.', - 'type' => 'select', - 'std' => 'green', - 'choices' => array( - 'red' => 'Red', - 'green' => 'Green', - 'blue' => 'Blue' - ) - ), - array( - 'id' => 'radio', - 'title' => 'Radio', - 'desc' => 'This is a description.', - 'type' => 'radio', - 'std' => 'green', - 'choices' => array( - 'red' => 'Red', - 'green' => 'Green', - 'blue' => 'Blue' - ) - ), - array( - 'id' => 'checkbox', - 'title' => 'Checkbox', - 'desc' => 'This is a description.', - 'type' => 'checkbox', - 'std' => 1 - ), - array( - 'id' => 'checkboxes', - 'title' => 'Checkboxes', - 'desc' => 'This is a description.', - 'type' => 'checkboxes', - 'std' => array( - 'red', - 'blue' - ), - 'choices' => array( - 'red' => 'Red', - 'green' => 'Green', - 'blue' => 'Blue' - ) - ) + 'label' => 'Text', + 'desc' => 'This is a description.', + 'id' => 'text', + 'type' => 'text', + 'std' => 'This is std' + ), + array( + 'label' => 'Textarea', + 'desc' => 'This is a description.', + 'id' => 'textarea', + 'type' => 'textarea', + 'std' => 'This is std' + ), + array( + 'label' => 'Select', + 'desc' => 'This is a description.', + 'id' => 'select', + 'type' => 'select', + 'std' => 'green', + 'args' => array( + 'red' => 'Red', + 'green' => 'Green', + 'blue' => 'Blue' + ) + ), + array( + 'label' => 'Radio', + 'desc' => 'This is a description.', + 'id' => 'radio', + 'type' => 'radio', + 'std' => 'green', + 'args' => array( + 'red' => 'Red', + 'green' => 'Green', + 'blue' => 'Blue' + ) + ), + array( + 'label' => 'Checkbox', + 'desc' => 'This is a description.', + 'id' => 'checkbox', + 'type' => 'checkbox', + 'std' => 1 + ), + array( + 'label' => 'Checkboxes', + 'desc' => 'This is a description.', + 'id' => 'checkboxes', + 'type' => 'checkboxes', + 'std' => array( + 'red', + 'blue' + ), + 'args' => array( + 'red' => 'Red', + 'green' => 'Green', + 'blue' => 'Blue' + ) + ), + array( + 'label' => 'WYSIWYG', + 'desc' => 'This is a rich content editor.', + 'type' => 'wysiwyg', + 'id' => 'wysiwyg', + 'args' => array( + 'media_buttons' => false, + 'tinymce' => array( + 'theme_advanced_buttons1' => 'bold,italic,underline' + ), + 'teeny' => true + ) + ) ) ); dev7_add_meta_box( $meta_box ); @@ -94,7 +107,7 @@ $my_field = get_post_meta(get_the_ID(), 'my_field_id', true); A field has the following structure: * `id` (required) - The ID of the field (must be unique) -* `title` - The title of the field +* `label` - The label of the field * `desc` - The field description * `type` (required) - The type of field (see list below) * `std` - The default value of the field diff --git a/dev7_meta_box_framework.php b/dev7_meta_box_framework.php index 1785a97..be9db5b 100644 --- a/dev7_meta_box_framework.php +++ b/dev7_meta_box_framework.php @@ -4,7 +4,7 @@ * * @author Gilbert Pellegrom * @link https://github.com/Dev7studios/Dev7studios-Meta-Box-Framework - * @version 1.0 + * @version 1.0.3 * @license MIT */ @@ -33,7 +33,7 @@ function add_meta_boxes() { foreach( $dev7_meta_boxes as $meta_box ){ if( is_array($meta_box['pages']) ){ foreach( $meta_box['pages'] as $page ){ - add_meta_box( $meta_box['id'], $meta_box['title'], array(&$this, 'meta_box_output'), $page, $meta_box['context'], $meta_box['priority'], array('dev7_meta_box' => $meta_box) ); + add_meta_box( $meta_box['id'].'_mb', $meta_box['title'], array(&$this, 'meta_box_output'), $page, $meta_box['context'], $meta_box['priority'], array('dev7_meta_box' => $meta_box) ); } } else { add_meta_box( $meta_box['id'], $meta_box['title'], array(&$this, 'meta_box_output'), $meta_box['pages'], $meta_box['context'], $meta_box['priority'], array('dev7_meta_box' => $meta_box) ); @@ -42,20 +42,15 @@ function add_meta_boxes() { } function meta_box_save( $post_id ) { - if ( 'page' == $_REQUEST['post_type'] ) { - if ( !current_user_can( 'edit_page', $post_id ) ) - return; - } else { - if ( !current_user_can( 'edit_post', $post_id ) ) - return; - } + + if ( !current_user_can( 'edit_page', $post_id ) || !current_user_can( 'edit_post', $post_id ) ) return; if ( !isset( $_POST['dev7_meta_box_nonce'] ) || !wp_verify_nonce( $_POST['dev7_meta_box_nonce'], plugin_basename( __FILE__ ) ) ) return; global $dev7_meta_boxes; - if( !is_array($dev7_meta_boxes) ) - return; + + if( !is_array($dev7_meta_boxes) ) return; foreach( $dev7_meta_boxes as $meta_box ){ if( isset($meta_box['fields']) && is_array($meta_box['fields']) ){ @@ -88,6 +83,44 @@ function meta_box_output( $post, $args ) { foreach( $dev7_meta_boxes as $meta_box ){ if( isset($args['args']['dev7_meta_box']['id']) && $args['args']['dev7_meta_box']['id'] == $meta_box['id'] ){ if( isset($meta_box['fields']) && is_array($meta_box['fields']) ){ + + ?> + + + + '; + foreach( $meta_box['fields'] as $field ){ if( isset($field['id']) && isset($field['type']) ){ $value = get_post_meta( $post->ID, $field['id'], true ); @@ -100,25 +133,24 @@ function meta_box_output( $post, $args ) { } } - echo '

'; + echo ''; - if( isset($field['name']) && $field['name'] ){ - echo ' '; - } - if( isset($field['desc']) && $field['desc'] ){ - echo ''. $field['desc'] .''; + if( isset($field['label']) && $field['label'] ){ + echo ' '; + echo ''; + } else { + echo ''; } - echo '
'; switch( $field['type'] ){ case 'text': $value = esc_attr(stripslashes($value)); - echo ''; + echo ''; break; case 'textarea': $value = esc_html(stripslashes($value)); - echo ''; + echo ''; break; case 'select': $value = esc_html(esc_attr($value)); @@ -141,7 +173,7 @@ function meta_box_output( $post, $args ) { case 'checkbox': $value = esc_attr(stripslashes($value)); echo ''; - echo ''; + echo ''; break; case 'checkboxes': if( isset($field['choices']) ){ @@ -159,15 +191,24 @@ function meta_box_output( $post, $args ) { break; } - echo '

'; + if( isset($field['desc']) && $field['desc'] && 'checkbox' != $field['type'] ){ + echo '

'. $field['desc'] .'

'; + } + + echo ''; + } - } + } // end foreach field + + echo ''; + } } } } } + new Dev7_Meta_Box_Framework(); } @@ -177,10 +218,17 @@ function meta_box_output( $post, $args ) { function dev7_add_meta_box( $meta_box ) { global $dev7_meta_boxes; - if( !is_array($dev7_meta_boxes) ) - $dev7_meta_boxes = array(); + if( !is_array($dev7_meta_boxes) ) $dev7_meta_boxes = array(); $dev7_meta_boxes[] = $meta_box; } -} \ No newline at end of file +} + +// simple way to return the value +function get_metabox( $key = "" ) { + + return get_post_meta( get_the_ID(), $key, true ); + +} +