-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
3 changed files
with
108 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,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 | ||
|
||
?> |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,39 @@ | ||
=== ACF Dynamic Choices === | ||
Contributors: grexican | ||
Tags: acf, dynamic, sql, query | ||
Requires at least: 3.0.1 | ||
Tested up to: 3.9.1 | ||
Stable tag: .5 | ||
License: MIT | ||
License URI: https://raw.githubusercontent.com/grexican/acf-dynamic-choices/master/LICENSE | ||
|
||
Advanced Custom Fields plugin to allow for dynamic choices (through SQL queries) | ||
|
||
== Description == | ||
|
||
If you've ever needed your ACF choices to be dynamically generated by a SQL query, this plugin will help you get the job done. | ||
|
||
You can mix and match static choices with dynamic, SQL-based choices by placing as many regular choices in the list as you'd like, followed by the special `%%QUERY%%` choice that will tell this plugin to load choices dynamically from the database. | ||
|
||
An example on how to list users, setting the `id` as the value, would look like this: | ||
|
||
`%%QUERY%% : SELECT user_nicename AS text, id AS value FROM wp_users` | ||
|
||
|
||
== Installation == | ||
|
||
1. Upload `acf-dynamic-choices` to the `/wp-content/plugins/` directory. Alternatively, install the `zip` through the WordPress Plugins installer. | ||
1. Activate the plugin through the 'Plugins' menu in WordPress | ||
1. Place a query in your ACF choices box in this form: `%%QUERY%% : SELECT field1 AS text, field2 AS value FROM ....` | ||
1. Browse to a Post or the Options Page where you just added the dynamic query and ensure that the choices are properly being populated. | ||
|
||
== Frequently Asked Questions == | ||
|
||
|
||
== Changelog == | ||
|
||
= .5 = | ||
* Initial public release | ||
|
||
|
||
== Arbitrary section == |