Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
145 changes: 79 additions & 66 deletions README.md
100644 → 100755
Original file line number Diff line number Diff line change
Expand Up @@ -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 );
Expand All @@ -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
Expand Down
100 changes: 74 additions & 26 deletions dev7_meta_box_framework.php
Original file line number Diff line number Diff line change
Expand Up @@ -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
*/

Expand Down Expand Up @@ -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) );
Expand All @@ -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']) ){
Expand Down Expand Up @@ -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']) ){

?>

<style>
#poststuff .postbox[id*="_mb"] .inside {
margin: 0;
padding: 0;
}
table.meta {
border-collapse: collapse;
width: 100%;
margin: 0;
}
table.meta th, table.meta td {
padding: 1em;
text-align: left;
vertical-align: top;
}
table.meta tr + tr th, table.meta tr + tr td {
border-top: 1px solid #ddd;
}
table.meta th {
width: 25%;
background: #eee;
}
table.meta input[type="text"], table.meta textarea {
width: 100%;
}
table.meta textarea {
height: 150px;
resize: vertical;
}
</style>

<?php

echo '<table class="meta">';

foreach( $meta_box['fields'] as $field ){
if( isset($field['id']) && isset($field['type']) ){
$value = get_post_meta( $post->ID, $field['id'], true );
Expand All @@ -100,25 +133,24 @@ function meta_box_output( $post, $args ) {
}
}

echo '<p>';
echo '<tr>';

if( isset($field['name']) && $field['name'] ){
echo '<label for="'. $field['id'] .'"><strong>'. $field['name'] .'</strong></label> ';
}
if( isset($field['desc']) && $field['desc'] ){
echo '<span class="help">'. $field['desc'] .'</span>';
if( isset($field['label']) && $field['label'] ){
echo '<th><label for="'. $field['id'] .'">'. $field['label'] .'</label></th> ';
echo '<td>';
} else {
echo '<td colspan="2">';
}

echo '<br />';

switch( $field['type'] ){
case 'text':
$value = esc_attr(stripslashes($value));
echo '<input type="text" name="'. $field['id'] .'" id="'. $field['id'] .'" value="'. $value .'" style="width:100%" />';
echo '<input type="text" name="'. $field['id'] .'" id="'. $field['id'] .'" value="'. $value .'" />';
break;
case 'textarea':
$value = esc_html(stripslashes($value));
echo '<textarea name="'. $field['id'] .'" id="'. $field['id'] .'" style="width:100%;height:150px;">'. $value .'</textarea>';
echo '<textarea name="'. $field['id'] .'" id="'. $field['id'] .'">'. $value .'</textarea>';
break;
case 'select':
$value = esc_html(esc_attr($value));
Expand All @@ -141,7 +173,7 @@ function meta_box_output( $post, $args ) {
case 'checkbox':
$value = esc_attr(stripslashes($value));
echo '<input type="hidden" name="'. $field['id'] .'" value="0" />';
echo '<label><input type="checkbox" name="'. $field['id'] .'" id="'. $field['id'] .'" value="1"'. (($value) ? ' checked="checked"' : '') .' /> '. $desc .'</label>';
echo '<label><input type="checkbox" name="'. $field['id'] .'" id="'. $field['id'] .'" value="1"'. (($value) ? ' checked="checked"' : '') .' /> '.$field['desc'].'</label>';
break;
case 'checkboxes':
if( isset($field['choices']) ){
Expand All @@ -159,15 +191,24 @@ function meta_box_output( $post, $args ) {
break;
}

echo '</p>';
if( isset($field['desc']) && $field['desc'] && 'checkbox' != $field['type'] ){
echo '<p class="description">'. $field['desc'] .'</p>';
}

echo '</td></tr>';

}
}
} // end foreach field

echo '</table>';

}
}
}
}

}

new Dev7_Meta_Box_Framework();

}
Expand All @@ -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;
}

}
}

// simple way to return the value
function get_metabox( $key = "" ) {

return get_post_meta( get_the_ID(), $key, true );

}