This weekend I started looking at the available plugins for reusable field groups. Testing and research turned into an entire weekend and at the end I had created my own version of a reusable field group field.
It works a bit differently than the others, it works by rebuilding field groups that include a reusable field group field ans local field groups which override the original field groups. This only works in ACF5, ACF4 does not support acf_local()
.
I have not completely tested it and would be interested in feedback or bug reports. I’ll also answer question. There’s not documentation yet, mostly because I think that the instructions when creating the field are pretty self explanatory.
https://github.com/Hube2/acf-reusable-field-group-field
This weekend I started looking at the available plugins for reusable field groups. Testing and research turned into an entire weekend and at the end I had created my own version of a reusable field group field.
It works a bit differently than the others, it works by rebuilding field groups that include a reusable field group field ans local field groups which override the original field groups. This only works in ACF5, ACF4 does not support acf_local()
.
I have not completely tested it and would be interested in feedback or bug reports. I’ll also answer question. There’s not documentation yet, mostly because I think that the instructions when creating the field are pretty self explanatory.
https://github.com/Hube2/acf-reusable-field-group-field
This weekend I started looking at the available plugins for reusable field groups. Testing and research turned into an entire weekend and at the end I had created my own version of a reusable field group field.
It works a bit differently than the others, it works by rebuilding field groups that include a reusable field group field ans local field groups which override the original field groups. This only works in ACF5, ACF4 does not support acf_local()
.
I have not completely tested it and would be interested in feedback or bug reports. I’ll also answer question. There’s not documentation yet, mostly because I think that the instructions when creating the field are pretty self explanatory.
https://github.com/Hube2/acf-reusable-field-group-field
I started looking into making a reusable field group field after my last comment. Testing and research turned into an entire weekend and at the end I had created my own version of a reusable field group field.
It only works with ACF5. It works by rebuilding field groups that include a reusable field group field ans local field groups which override the original field groups. The reason it only works with ACF5 is that acf_local()
is not available before 5.
I have not completely tested it and would be interested in feedback or bug reports. I’ll also answer question. There’s not documentation yet, mostly because I think that the instructions when creating the field are pretty self explanatory.
https://github.com/Hube2/acf-reusable-field-group-field
There is a plugin that was created by someone that is supposed to do this. I haven’t had the chance to try it out yet.
https://github.com/tybruffy/ACF-Reusable-Field-Group
+100.
I’ve had some success with using this: https://github.com/tybruffy/ACF-Reusable-Field-Group
+10 for having this functionality built in. I’ve been thinking of ways to do this for a while now. It’d be great to abstract out custom field groups and set them up for reuse later.
Many examples of this such as: a big select list that will need to be managed but used in multiple places on the site, complex groupings like for a button field where you need a text field paired with a page link or url and a select list of styles or colors…
I’ve tested out the https://github.com/mvpdesign/acf-reusable-field-group add-on field and am impressed thus far. Grabbing the data is a bit different in that it returns all the values in an array, but once I realized that it was easy to work with. But I think the interface in the back-end used here is the ideal way to do it. It keeps it all very simple.
I tend to use the second example, where the prefix matches the slug of the CPT I’m using them on. book_authour
etc. On options pages I use a prefix that indicates the type of option it’s for, for example site_options_field_name
. I have, in the past used a prefix that represented the client, but I stopped doing that when I started reusing field groups and needed to rename them all to match another client.
To be honest, I don’t think that there is a “standard WordPress practice” except that the top level fields should have a prefix and I try to make sure that my field names will not conflict with any of the more popular plugins, and I like using things that will be reusable.
@floflo91 thanks for the help! That one works well for the normal fields inside the Reusable field but not for a flexible field inside a Reusable field.
i tried to use print_r
<?php print_r(get_sub_field('modules_custom_reusable')); ?>
which outputted:
Array ( [modules_custom] => Array ( [0] => Array ( [acf_fc_layout] => module_sample [content_title] => text field inside flexible ) ) [content_title] => text field outside flexible )
So it is picking up the content of the flexible field i just don’t know how to pull them out separately and apply the layout fields. :/ Will try it more.
Here are the templates for the normal fields in case anyone will need them.
Inside a repeater:
<?php if( have_rows('repeater') ): ?>
<?php while( have_rows('repeater') ): the_row(); ?>
<?php
if (get_sub_field('reusable_field')) {
foreach (get_sub_field('reusable_field') as $p) {
echo $p;
the_sub_field('subfieldName');
the_sub_field('subFieldName2');
}
} ?>
<?php endwhile; ?>
<?php endif; ?>
inside a flexible field type:
<?php
if( have_rows('flexible') ):
while ( have_rows('flexible') ) : the_row();
if( get_row_layout() == 'layout_flexible' ): ?>
<?php
if (get_sub_field('reusable_field_group_')) {
foreach (get_sub_field('reusable_field_group') as $p) {
echo $p;
the_sub_field('subfieldName');
echo "<br>";
the_sub_field('subFieldName2');
}
} ?>
<?php elseif( get_row_layout() == 'other' ):
endif;
endwhile;
else :
endif;
?>
Test with get_sub_field function
if (get_sub_field('reusable_field_group_for_text')) {
foreach (get_sub_field('reusable_field_group_for_text') as $p) {
echo $p;
the_sub_field('subfieldName');
the_sub_field('subFieldName2');
}
}
@floflo91 hm.. couldn’t make it to work.
I also started from scratch and made a field group with just 2 text fields.
Made a second field group with the reusable group and called the first one.
Made 2 tests in the template:
Test A
<?php the_field('reusable_field_group_for_text'); ?>
Test B
<?php
if( have_rows('reusable_field_group_for_text') ):
while ( have_rows('reusable_field_group_for_text') ) : the_row();
echo "TEST";
endwhile; endif;
?>
TEST A output was and array of both text fields, although i would want to call them separately.
TEST B code didn’t work so maybe i just don’t know how to call the Reusable field type and it’s contents correctly :/ Even without any repeater/flexible.
Too bad the plugin docs don’t have any usage code for it.
@floflo91 thanks for the help!
I used the plugin you mentioned and the logic seems fine on the backend etc.
A Repeater field (so i can mix and match the flexible content)
A.1 Checkbox to choose to have flexible content A or B
A.2 Reusable field of Flexible content A (core modules for use on other websites)
A.3 Reusable field of Flexible content B (specific custom modules used for specific website).
The frontent/template part is a bit tricky.
Repeater field
– checkbox
— Reusable Field group (ex. modules_main_reusable)
— Flexible content field (ex. modules_main)
—- Field types for the flexible layout
How should i call the modules_main_reusable field type? like this:
if( have_rows('modules_main_reusable') ):
while ( have_rows('modules_main_reusable') ) : the_row();
And then add the flexible repeater code inside it?
<?php
if( have_rows('modules_main') ):
while ( have_rows('modules_main') ) : the_row(); ?>
<?php if( get_row_layout() == 'module_sample' ): ?>
<?php get_template_part('templates/modules-main/module', 'sample') ?>
<?php endif; ?>
<?php endwhile; endif; ?>
@darisi Please be aware that there are multiple Reusable Field Group plugins:
https://github.com/mvpdesign/acf-reusable-field-group
https://github.com/tybruffy/ACF-Reusable-Field-Group
I’m using the second, i don’t know how the first one works.
@timh Ah thanks, so maybe it just doesn’t work with the reusable field group being a flexible content.
I thought the reusable field group works as a “row” so you could add more html etc. to the template.
Hi,
First, when you use a ACF Reusable Field, you have to know that you have one more imbrication in your template, so you have to do a loop with the Reusable Field to get its childs fields.
Secondly, I don’t see where you call your ACF Reusable Field in your template, you have to call it : modules_main_reusable.
Third, I encourage you to use this fork of the plugin if you want to have multiples Reusable field of an ACF Group Field in a flexible field.
https://github.com/mvpdesign/acf-reusable-field-group
Hope it helps.
Hi Darisi,
I’m not sure what you mean exactly. I just downloaded and installed the plugin. This gives me an extra option in the Field Type dropdown: Reusable Field Group.
I’m using Advanced Custom Fields Pro 5.2.8 with the latest WordPress version, in case that makes a difference.
In my php templates, i just use the regular ACF functions like get_field() to retrieve the values from the Reusable Field Group fields.
I tried couple of things, found this plugin https://github.com/mvpdesign/acf-reusable-field-group which in a way would be possible to make it but it doesn’t seem to work (attached the fields structure).
and here is the code i’m running through the template:
<?php
// check if the repeater field has rows of data
if( have_rows('modules_all') ):
// loop through the rows of data
while ( have_rows('modules_all') ) : the_row(); ?>
<?php
$checkboxcustom = get_sub_field('is_custom_modules');
if($checkboxcustom) { // is yes ?>
CUSTOM
<?php
if( have_rows('modules_custom') ):
while ( have_rows('modules_custom') ) : the_row(); ?>
<?php if( get_row_layout() == 'module_sample' ): ?>
<?php get_template_part('templates/modules-custom/module', 'sample') ?>
<?php endif; ?>
<?php endwhile; ?>
<?php endif; ?>
<?php } else { ?>
MAIN
<?php
if( have_rows('modules_main') ):
while ( have_rows('modules_main') ) : the_row(); ?>
<?php if( get_row_layout() == 'module_main' ): ?>
<?php get_template_part('templates/modules-main/module', 'main') ?>
<?php endif; ?>
<?php endwhile; ?>
<?php endif; ?>
<?php } ?>
<?php endwhile;
else :
endif;
?>
IT seems to be working nicely for the Main fields but not the Custom one, as they are not beeing displayed.
I’ve used this plugin in a couple of projects: https://github.com/tybruffy/ACF-Reusable-Field-Group
Haven’t found any problems yet
There is a fork of the plugin that fix the issue of adding n number of instances in a flexible field :
That actually seems to be a fork of a different plugin than the one mentioned above (created by @beneverard). I haven’t tried it before but it looks interesting thank you, I will try it out on my next project (that requires it) and report back.
This is still a feature I desire greatly from ACF and would love to see it as part of the core functionality (or happy to pay for a premium add-on).
Hello,
There is a fork of the plugin that fix the issue of adding n number of instances in a flexible field :
https://github.com/mvpdesign/acf-reusable-field-group
cc : @Will @shanomurphy
Hi Daniel,
If I’m understanding you correctly, someone has implemented a plugin that does what you’re asking for. It’s called ACF Reusable Field Group: https://github.com/mvpdesign/acf-reusable-field-group
Let’s say you have 2 field groups you want to choose from: FG1 and FG2
Create a 3rd field group (let’s call it Repeater Group) that’s a repeater with a Reusable Field Group field in it. Add the Repeater Group to your pages.
Then, when filling out the page, you would add a row to the Repeater Group Field, select FG1 and fill it out. Add a new row, select FG2, and fill it out. Just watch out for ACF Inception, I think it might be possible to select the Repeater Group itself and create a field within a field within a field. 🙂
I found a solution in this thread:
http://support.advancedcustomfields.com/forums/topic/reusable-field-groups/
Here is the link to the GitHub page:
https://github.com/theideabureau/acf-reusable-group-field
Works perfectly.
Here’s the code I created.
This plugin works well and is very handy. However I’ve just run into one caveat – when using the Reusable Group Field within a Flexible Content Field you can only use it once per page.
If you try and use a Reusable Group in more than one row it doesn’t seem to store a separate database entry for each instance of the group.
This would be such a great feature to have as it would give ACF a really nice modular approach.
Here’s the code I created.
https://github.com/theideabureau/acf-reusable-group-field
Welcome to the Advanced Custom Fields community forum.
Browse through ideas, snippets of code, questions and answers between fellow ACF users
Helping others is a great way to earn karma, gain badges and help ACF development!
We use cookies to offer you a better browsing experience, analyze site traffic and personalize content. Read about how we use cookies and how you can control them in our Privacy Policy. If you continue to use this site, you consent to our use of cookies.