Ask your WordPress questions! Pay money and get answers fast! Comodo Trusted Site Seal
Official PayPal Seal

Create Another Metabox Using This Code WordPress

  • SOLVED

I'm using a code to create a Metabox I've included below. My problem is when I want to create ANOTHER Metabox. Yes, I could duplicate the code, but that seems a bit redundant. I would like a modification of the code to all me to create another MetaBox by simply creating another array.

I don't want to use a different Metabox class, as this is a plugin and I've found that most classes are not setup to account for two plugins using the same class.

Here's my code I'm currently using. Any ideas?


//Create MetaBox Array
$meta_box = array(
'id' => 'my-meta-box',
'title' => 'Custom meta box',
'page' => 'optinpage',
'context' => 'normal',
'priority' => 'high',
'fields' => array(
array(
'name' => 'Text box',
'desc' => 'Enter something here',
'id' => $prefix . 'text',
'type' => 'text',
'std' => 'Default value 1'
)
)
);

// Adds MetaBox
add_action('admin_menu', 'mytheme_add_box');
function mytheme_add_box() {
global $meta_box;

add_meta_box($meta_box['id'], $meta_box['title'], 'mytheme_show_box', $meta_box['page'], $meta_box['context'], $meta_box['priority']);
}


// Callback function to show fields in meta box
function mytheme_show_box() {
global $meta_box, $post;

// Use nonce for verification
echo '<input type="hidden" name="mytheme_meta_box_nonce" value="', wp_create_nonce(basename(__FILE__)), '" />';

echo '<div class="optinpage">';

foreach ($meta_box['fields'] as $field) {
// get current post meta data
$meta = get_post_meta($post->ID, $field['id'], true);

switch ($field['type']) {
case 'text':
echo '<label for="', $field['id'], '">', $field['name'], '</label><div class="description">', $field['desc'],'</div>';
echo '<input type="text" name="', $field['id'], '" id="', $field['id'], '" value="', $meta ? $meta : $field['std'], '" size="30" style="width:97%" />';
break;
case 'textarea':
echo '<label for="', $field['id'], '">', $field['name'], '</label><div class="description">', $field['desc'],'</div>';
echo '<textarea name="', $field['id'], '" id="', $field['id'], '" cols="60" rows="4" style="width:97%">', $meta ? $meta : $field['std'], '</textarea>';
break;
case 'wysiwyg':
echo '<label for="', $field['id'], '">', $field['name'], '</label><div class="description">', $field['desc'],'</div>';
wp_editor($meta ? $meta : $field['std'], $field['id']);
break;
case 'select':
echo '<label for="', $field['id'], '">', $field['name'], '</label><div class="description">', $field['desc'],'</div>';
echo '<select name="', $field['id'], '" id="', $field['id'], '">';
foreach ($field['options'] as $option) {
echo '<option', $meta == $option ? ' selected="selected"' : '', '>', $option, '</option>';
}
echo '</select>';
break;
case 'radio':
echo '<label for="', $field['id'], '">', $field['name'], '</label><div class="description">', $field['desc'],'</div>';
foreach ($field['options'] as $option) {
echo '<input type="radio" name="', $field['id'], '" value="', $option['value'], '"', $meta == $option['value'] ? ' checked="checked"' : '', ' />', $option['name'];
}
break;
case 'checkbox':
echo '<label for="', $field['id'], '">', $field['name'], '</label><div class="description">', $field['desc'],'</div>';
echo '<input type="checkbox" name="', $field['id'], '" id="', $field['id'], '"', $meta ? ' checked="checked"' : '', ' />';
break;
}

}

echo '</div>';
}


add_action('save_post', 'mytheme_save_data');

