-
Notifications
You must be signed in to change notification settings - Fork 57
/
Copy pathacf-load-parent-theme-field-groups.php
64 lines (53 loc) · 1.88 KB
/
acf-load-parent-theme-field-groups.php
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
<?php
/* IMPORTANT NOTE
Since this was written AVF added the ability to add load points to load JSON from in addition to the theme
It would be better to add a json load point for the parent themes acf-json file if a child theme is active
*/
/*
Add this class to a parent theme that uses ACF5 Pro
When loaded from a parent theme it will load all of the
field groups in the acf-json theme folder of the parent theme
This lets you create themes using ACF and have the field groups
automagically added to the child theme
*/
new acf_load_parent_theme_field_groups();
class acf_load_parent_theme_field_groups {
public function __construct() {
add_action('acf/include_fields', array($this, 'include_fields'), 50);
} // end public function __construct
public function include_fields() {
$path = get_template_directory().'/acf-json';
if (!is_dir($path) ||
($files = scandir($path)) === false ||
!count($files)) {
return;
}
$groups = $this->get_acf_field_groups();
foreach ($files as $file) {
$file_path = $path.'/'.$file;
if (is_dir($file_path) || !preg_match('/\.json$/', $file)) {
continue;
}
$group_key = preg_replace('/\.json$/', '', $file);
if (!isset($groups[$group_key]) &&
($json = file_get_contents($file_path)) !== false &&
($field_group = json_decode($json, true)) !== NULL) {
acf_add_local_field_group($field_group);
}
}
// need to delete the ACF cache
wp_cache_delete('get_field_groups', 'acf');
} // end public function include_fields
private function get_acf_field_groups() {
$groups = array();
$acf_groups = acf_get_field_groups();
if (!count($acf_groups)) {
return;
}
foreach ($acf_groups as $group) {
$groups[$group['key']] = $group['key'];
}
return $groups;
} // end private function get_acf_field_groups
} // end class acf_load_theme_groups
?>