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