// Save data from meta box
function mytheme_save_data($post_id) {
global $meta_box;

// verify nonce
if (!wp_verify_nonce($_POST['mytheme_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;
}

foreach ($meta_box['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);
}
}
}

Answers (2)

2011-10-26

Maor Barazany answers:

Take a look in the updated code I wrote below...
Removed my first partial answer.


Maor Barazany comments:

Sorry, I forgot some things.
This is the whole code:


<?php
//Create MetaBox Array
$meta_boxes = array(
array(
'id' => 'my-meta-box',
'title' => 'Custom meta box',
'page' => 'optinpage',
'context' => 'normal',
'priority' => 'high',
'fields' => array(
array(
'name' => 'Text box',
'desc' => 'Enter something here',
'id' => $prefix . 'text',
'type' => 'text',
'std' => 'Default value 1'
)
)
),

array(
'id' => 'my-meta-box2',
'title' => 'Custom meta box 2',
'page' => 'optinpage',
'context' => 'normal',
'priority' => 'high',
'fields' => array(
array(
'name' => 'Text box',
'desc' => 'Enter something here',
'id' => $prefix . 'text2',
'type' => 'text',
'std' => 'Default value 1'
)
)
)
);

// Adds MetaBox

add_action('admin_menu', 'mytheme_add_box');

function mytheme_add_box() {
global $meta_boxes;

foreach ($meta_boxes as $meta_box) :
add_meta_box($meta_box['id'], $meta_box['title'], 'mytheme_show_box', $meta_box['page'], $meta_box['context'], $meta_box['priority']);
endforeach;
}


// Callback function to show fields in meta box

function mytheme_show_box() {

global $meta_boxes, $post;
// Use nonce for verification
echo '<input type="hidden" name="mytheme_meta_box_nonce" value="', wp_create_nonce(basename(__FILE__)), '" />';
echo '<div class="optinpage">';

foreach ($meta_boxes as $meta_box) {
foreach ($meta_box['fields'] as $field) {
// get current post meta data
$meta = get_post_meta($post->ID, $field['id'], true);
switch ($field['type']) {
case 'text':
echo '<label for="', $field['id'], '">', $field['name'], '</label><div class="description">', $field['desc'],'</div>';
echo '<input type="text" name="', $field['id'], '" id="', $field['id'], '" value="', $meta ? $meta : $field['std'], '" size="30" style="width:97%" />';
break;

case 'textarea':
echo '<label for="', $field['id'], '">', $field['name'], '</label><div class="description">', $field['desc'],'</div>';
echo '<textarea name="', $field['id'], '" id="', $field['id'], '" cols="60" rows="4" style="width:97%">', $meta ? $meta : $field['std'], '</textarea>';
break;

case 'wysiwyg':
echo '<label for="', $field['id'], '">', $field['name'], '</label><div class="description">', $field['desc'],'</div>';
wp_editor($meta ? $meta : $field['std'], $field['id']);
break;

case 'select':
echo '<label for="', $field['id'], '">', $field['name'], '</label><div class="description">', $field['desc'],'</div>';
echo '<select name="', $field['id'], '" id="', $field['id'], '">';
foreach ($field['options'] as $option) {
echo '<option', $meta == $option ? ' selected="selected"' : '', '>', $option, '</option>';
}
echo '</select>';
break;

case 'radio':
echo '<label for="', $field['id'], '">', $field['name'], '</label><div class="description">', $field['desc'],'</div>';
foreach ($field['options'] as $option) {
echo '<input type="radio" name="', $field['id'], '" value="', $option['value'], '"', $meta == $option['value'] ? ' checked="checked"' : '', ' />', $option['name'];
}
break;

case 'checkbox':
echo '<label for="', $field['id'], '">', $field['name'], '</label><div class="description">', $field['desc'],'</div>';
echo '<input type="checkbox" name="', $field['id'], '" id="', $field['id'], '"', $meta ? ' checked="checked"' : '', ' />';
break;
}
}
}
echo '</div>';
}


add_action('save_post', 'mytheme_save_data');


// Save data from meta box

function mytheme_save_data($post_id) {

global $meta_boxes;

// verify nonce
if (!wp_verify_nonce($_POST['mytheme_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;
}

foreach ($meta_boxes as $meta_box) {

foreach ($meta_box['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);
}
}
}
}


Armand Morin comments:

Almost worked... it created the two metaboxes but blew an error...

"Warning: Invalid argument supplied for foreach()"

It points out this line... as the culprit...

foreach ($meta_box['fields'] as $field) {

Ideas? I tried simply changing $meta_box to $metaboxes but that didn't work.


Armand Morin comments:

I tried with the first code... let me try the whole code you just posted.


Maor Barazany comments:

As you may see, the first array is now $meta_boxes and it contains arrays, each one is a metabox. You must remember not to use the same id key for different metaboxes.
Just add arrays there as you need..


Maor Barazany comments:

Yes, the first code does contain errors... I wrote it quick.
Let me know if now it is working well..


Armand Morin comments:

Ok, tried it... looks good at first... but then I noticed that the content is duplicated in both fields. In this case, I have two Metaboxes with two text boxes in each displaying he same info.


Armand Morin comments:

One more issue... it doesn't save any text inputs.


Maor Barazany comments:

Please try to re-check this.
I've tried this code now on a local dev WP and it works fine - one metabox with two text fields (the second field I added for testing creation of 2 fields), and it also saves the data.

Do you have a custom post type named optinpage?

This line -
'page' => 'optinpage',

determine in which CPT the box will appear in.
I changed to post, and it appears on posts.


Armand Morin comments:

I made a video with what I'm seeing and a commentary as to what it should do... I hope this helps.

http://www.screencast.com/users/armandmorin/folders/Default/media/301ab769-358f-4331-8ac0-c4a85d569938


Maor Barazany comments:

Ok, there were several issues there (again, since I wrote it quick to reply you asap...)
I've rewritten it, try this full code now -


//Create MetaBox Array
$meta_boxes = array(
array(
'id' => 'my-meta-box',
'title' => 'Custom meta box',
'page' => 'page',
'context' => 'normal',
'priority' => 'high',
'fields' => array(
array(
'name' => 'Text box',
'desc' => 'Enter something here',
'id' => $prefix . 'text',
'type' => 'text',
'std' => 'Default value 1'
)
)
),

array(
'id' => 'my-meta-box2',
'title' => 'Custom meta box 2',
'page' => 'page',
'context' => 'normal',
'priority' => 'high',
'fields' => array(
array(
'name' => 'Text box',
'desc' => 'Enter something here2',
'id' => $prefix . 'text2',
'type' => 'text',
'std' => 'Default value 1'
)
)
)
);

// Adds MetaBox

add_action('admin_menu', 'mytheme_add_box');

function mytheme_add_box() {
global $meta_boxes;

foreach ($meta_boxes as $meta_box) :
add_meta_box($meta_box['id'], $meta_box['title'], 'mytheme_show_box', $meta_box['page'], $meta_box['context'], $meta_box['priority'], array( 'id' => $meta_box['id']));

endforeach;
}


// Callback function to show fields in meta box

function mytheme_show_box( $post, $mb_id) {

global $meta_boxes, $post;
// Use nonce for verification
echo '<input type="hidden" name="mytheme_meta_box_nonce" value="', wp_create_nonce(basename(__FILE__)), '" />';
echo '<div class="optinpage">';

foreach ($meta_boxes as $meta_box) {
if ($meta_box['id'] == $mb_id['id']) :
foreach ($meta_box['fields'] as $field) {
// get current post meta data
$meta = get_post_meta($post->ID, $field['id'], true);
switch ($field['type']) {
case 'text':
echo '<label for="', $field['id'], '">', $field['name'], '</label><div class="description">', $field['desc'],'</div>';
echo '<input type="text" name="', $field['id'], '" id="', $field['id'], '" value="', $meta ? $meta : $field['std'], '" size="30" style="width:97%" />';
break;

case 'textarea':
echo '<label for="', $field['id'], '">', $field['name'], '</label><div class="description">', $field['desc'],'</div>';
echo '<textarea name="', $field['id'], '" id="', $field['id'], '" cols="60" rows="4" style="width:97%">', $meta ? $meta : $field['std'], '</textarea>';
break;

case 'wysiwyg':
echo '<label for="', $field['id'], '">', $field['name'], '</label><div class="description">', $field['desc'],'</div>';
wp_editor($meta ? $meta : $field['std'], $field['id']);
break;

case 'select':
echo '<label for="', $field['id'], '">', $field['name'], '</label><div class="description">', $field['desc'],'</div>';
echo '<select name="', $field['id'], '" id="', $field['id'], '">';
foreach ($field['options'] as $option) {
echo '<option', $meta == $option ? ' selected="selected"' : '', '>', $option, '</option>';
}
echo '</select>';
break;

case 'radio':
echo '<label for="', $field['id'], '">', $field['name'], '</label><div class="description">', $field['desc'],'</div>';
foreach ($field['options'] as $option) {
echo '<input type="radio" name="', $field['id'], '" value="', $option['value'], '"', $meta == $option['value'] ? ' checked="checked"' : '', ' />', $option['name'];
}
break;

case 'checkbox':
echo '<label for="', $field['id'], '">', $field['name'], '</label><div class="description">', $field['desc'],'</div>';
echo '<input type="checkbox" name="', $field['id'], '" id="', $field['id'], '"', $meta ? ' checked="checked"' : '', ' />';
break;
}
}
endif;
}
echo '</div>';
}


add_action('save_post', 'mytheme_save_data');


// Save data from meta box

function mytheme_save_data($post_id) {

global $meta_boxes;

// verify nonce
if (!wp_verify_nonce($_POST['mytheme_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;
}

foreach ($meta_boxes as $meta_box) {

foreach ($meta_box['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);
}
}
}
}


Armand Morin comments:

Works perfectly! thank you so much.

2011-10-26

Fahad Murtaza answers:

I am on it.


Fahad Murtaza comments:

There is no class in the functions above. So you want to use just above functions right?


Armand Morin comments:

Yes, there is no class here. I just want to use the above functions of course modified. Something like... $meta_boxes[] = array.... then do a foreach type statement. The problem I have is that I'm relatively new to programming, so I'm not sure how to write the foreach statement which would be needed.


Fahad Murtaza comments:

Looks like you already got the help.