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

How To Clone a Select box WordPress

  • SOLVED

I'm having an issue which I can't seem to get pass on cloning a select box inside a plugin I'm creating. I just can't seem to grasp the concept of how to do it properly.

I've created the metabox and field options with an array as follows...


$prefix = 'wpwebinar_';
$meta_boxes = array(
array(
'id' => 'settings_headline_mb',
'title' => 'Optin Page Settings',
'page' => 'wpwebinar',
'context' => 'normal',
'priority' => 'high',
'fields' => array(
array(
'name' => 'Selection Test',
'desc' => 'Testing Saving The Selection Below.',
'id' => $prefix . 'tests',
'type' => 'selecttest',
'options' => array('1' => 'orange', '2' => 'green', '3' => 'blue')
)
)
);


I'm then using a switch/case for this element like so...


case 'selecttest':
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 $value => $option) {
echo '<option value="',$value,'"', $meta == $value ? ' selected="selected"' : '', '>', $option, '</option>';
}
echo '</select>';
break;


I'm currently using jquery.appendo http://deepliquid.com/content/Appendo.html to do the "clone" function.

Here's my switch/case with the code for appendo... I've simply added a table around the select box called "appendo"


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


I know I'm not saving it properly, but here is my save code for everything.


add_action('save_post', 'wpwebinar_save_data');

// Save data from meta box

function wpwebinar_save_data($post_id) {
global $meta_boxes;

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


I'm currently using jquery.appendo http://deepliquid.com/content/Appendo.html to do the "clone" function. I'm open to change if necessary. I'm not stuck on this.

MY PROBLEM...

The jquery.appendo allows me to duplicate the field as many times as the user likes. I can get the code to duplicate the field, but when I save, the additional fields disappear and are not displayed in when refreshed.

Also, I have a small issue displaying the code on the frontend.

Any ideas? I appreciate the help. This is just bugging me big time. Thanks.

Answers (3)

2011-11-02

jevusi answers:

i think you could use the code i have paid here : [[LINK href="http://wpquestions.com/question/show/id/3170"]]http://wpquestions.com/question/show/id/3170[[/LINK]]


Armand Morin comments:

Thank, but I have most everything listed, the problem here lies on displaying the saved code. You solution is great, but it would require me to rewrite a large portion of what I have in a much different way.

2011-11-02

Gabriel Reguly answers:

Hi Amand,

This does not look correct:

echo '</select>';

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



I would use this:


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


Regards,
Gabriel


Armand Morin comments:

Gabriel,

Thanks... in my code I have it correct as you suggested. I was copying code to recreate it here for an easy paste and I screwed up. Thanks for the catch though.


Gabriel Reguly comments:

Amand,

Another fix you should make


'id' => 'settings_headline_mb',



'id' => 'settings_headline_mb[]',


I am not sure if WordPress would accept it, but PHP surely needs it to make the 'cloning'.

Also I am afraid you will need to forget about using the id field for the label and the select.

Change it to:


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


Hopefully it will solve your issues.

Regards,
Gabriel


Gabriel Reguly comments:

Hi Amand,

Any progress?

Regards,
Gabriel

2011-11-02

Fahad Murtaza answers:

Trying to solve it.


Fahad Murtaza comments:

I see jevusi has a working solution already. If that works for you. You can either get a refund or accept his answer.


Armand Morin comments:

In looking at jevusi version, it would take reworking everything to figure what I want, which is just how to save an display this one simple select box with repeats. If you have a solution using the code above, I would love to see it.


Fahad Murtaza comments:

Can you give me your complete code for plugin, it will be easy to debug then. You can send me a direct message with the link to plugin zip or upload it somewhere so I can download; if you don't want to share complete plugin here.


Armand Morin comments:

I've just sent you a PM with the link to download.