1

I am working on creating custom meta boxes for the back-end of WordPress.

I would like to add a metabox with several fields, in my case an image upload and a text input, but with a button that can duplicate both fields.

I've been following this tutorial and it works fine.

But since I want to duplicate multiple fields (as mentioned earlier, the media uploader and text input), the above tutorial does not quite fit. There is only 1 input text used.

Now I have found the following source code, which corresponds somewhat to my wishes:

https://github.com/tammyhart/Reusable-Custom-WordPress-Meta-Boxes

This makes it possible to have multiple cases in a repeatable field stop (this is what I want), only the code is far too extensive and is in a PHP class. Now I want this code into that of the tutorial, just try and get this to many trials for each other.

Now I wonder if anyone can help me with this. I've tried many things and work to date yet.

When I see the source code, I do not really need the piece and there will be more redundant than necessary.

I've also tried the simple case repeatable to copy / paste, but I get errors, including that some variable does not exist. (These are, as I see it also hell once declared nowhere ...)

Unfortunately gives these errors are, as I said, namely:

Notice: Undefined variable: repeatable_fields in on line 117 
Warning: Invalid argument supp song for foreach () in   
  customers/0/d/a/xxx/httpd.www/wordpress/wp-includes/functions.php on
  line 2638 
Warning: array_filter () Expects parameter 1 to be array,
  null Given in /file/ on line 118 
Notice: Uninitialized string offset: 0 in on
  line 138

Code can be viewed at: http://snippi.com/s/xpwucxg

How can I duplicate multiple fields?

8
  • Also here you can see the code from the tutorial, that I have coded: Code is available at: snippi.com/s/dz7nqo9
    – Casper
    Jan 5, 2013 at 20:45
  • 2
    I just write a comment, not an answer, but you should definitely take a look at the Advanced Custom Fields (advancedcustomfields.com) plugin. Seriously, this thing is awesome, i use it for every wordpress project i have. You have to pay for the repeater field addon, but the price is very low and you only have to buy it once and it does exactly what you want and of course a lot more. Just a suggestion:)
    – passatgt
    Jan 5, 2013 at 21:06
  • 1
    IMO, digging into all that code is a bit too much. I suggest you learn how to debug. Follow the lead of the error, this seems to be the line that is not working: $field_titles = array_filter( $field_titles );, from the snippi with id xpwucxg.
    – brasofilo
    Jan 5, 2013 at 21:48
  • @passatgt Yes I am familar with Advanced Custom Fields and have also the add-on repeater. But because I developing an plugin for WordPress, I can't get the image src and caption from it.. That's because I want to create my own. Or you have an solution for that, I like to hear from you.
    – Casper
    Jan 5, 2013 at 22:23
  • @brasofilo Thank you for your help. I will give it a try!
    – Casper
    Jan 5, 2013 at 22:24

2 Answers 2

2

I created and managed my own custom meta fields for years and I can tell you it was always a headache. I frequently came up against stumbling blocks like yours and ended up banging my fist against the wall. I've also worked on quite a few sites which had been flooded with plugins, sometimes so many that it soured me on plugins altogether and I resolved to use them only when absolutely critical to a project.

One of the only reliable plugins I came across was Advanced Custom Fields. I've used it on almost every project since first trying it out and it's truly expanded the possibilities of all my projects. It really organizes fields quite nicely and for $25 they also have a repeatable field add-on which I've become quite familiar with.

Give them a shot.

0

To add repeatable fields you have to create an array with, for example, text inputs:

array( // Repeatable & Sortable Text inputs
    'label' => 'Typical Day', // <label>
    'desc'  => '', // description
    'id'    => $prefix.'repeatable_typical_day', // field id and name
    'type'  => 'repeatable', // type of field
    'repeatable_fields' =>  array(
            array( // Text Input
                'repeatable_label'  => 'Title', // <label>
                'repeatable_desc'   => '', // description
                'repeatable_id' => $prefix.'program_act_title', // field id and name
                'repeatable_type'   => 'text' // type of field
            ),
            array( // Text Input
                'repeatable_label'  => 'Description', // <label>
                'repeatable_desc'   => '', // description
                'repeatable_id' => $prefix.'program_act_desc', // field id and name
                'repeatable_type'   => 'text' // type of field
            )

    )

)

This way you can add images, input text, etc...

------- Edit --------

Tammy has updated the code and fixed the issues.Latest version works. https://github.com/tammyhart/Reusable-Custom-WordPress-Meta-Boxes

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

Not the answer you're looking for? Browse other questions tagged or ask your own question.