-
Notifications
You must be signed in to change notification settings - Fork 0
/
acf-dynamic-choices.php
67 lines (52 loc) · 1.56 KB
/
acf-dynamic-choices.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
65
66
67
<?php
/**
* Plugin Name: Advanced Custom Fields Dynamic Choices
* Plugin URI: http://steppingback.com/project/acf-dynamic-choices/
* Description: Allows for dynamic choices (through SQL Queries) in ACF
* Version: .5
* Stable tag: .5
* Author: Eli Gassert
* Author URI: http://steppingback.com
* License: MIT
*/
global $acf_dynamic_choices_needs_values;
$acf_dynamic_choices_needs_values = false;
// I'm sure there's an ACF way to know if you're on the "setup" page vs the "in action" page, but I wasn't finding it, so I sniff it myself.
function acf_dynamic_choices_acf_admin_head()
{
global $acf_dynamic_choices_needs_values;
$acf_dynamic_choices_needs_values = true;
}
add_filter('acf/input/admin_head', 'acf_dynamic_choices_acf_admin_head');
function acf_dynamic_choices_acf_load_field($field)
{
global $acf_dynamic_choices_needs_values;
global $wpdb;
if(!$acf_dynamic_choices_needs_values) return $field;
$choices = $field['choices'];
$newChoices = array();
foreach($choices as $key => $choice)
{
if(strtolower($key) == '%%query%%')
{
$results = $wpdb->get_results($choice);// or die(mysql_error());
foreach($results as $r)
{
$newChoices[$r->value] = $r->text;
}
}
else
{
$newChoices[$key] = $choice;
}
}
$field['choices'] = $newChoices;
return $field;
}
add_filter('acf/load_field/type=select', 'acf_dynamic_choices_acf_load_field');
function action_function_name( $field ) {
echo '<pre>Some extra HTML</pre>';
}
add_filter( 'acf/render_field', 'action_function_name', 10, 1 );
// END ACF QUERY SELECT PLUGIN
?>