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

duplicate a field without plugin WordPress

  • SOLVED

I create a metabox with fields.

I need a button "duplicate" for each field.
When i click the button the field is duplicate.

And i need the code to display the value of each field in the front.






I can do that with plugins like "magic fields" or "simple fields". But i need to do that without plugin.

Answers (4)

2011-10-19

Luis Abarca answers:

Did you want something like the image attached ??

http://cl.ly/B5Yn


This is the form.php

http://pastebin.com/CTmPyGRc

And this is the code



add_action('add_meta_boxes', 'mycpt_meta_boxes');

function mycpt_meta_boxes()
{
add_meta_box( 'cpt_metabox-1', __( 'Extra fields' ),
'mycpt_meta_box_content', 'mycpt', 'normal', 'high');
}

// }}}
// {{{

function mycpt_meta_box_content()
{
global $post;

include 'form.php';
wp_nonce_field('mycpt_save', 'mycpt_nonce');
}


add_action('save_post', 'mycpt_save_postdata', 10, 2);

function mycpt_save_postdata( $post_id )
{
global $wpdb;

if ( !isset($_POST['mycpt_nonce']) ) {
return $post_id;
}

if ( !wp_verify_nonce( $_POST['mycpt_nonce'], 'mycpt_save' ) ) {
return $post_id;
}

if ( defined('DOING_AUTOSAVE') && DOING_AUTOSAVE ) {
return $post_id;
}

// Check permissions ....

/*
* Videos names/titles
*/
$videos_titulos = $_POST['video_title'];

// First is empty
array_shift($videos_titulos);

/*
* Video URLs
*/
$videos_urls = $_POST['video_url'];
// first is empty
array_shift($videos_urls);

// How many ?
$videos_count = sizeof($videos_titulos);

$videos = array();
$content = '';

for ($i = 0; $i < $videos_count; $i++) {
$title = $videos_titulos[$i];
$url = $videos_urls[$i];

// check if empty
if (!empty($titulo) && !empty($url)) {
$videos[] = array(
'title' => $title,
'url' => $url, 1
'image' => get_youtube_video_thumb($url) // custom function
);
}
}

// Serializamos videos en caso que existan, si no, eliminar el post meta
if ( sizeof($videos) > 0 ) {
$content = serialize($videos);
}

update_post_meta($post_id, 'ponente', $_POST['ponente']);
update_post_meta($post_id, 'videos', $content);
}


jevusi comments:

absolutely


Luis Abarca comments:

Check the code posted above and let me know if you have any issue


jevusi comments:

only the first field is update.
Te others are empty.

and the duplicate fields disappear


Luis Abarca comments:

OK, let me check whats wrong, this is the code used in the image attached


jevusi comments:

ok, tell me when it's ok


Luis Abarca comments:

Its working now amigo, check the updated code

form.php

http://pastebin.com/CTmPyGRc

functions.php or in a plugin

http://pastebin.com/cVwGt77L


jevusi comments:

i have this error message :
Notice: Undefined index: mycpt_nonce in /homez.144/xxx/demo/wp-content/themes/twentyten/functions.php on line 37

Warning: Cannot modify header information - headers already sent by (output started at /homez.144/xxx/demo/wp-content/themes/twentyten/functions.php:37) in /homez.144/xxx/demo/wp-includes/class-wp-ajax-response.php on line 129



the line 37 is
if ( !wp_verify_nonce( $_POST['mycpt_nonce'], 'mycpt_save' ) ) {return $post_id;}
if i comment this line i have this error message



Notice: Undefined index: video_title in /homez.144/xxx/demo/wp-content/themes/twentyten/functions.php on line 50

Warning: array_shift() [function.array-shift]: The argument should be an array in /homez.144/xxx/demo/wp-content/themes/twentyten/functions.php on line 53

Notice: Undefined index: video_url in /homez.144/xxx/demo/wp-content/themes/twentyten/functions.php on line 58

Warning: array_shift() [function.array-shift]: The argument should be an array in /homez.144/xxx/demo/wp-content/themes/twentyten/functions.php on line 60


jevusi comments:

i see in the recent events that "Luis Abarca posted an answer for: duplicate a field without plugin", but i don't see this response...
strange...


Luis Abarca comments:

I posted on pastebin.com, when i paste code here, it lostthe format, you are recieving the PHP notice because you setup PHP to show you all errors, the php notice its not important.

Change or add this code in your wp-config.php


define('WP_DEBUG', true);
error_reporting(E_ALL & ~E_NOTICE);
define('WP_DEBUG_LOG', true);
define('WP_DEBUG_DISPLAY', false);
@ini_set('display_errors', 0);



form.php

http://pastebin.com/CTmPyGRc

functions.php or in a plugin

http://pastebin.com/cVwGt77L


Luis Abarca comments:

I forgot to mention, if you configure your wp-config in that way, you will find all the debug info in the file wp-content/debug.log

if debug.log doesnt show up, create the file first and add write permissions to the server user with 644 or 664 is enough


jevusi comments:

perfect,
that's work
and how can i display the value of each duplicate fields please ?

2011-10-19

Sébastien | French WordpressDesigner answers:

my suggestion : use the plugin "magic fields"

2011-10-19

Jurre Hanema answers:

If you share the code you are currently using, I can take a look at this.


jevusi comments:

euh... so... your solution will be adapted only for this code ?

2011-10-19

Fahad Murtaza answers:

OK

Do you want a javascript solution to duplicate a custom field. What naming convention for duplicate field would you like?


jevusi comments:

yes, javascript solution... probably ajax in fact
naming convention ? I don't understand what is it