Morning!
I have a call in my functions.php
file to include a custom meta-box php file in my theme's directory called: /metaboxes/home-meta.php
The code is as follows for the call:
add_action( 'add_meta_boxes_page','load_home_meta' );
function load_home_meta() {
$post_id = $_GET['post'] ? $_GET['post'] : $_POST['post_ID'] ;
if($post_id == '104'){
include( get_template_directory() . '/metaboxes/home-meta.php' );
}
}
The include is now working (had an add_meta_boxes
function wrapped around my metaboxes within the home-meta.php
file below (updated code)).
However, I'm getting errors in each metabox siting the first case
within the switch
for the callback functions. An example error is:
Warning: Invalid argument supplied for foreach() in /Users/[my_mac]/Sites/[site_dir]/wp-content/themes/[site_theme]/metaboxes/home-meta.php on line 224
The home-meta.php
code is:
<?php
/**
* Home Page Custom Meta Content
*
**/
add_meta_box(
'home_meta_box', // $id
'Home Page Content', // $title
'show_home_meta_box', // $callback
'page', // $page
'normal', // $context
'high'); // $priority
add_meta_box(
'home_meta_box_lower_1', // $id
'Home Lower Left', // $title
'show_home_meta_box_lower_left', // $callback
'page', // $page
'normal', // $context
'high'); // $priority
add_meta_box(
'home_meta_box_lower_2', // $id
'Home Lower Center', // $title
'show_home_meta_box_lower_center', // $callback
'page', // $page
'normal', // $context
'high'); // $priority
add_meta_box(
'home_meta_box_lower_3', // $id
'Home Lower Right', // $title
'show_home_meta_box_lower_right', // $callback
'page', // $page
'normal', // $context
'high'); // $priority
// Creating Array for Fields
$prefix = 'home_';
$home_meta_fields = array(
array(
'label' => 'Caption Title',
'desc' => 'Upper section H2 caption title.',
'id' => $prefix.'title',
'type' => 'text'
),
array(
'label' => 'Caption Sub Title',
'desc' => 'Upper section H3 caption title.',
'id' => $prefix.'sub_title',
'type' => 'text'
),
array(
'label'=> 'Caption',
'desc' => 'Caption text block.',
'id' => $prefix.'caption',
'type' => 'textarea'
),
array(
'label' => 'Caption Image',
'desc' => 'Upload a pre-cropped 1140px wide x 530px tall web-optimized image.',
'id' => $prefix.'image',
'type' => 'image'
)
);// end caption array
$prefix2 = 'home_lower_left_';
$home_meta_fields_lower_left = array(
array(
'label' => 'Column Title',
'desc' => 'H2 title for column.',
'id' => $prefix2.'title',
'type' => 'text'
),
array(
'label' => 'Column Image',
'desc' => 'Upload a pre-cropped 360px wide x 300px tall web-optimized image.',
'id' => $prefix2.'image',
'type' => 'image'
),
array(
'label' => 'Column Sub-Title',
'desc' => 'H3 sub-title above text block.',
'id' => $prefix2.'sub_title',
'type' => 'text'
),
array(
'label'=> 'Text block',
'desc' => 'Caption text block.',
'id' => $prefix2.'caption',
'type' => 'textarea'
),
array(
'label' => 'Button Label',
'desc' => 'Button link label (what user reads on button).',
'id' => $prefix2.'btn_label',
'type' => 'text'
),
array(
'label' => 'Button Title Tag',
'desc' => 'On hover and SEO text.',
'id' => $prefix2.'btn_title_tag',
'type' => 'text'
),
array(
'label' => 'Button URL',
'desc' => 'URL where button links to. Enter http:// to work.',
'id' => $prefix2.'btn_url',
'type' => 'text'
)
);// end lower left array
$prefix3 = 'home_lower_center_';
$home_meta_fields_lower_center = array(
array(
'label' => 'Column Title',
'desc' => 'H2 title for column.',
'id' => $prefix3.'title',
'type' => 'text'
),
array(
'label' => 'Column Image',
'desc' => 'Upload a pre-cropped 360px wide x 300px tall web-optimized image.',
'id' => $prefix3.'image',
'type' => 'image'
),
array(
'label' => 'Column Sub-Title',
'desc' => 'H3 sub-title above text block.',
'id' => $prefix3.'sub_title',
'type' => 'text'
),
array(
'label'=> 'Text block',
'desc' => 'Caption text block.',
'id' => $prefix3.'caption',
'type' => 'textarea'
),
array(
'label' => 'Button Label',
'desc' => 'Button link label (what user reads on button).',
'id' => $prefix3.'btn_label',
'type' => 'text'
),
array(
'label' => 'Button Title Tag',
'desc' => 'On hover and SEO text.',
'id' => $prefix3.'btn_title_tag',
'type' => 'text'
),
array(
'label' => 'Button URL',
'desc' => 'URL where button links to. Enter http:// to work.',
'id' => $prefix3.'btn_url',
'type' => 'text'
)
);// end lower center array
$prefix4 = 'home_lower_right_';
$home_meta_fields_lower_right = array(
array(
'label' => 'Column Title',
'desc' => 'H2 title for column.',
'id' => $prefix4.'title',
'type' => 'text'
),
array(
'label' => 'Column Image',
'desc' => 'Upload a pre-cropped 360px wide x 300px tall web-optimized image.',
'id' => $prefix4.'image',
'type' => 'image'
),
array(
'label' => 'Column Sub-Title',
'desc' => 'H3 sub-title above text block.',
'id' => $prefix4.'sub_title',
'type' => 'text'
),
array(
'label'=> 'Text block',
'desc' => 'Caption text block.',
'id' => $prefix4.'caption',
'type' => 'textarea'
),
array(
'label' => 'Button Label',
'desc' => 'Button link label (what user reads on button).',
'id' => $prefix4.'btn_label',
'type' => 'text'
),
array(
'label' => 'Button Title Tag',
'desc' => 'On hover and SEO text.',
'id' => $prefix4.'btn_title_tag',
'type' => 'text'
),
array(
'label' => 'Button URL',
'desc' => 'URL where button links to. Enter http:// to work.',
'id' => $prefix4.'btn_url',
'type' => 'text'
)
);// end lower right array
//The Callback
function show_home_meta_box() {
global $home_meta_fields, $post;
// Using nonce for verification
echo '<input type="hidden" name="home_meta_box_nonce" value="'.wp_create_nonce(basename(__FILE__)).'" />';
//Begin field table and loop
echo '<table class="form-table">';
foreach ($home_meta_fields as $field) {
// get value of this field if it exists for this page
$meta = get_post_meta($post->ID, $field['id'], true);
// begin a table row with
echo '<tr>
<th><label for="'.$field['id'].'">'.$field['label'].'</label></th>
<td>';
switch($field['type']) {
// case items will go here
// text
case 'text':
echo '<input type="text" name="'.$field['id'].'" id="'.$field['id'].'" value="'.$meta.'" size="30" />
<br /><span class="description">'.$field['desc'].'</span>';
break;
// textarea
case 'textarea':
echo '<textarea name="'.$field['id'].'" id="'.$field['id'].'" cols="60" rows="4">'.$meta.'</textarea>
<br /><span class="description">'.$field['desc'].'</span>';
break;
case 'image':
$image = get_template_directory_uri().'/library/images/img-preview-blank.png';
echo '<span class="custom_default_image" style="display:none">'.$image.'</span>';
if ($meta) { $image = wp_get_attachment_image_src($meta, 'medium'); $image = $image[0]; }
echo '<input name="'.$field['id'].'" type="hidden" class="custom_upload_image" value="'.$meta.'" />
<img src="'.$image.'" class="custom_preview_image" style="max-width:300px" alt="" /><br />
<input class="custom_upload_image_button button" type="button" value="Choose Image" />
<small> <a href="#" class="custom_clear_image_button">Remove Image</a></small>
<br clear="all" /><span class="description">'.$field['desc'].'';
break;
} // end switch
echo '</td></tr>';
} // end foreach
echo '</table>'; // end table
}
// Save the Data
function save_home_meta($post_id) {
global $home_meta_fields;
// verify nonce
if (!wp_verify_nonce($_POST['home_meta_box_nonce'], basename(__FILE__)))
return $post_id;
// check autosave
if (defined('DOING_AUTOSAVE') && DOING_AUTOSAVE)
return $post_id;
// check permissions
if ('page' == $_POST['post_type']) {
if (!current_user_can('edit_page', $post_id))
return $post_id;
} elseif (!current_user_can('edit_post', $post_id)) {
return $post_id;
}
// loop through fields and save the data
foreach ($home_meta_fields as $field) {
$old = get_post_meta($post_id, $field['id'], true);
$new = $_POST[$field['id']];
if ($new && $new != $old) {
update_post_meta($post_id, $field['id'], $new);
} elseif ('' == $new && $old) {
delete_post_meta($post_id, $field['id'], $old);
}
} // end foreach
}
add_action('save_post', 'save_home_meta');
// end Upper Home Content
// Lower Left
//The Callback
function show_home_meta_box_lower_left() {
global $home_meta_fields_lower_left, $post;
// Using nonce for verification
echo '<input type="hidden" name="home_meta_box_left_nonce" value="'.wp_create_nonce(basename(__FILE__)).'" />';
//Begin field table and loop
echo '<table class="form-table">';
foreach ($home_meta_fields_lower_left as $field) {
// get value of this field if it exists for this page
$meta = get_post_meta($post->ID, $field['id'], true);
// begin a table row with
echo '<tr>
<th><label for="'.$field['id'].'">'.$field['label'].'</label></th>
<td>';
switch($field['type']) {
// case items will go here
// text
case 'text':
echo '<input type="text" name="'.$field['id'].'" id="'.$field['id'].'" value="'.$meta.'" size="30" />
<br /><span class="description">'.$field['desc'].'</span>';
break;
// textarea
case 'textarea':
echo '<textarea name="'.$field['id'].'" id="'.$field['id'].'" cols="60" rows="4">'.$meta.'</textarea>
<br /><span class="description">'.$field['desc'].'</span>';
break;
case 'image':
$image = get_template_directory_uri().'/library/images/img-preview-blank.png';
echo '<span class="custom_default_image" style="display:none">'.$image.'</span>';
if ($meta) { $image = wp_get_attachment_image_src($meta, 'medium'); $image = $image[0]; }
echo '<input name="'.$field['id'].'" type="hidden" class="custom_upload_image" value="'.$meta.'" />
<img src="'.$image.'" class="custom_preview_image" style="max-width:300px" alt="" /><br />
<input class="custom_upload_image_button button" type="button" value="Choose Image" />
<small> <a href="#" class="custom_clear_image_button">Remove Image</a></small>
<br clear="all" /><span class="description">'.$field['desc'].'';
break;
} // end switch
echo '</td></tr>';
} // end foreach
echo '</table>'; // end table
}
// Save the Data
function save_home_meta_lower_left($post_id) {
global $home_meta_fields_lower_left;
// verify nonce
if (!wp_verify_nonce($_POST['home_meta_box_left_nonce'], basename(__FILE__)))
return $post_id;
// check autosave
if (defined('DOING_AUTOSAVE') && DOING_AUTOSAVE)
return $post_id;
// check permissions
if ('page' == $_POST['post_type']) {
if (!current_user_can('edit_page', $post_id))
return $post_id;
} elseif (!current_user_can('edit_post', $post_id)) {
return $post_id;
}
// loop through fields and save the data
foreach ($home_meta_fields_lower_left as $field) {
$old = get_post_meta($post_id, $field['id'], true);
$new = $_POST[$field['id']];
if ($new && $new != $old) {
update_post_meta($post_id, $field['id'], $new);
} elseif ('' == $new && $old) {
delete_post_meta($post_id, $field['id'], $old);
}
} // end foreach
}
add_action('save_post', 'save_home_meta_lower_left');
// end lower left
// Lower Center Column
//The Callback
function show_home_meta_box_lower_center() {
global $home_meta_fields_lower_center, $post;
// Using nonce for verification
echo '<input type="hidden" name="home_meta_box_center_nonce" value="'.wp_create_nonce(basename(__FILE__)).'" />';
//Begin field table and loop
echo '<table class="form-table">';
foreach ($home_meta_fields_lower_center as $field) {
// get value of this field if it exists for this page
$meta = get_post_meta($post->ID, $field['id'], true);
// begin a table row with
echo '<tr>
<th><label for="'.$field['id'].'">'.$field['label'].'</label></th>
<td>';
switch($field['type']) {
// case items will go here
// text
case 'text':
echo '<input type="text" name="'.$field['id'].'" id="'.$field['id'].'" value="'.$meta.'" size="30" />
<br /><span class="description">'.$field['desc'].'</span>';
break;
// textarea
case 'textarea':
echo '<textarea name="'.$field['id'].'" id="'.$field['id'].'" cols="60" rows="4">'.$meta.'</textarea>
<br /><span class="description">'.$field['desc'].'</span>';
break;
case 'image':
$image = get_template_directory_uri().'/library/images/img-preview-blank.png';
echo '<span class="custom_default_image" style="display:none">'.$image.'</span>';
if ($meta) { $image = wp_get_attachment_image_src($meta, 'medium'); $image = $image[0]; }
echo '<input name="'.$field['id'].'" type="hidden" class="custom_upload_image" value="'.$meta.'" />
<img src="'.$image.'" class="custom_preview_image" style="max-width:300px" alt="" /><br />
<input class="custom_upload_image_button button" type="button" value="Choose Image" />
<small> <a href="#" class="custom_clear_image_button">Remove Image</a></small>
<br clear="all" /><span class="description">'.$field['desc'].'';
break;
} // end switch
echo '</td></tr>';
} // end foreach
echo '</table>'; // end table
}
// Save the Data
function save_home_meta_lower_center($post_id) {
global $home_meta_fields_lower_center;
// verify nonce
if (!wp_verify_nonce($_POST['home_meta_box_center_nonce'], basename(__FILE__)))
return $post_id;
// check autosave
if (defined('DOING_AUTOSAVE') && DOING_AUTOSAVE)
return $post_id;
// check permissions
if ('page' == $_POST['post_type']) {
if (!current_user_can('edit_page', $post_id))
return $post_id;
} elseif (!current_user_can('edit_post', $post_id)) {
return $post_id;
}
// loop through fields and save the data
foreach ($home_meta_fields_lower_center as $field) {
$old = get_post_meta($post_id, $field['id'], true);
$new = $_POST[$field['id']];
if ($new && $new != $old) {
update_post_meta($post_id, $field['id'], $new);
} elseif ('' == $new && $old) {
delete_post_meta($post_id, $field['id'], $old);
}
} // end foreach
}
add_action('save_post', 'save_home_meta_lower_center');
// end lower center
// Lower Right Column
//The Callback
function show_home_meta_box_lower_right() {
global $home_meta_fields_lower_right, $post;
// Using nonce for verification
echo '<input type="hidden" name="home_meta_box_right_nonce" value="'.wp_create_nonce(basename(__FILE__)).'" />';
//Begin field table and loop
echo '<table class="form-table">';
foreach ($home_meta_fields_lower_right as $field) {
// get value of this field if it exists for this page
$meta = get_post_meta($post->ID, $field['id'], true);
// begin a table row with
echo '<tr>
<th><label for="'.$field['id'].'">'.$field['label'].'</label></th>
<td>';
switch($field['type']) {
// case items will go here
// text
case 'text':
echo '<input type="text" name="'.$field['id'].'" id="'.$field['id'].'" value="'.$meta.'" size="30" />
<br /><span class="description">'.$field['desc'].'</span>';
break;
// textarea
case 'textarea':
echo '<textarea name="'.$field['id'].'" id="'.$field['id'].'" cols="60" rows="4">'.$meta.'</textarea>
<br /><span class="description">'.$field['desc'].'</span>';
break;
case 'image':
$image = get_template_directory_uri().'/library/images/img-preview-blank.png';
echo '<span class="custom_default_image" style="display:none">'.$image.'</span>';
if ($meta) { $image = wp_get_attachment_image_src($meta, 'medium'); $image = $image[0]; }
echo '<input name="'.$field['id'].'" type="hidden" class="custom_upload_image" value="'.$meta.'" />
<img src="'.$image.'" class="custom_preview_image" style="max-width:300px" alt="" /><br />
<input class="custom_upload_image_button button" type="button" value="Choose Image" />
<small> <a href="#" class="custom_clear_image_button">Remove Image</a></small>
<br clear="all" /><span class="description">'.$field['desc'].'';
break;
} // end switch
echo '</td></tr>';
} // end foreach
echo '</table>'; // end table
}
// Save the Data
function save_home_meta_lower_right($post_id) {
global $home_meta_fields_lower_right;
// verify nonce
if (!wp_verify_nonce($_POST['home_meta_box_right_nonce'], basename(__FILE__)))
return $post_id;
// check autosave
if (defined('DOING_AUTOSAVE') && DOING_AUTOSAVE)
return $post_id;
// check permissions
if ('page' == $_POST['post_type']) {
if (!current_user_can('edit_page', $post_id))
return $post_id;
} elseif (!current_user_can('edit_post', $post_id)) {
return $post_id;
}
// loop through fields and save the data
foreach ($home_meta_fields_lower_right as $field) {
$old = get_post_meta($post_id, $field['id'], true);
$new = $_POST[$field['id']];
if ($new && $new != $old) {
update_post_meta($post_id, $field['id'], $new);
} elseif ('' == $new && $old) {
delete_post_meta($post_id, $field['id'], $old);
}
} // end foreach
}
add_action('save_post', 'save_home_meta_lower_right');
// end lower right
?>
home-meta.php
, can you create a very simpleadd_meta_box()
call, and in the callback, just output someHELLO WORLD
text?