logo
Ask your WordPress questions! Pay money and get answers fast! (more info)

Warning: Please do not give out any FTP or ssh credentials to anyone, unless you trust them completely. Giving out login details is dangerous.

If the asker does not get an answer then they have 10 days to request a refund.

$50
duplicate a field without plugin

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.

This question has been answered.

jevusi | 10/19/11 at 12:39pm Edit


(4) Possible Answers Submitted...

See a chronological view of answers?

Warning: Please do not give out any FTP or ssh credentials to anyone, unless you trust them completely. Giving out login details is dangerous.

  • avatar
    Last edited:
    10/22/11
    10:36am
    S├ębastien | French WordpressDesigner says:

    my suggestion : use the plugin "magic fields"

    Previous versions of this answer: 10/22/11 at 10:36am

  • avatar
    Last edited:
    10/19/11
    12:45pm
    Jurre Hanema says:

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

    • 10/19/11 1:35pm

      jevusi says:

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

  • avatar
    Last edited:
    10/19/11
    1:52pm
    Luis Abarca says:

    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);
    }

    attachment image View Attachment

    Previous versions of this answer: 10/19/11 at 12:48pm | 10/19/11 at 1:52pm

    • 10/19/11 1:32pm

      jevusi says:

      absolutely

    • 10/19/11 2:19pm

      Luis Abarca says:

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

    • 10/20/11 6:48am

      jevusi says:

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

      and the duplicate fields disappear

    • 10/20/11 7:02am

      Luis Abarca says:

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

    • 10/20/11 8:02am

      jevusi says:

      ok, tell me when it's ok

    • 10/20/11 8:41am

      Luis Abarca says:

      Its working now amigo, check the updated code

      form.php

      http://pastebin.com/CTmPyGRc

      functions.php or in a plugin

      http://pastebin.com/cVwGt77L

    • 10/22/11 10:04am

      jevusi says:

      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

    • 10/22/11 10:26am

      jevusi says:

      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...

    • 10/22/11 11:41am

      Luis Abarca says:

      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

    • 10/22/11 11:49am

      Luis Abarca says:

      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

    • 10/22/11 12:47pm

      jevusi says:

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

  • avatar
    Last edited:
    10/19/11
    1:20pm
    Fahd Murtaza says:

    OK

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

    • 10/19/11 1:33pm

      jevusi says:

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

This question has expired.



Luis Abarca had additional discourse to offer.

jevusi, Linda voted on this question.



Current status of this question: Completed



Warning: Please do not give out any FTP or ssh credentials to anyone, unless you trust them completely. Giving out login details is dangerous.

If the asker does not get an answer then they have 10 days to request a refund.