├── .gitattributes ├── .gitignore ├── LICENSE ├── README.md ├── languages └── acf-taxonomy-field.pot ├── readme.txt ├── screenshot-1.png ├── screenshot-2.png ├── screenshot-3.png └── taxonomy-field.php /.gitattributes: -------------------------------------------------------------------------------- 1 | * text=auto 2 | 3 | #Force as text files to normalize line endings 4 | *.css text 5 | *.js text 6 | *.php text diff=php whitespace=strip 7 | *.md text 8 | 9 | #Force as binary files 10 | *.png binary 11 | 12 | -------------------------------------------------------------------------------- /.gitignore: -------------------------------------------------------------------------------- 1 | *.log 2 | .svn 3 | -------------------------------------------------------------------------------- /LICENSE: -------------------------------------------------------------------------------- 1 | Copyright (c) 2012, CAMPUS CRUSADE FOR CHRIST 2 | All rights reserved. 3 | 4 | Redistribution and use in source and binary forms, with or without modification, 5 | are permitted provided that the following conditions are met: 6 | 7 | Redistributions of source code must retain the above copyright notice, this 8 | list of conditions and the following disclaimer. 9 | Redistributions in binary form must reproduce the above copyright notice, 10 | this list of conditions and the following disclaimer in the documentation 11 | and/or other materials provided with the distribution. 12 | Neither the name of CAMPUS CRUSADE FOR CHRIST nor the names of its 13 | contributors may be used to endorse or promote products derived from this 14 | software without specific prior written permission. 15 | 16 | THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND 17 | ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED 18 | WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. 19 | IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, 20 | INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, 21 | BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, 22 | DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF 23 | LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE 24 | OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED 25 | OF THE POSSIBILITY OF SUCH DAMAGE. 26 | -------------------------------------------------------------------------------- /README.md: -------------------------------------------------------------------------------- 1 | **DEPRECATED** 2 | ============== 3 | This plugin is no longer maintained. The functionality is provided by core Advanced Custom Fields. 4 | 5 | Advanced Custom Fields - Taxonomy Field add-on 6 | ============================================== 7 | 8 | Adds a Taxonomy Field to Advanced Custom Fields. Select one or more taxonomy terms and assign them to the post. 9 | 10 | Description 11 | ----------- 12 | 13 | This is an add-on for the [Advanced Custom Fields](http://wordpress.org/extend/plugins/advanced-custom-fields/) 14 | WordPress plugin and will not provide any functionality to WordPress unless Advanced Custom Fields is installed 15 | and activated. 16 | 17 | The taxonomy field provides a select, multi-select or checkboxes of taxonomy terms (categories, tags, custom taxonomies...) 18 | and the ability to map the selected terms to the post. The post type must support the taxonomy for the mapping to work. 19 | The taxonomy field currently does not provide the ability to add new terms to a taxonomy. The return type of the `get_value()` 20 | api can be changed in the field settings. 21 | 22 | ### Source Repository on GitHub 23 | https://github.com/GCX/acf-taxonomy-field 24 | 25 | ### Bugs, Questions or Suggestions 26 | https://github.com/GCX/acf-taxonomy-field/issues 27 | 28 | Installation 29 | ------------ 30 | 31 | The Taxonomy Field plugin can be used as a WordPress plugin or included in other plugins or themes. 32 | There is no need to call the Advanced Custom Fields `register_field()` method for this field. 33 | 34 | * WordPress plugin 35 | 1. Download the plugin and extract it to `/wp-content/plugins/` directory. 36 | 2. Activate the plugin through the `Plugins` menu in WordPress. 37 | * Added to Theme or Plugin 38 | 1. Download the plugin and extract it to your theme or plugin directory. 39 | 2. Include the `taxonomy-field.php` file in you theme's `functions.php` or plugin file. 40 | `include_once( rtrim( dirname( __FILE__ ), '/' ) . '/acf-taxonomy-field/taxonomy-field.php' );` 41 | 42 | Frequently Asked Questions 43 | -------------------------- 44 | 45 | ### I've activated the plugin, but nothing happens! 46 | 47 | Make sure you have [Advanced Custom Fields](http://wordpress.org/extend/plugins/advanced-custom-fields/) installed and 48 | activated. This is not a standalone plugin for WordPress, it only adds additional functionality to Advanced Custom Fields. 49 | 50 | Todo 51 | ---- 52 | * Add ability to add new terms to a taxonomy 53 | * Add more term selection methods (token input). 54 | -------------------------------------------------------------------------------- /languages/acf-taxonomy-field.pot: -------------------------------------------------------------------------------- 1 | # Copyright (C) 2012 2 | # This file is distributed under the same license as the package. 3 | msgid "" 4 | msgstr "" 5 | "Project-Id-Version: \n" 6 | "Report-Msgid-Bugs-To: http://wordpress.org/tag/advanced-custom-fields-" 7 | "taxonomy-field-add-on\n" 8 | "POT-Creation-Date: 2012-08-08 20:13:28+00:00\n" 9 | "MIME-Version: 1.0\n" 10 | "Content-Type: text/plain; charset=UTF-8\n" 11 | "Content-Transfer-Encoding: 8bit\n" 12 | "PO-Revision-Date: 2012-MO-DA HO:MI+ZONE\n" 13 | "Last-Translator: FULL NAME \n" 14 | "Language-Team: LANGUAGE \n" 15 | 16 | #: taxonomy-field.php:219 taxonomy-field.php:311 17 | msgid "Taxonomy" 18 | msgstr "" 19 | 20 | #: taxonomy-field.php:312 21 | msgid "Select the taxonomy to display." 22 | msgstr "" 23 | 24 | #: taxonomy-field.php:327 25 | msgid "Input Method" 26 | msgstr "" 27 | 28 | #: taxonomy-field.php:338 29 | msgid "Select" 30 | msgstr "" 31 | 32 | #: taxonomy-field.php:339 33 | msgid "Multi-Select" 34 | msgstr "" 35 | 36 | #: taxonomy-field.php:340 37 | msgid "Hierarchical Checkboxes" 38 | msgstr "" 39 | 40 | #: taxonomy-field.php:348 41 | msgid "Set Post Terms" 42 | msgstr "" 43 | 44 | #: taxonomy-field.php:349 45 | msgid "" 46 | "Add the selected term(s) to the post. The current post must support the " 47 | "selected taxonomy for this to work." 48 | msgstr "" 49 | 50 | #: taxonomy-field.php:359 51 | msgid "Not Set" 52 | msgstr "" 53 | 54 | #: taxonomy-field.php:360 55 | msgid "Append Terms" 56 | msgstr "" 57 | 58 | #: taxonomy-field.php:361 59 | msgid "Override Terms" 60 | msgstr "" 61 | 62 | #: taxonomy-field.php:369 63 | msgid "Use Post Terms for Field Value" 64 | msgstr "" 65 | 66 | #: taxonomy-field.php:370 67 | msgid "Pre-populate the field value using the terms assigned to the post." 68 | msgstr "" 69 | 70 | #: taxonomy-field.php:378 71 | msgid "Pre-populate the field value" 72 | msgstr "" 73 | 74 | #: taxonomy-field.php:381 75 | msgid "" 76 | "Setting this option will cause the field value as well as the get_value() " 77 | "api call to use the terms assigned to the post as the value of the field. " 78 | "Enabling this option when using a Repeater or the same taxonomy multiple " 79 | "times in an ACF group will cause all the taxonomy fields have the same " 80 | "values, regardless of the values selected." 81 | msgstr "" 82 | 83 | #: taxonomy-field.php:386 84 | msgid "Return Value" 85 | msgstr "" 86 | 87 | #: taxonomy-field.php:387 88 | msgid "Choose the field value type returned by API calls." 89 | msgstr "" 90 | 91 | #: taxonomy-field.php:397 92 | msgid "Links" 93 | msgstr "" 94 | 95 | #: taxonomy-field.php:398 96 | msgid "Objects" 97 | msgstr "" 98 | 99 | #: taxonomy-field.php:399 100 | msgid "Term IDs" 101 | msgstr "" 102 | 103 | #: taxonomy-field.php:407 104 | msgid "Multi-Select Size" 105 | msgstr "" 106 | 107 | #: taxonomy-field.php:408 108 | msgid "The number of terms to show at once in a multi-select." 109 | msgstr "" 110 | -------------------------------------------------------------------------------- /readme.txt: -------------------------------------------------------------------------------- 1 | === Advanced Custom Fields - Taxonomy Field add-on === 2 | Contributors: Omicron7 3 | Tags: acf, acf add-on, taxonomy, custom field, taxonomy field 4 | Requires at least: 3.2 5 | Tested up to: 3.4.1 6 | Stable tag: 1.4 7 | 8 | Adds a Taxonomy Field to Advanced Custom Fields. Select one or more taxonomy terms and assign them to the post. 9 | 10 | == Description == 11 | 12 | This is an add-on for the [Advanced Custom Fields](http://wordpress.org/extend/plugins/advanced-custom-fields/) 13 | WordPress plugin and will not provide any functionality to WordPress unless Advanced Custom Fields is installed 14 | and activated. 15 | 16 | The taxonomy field provides a select, multi-select or checkboxes of taxonomy terms (categories, tags, custom taxonomies...) 17 | and the ability to map the selected terms to the post. The post type must support the taxonomy for the mapping to work. 18 | The taxonomy field currently does not provide the ability to add new terms to a taxonomy. The return type of the `get_value()` 19 | api can be changed in the field settings. 20 | 21 | = Source Repository on GitHub = 22 | https://github.com/GCX/acf-taxonomy-field 23 | 24 | = Bugs, Questions or Suggestions = 25 | https://github.com/GCX/acf-taxonomy-field/issues 26 | 27 | = Todo = 28 | * Add ability to add new terms to a taxonomy 29 | * Add more term selection methods (checkboxes, token input). 30 | 31 | == Installation == 32 | 33 | The Taxonomy Field plugin can be used as WordPress plugin or included in other plugins or themes. 34 | There is no need to call the Advanced Custom Fields `register_field()` method for this field. 35 | 36 | * WordPress plugin 37 | 1. Download the plugin and extract it to `/wp-content/plugins/` directory. 38 | 2. Activate the plugin through the `Plugins` menu in WordPress. 39 | * Added to Theme or Plugin 40 | 1. Download the plugin and extract it to your theme or plugin directory. 41 | 2. Include the `taxonomy-field.php` file in you theme's `functions.php` or plugin file. 42 | `include_once( rtrim( dirname( __FILE__ ), '/' ) . '/acf-taxonomy-field/taxonomy-field.php' );` 43 | 44 | == Frequently Asked Questions == 45 | 46 | = I've activated the plugin, but nothing happens! = 47 | 48 | Make sure you have [Advanced Custom Fields](http://wordpress.org/extend/plugins/advanced-custom-fields/) installed and 49 | activated. This is not a standalone plugin for WordPress, it only adds additional functionality to Advanced Custom Fields. 50 | 51 | == Screenshots == 52 | 53 | 1. Taxonomy Field. 54 | 2. Select the taxonomy to show. 55 | 3. Adding a term to a page. 56 | 57 | == Changelog == 58 | 59 | = 1.4 = 60 | * Added option to use the terms assigned to the post as the field value. 61 | 62 | = 1.3.1 = 63 | * Fixed undefined class constant FIELD_NAME issue. Props dmeehan1968 64 | 65 | = 1.3 = 66 | * Fixed issue where ACF value was incorrect when setting terms on a post. Props dmeehan1968 67 | * Fixed issue with repeater returning the same value for every row when Set Post Terms is enabled. Props XedinUnknown 68 | * Added indentation to hierarchical taxonomies in the select or multiselect. Props dmeehan1968 69 | * Added the ability to append or override terms on a post. Props dmeehan1968 70 | * Removed unused code and improved coding practices by using constants. 71 | * Updated localizations 72 | 73 | = 1.2 = 74 | * Fixed issue Taxonomy Field not working in a Repeater Field. Props markSal 75 | * Fixed a bug which caused wrong options to be shown in the metabox. Props FunkyM 76 | * Fixed an additional issue with URL generation on Windows hosts. 77 | * Added hierarchical checkboxes input type similar to builting WordPress taxonomy chooser. Props FunkyM 78 | * Added option to choose link, object or ID as return value for field API calls. Props FunkyM 79 | * Updated localizations 80 | 81 | = 1.1.1 = 82 | * Fixed issue with path and URI generation on Windows hosts. 83 | * Fixed missing and invalid argument notices for get_field() api call. Thanks Rahe for the patch. 84 | 85 | = 1.1 = 86 | * Improved get_value API call. Using `get_value()` now returns a string of term links. Similar to WordPress `get_the_term_list()`. 87 | * Fixed an issue with terms not being pre-selected when editing a post. 88 | * Fixed a localization variable name typo. 89 | 90 | = 1.0 = 91 | * Initial Release -------------------------------------------------------------------------------- /screenshot-1.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/GCX/acf-taxonomy-field/126d42df08f20edb6ae1d65a8730bcff041374dc/screenshot-1.png -------------------------------------------------------------------------------- /screenshot-2.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/GCX/acf-taxonomy-field/126d42df08f20edb6ae1d65a8730bcff041374dc/screenshot-2.png -------------------------------------------------------------------------------- /screenshot-3.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/GCX/acf-taxonomy-field/126d42df08f20edb6ae1d65a8730bcff041374dc/screenshot-3.png -------------------------------------------------------------------------------- /taxonomy-field.php: -------------------------------------------------------------------------------- 1 | 14 | 43 | 51 | * @version 1.4 52 | */ 53 | class ACF_Taxonomy_Field extends acf_Field { 54 | 55 | /** 56 | * Field name 57 | * @var string 58 | */ 59 | const FIELD_NAME = 'name'; 60 | 61 | /** 62 | * Field class 63 | * @var string 64 | */ 65 | const FIELD_CLASS = 'class'; 66 | 67 | /** 68 | * Field value 69 | * @var string 70 | */ 71 | const FIELD_VALUE = 'value'; 72 | 73 | /** 74 | * Field taxonomy type 75 | * @var string 76 | */ 77 | const FIELD_TAXONOMY = 'taxonomy'; 78 | 79 | /** 80 | * Field input type 81 | * @var string 82 | */ 83 | const FIELD_INPUT_TYPE = 'input_type'; 84 | 85 | /** 86 | * Field input size 87 | * @var string 88 | */ 89 | const FIELD_INPUT_SIZE = 'input_size'; 90 | 91 | /** 92 | * Field set post terms 93 | * @var string 94 | */ 95 | const FIELD_SET_TERMS = 'set_post_terms'; 96 | 97 | /** 98 | * Field return value type 99 | * @var string 100 | */ 101 | const FIELD_RETURN_TYPE = 'return_value_type'; 102 | 103 | /** 104 | * Field use post terms for value 105 | * @var string 106 | */ 107 | const FIELD_USE_TERMS = 'use_post_terms'; 108 | 109 | /** 110 | * Input Type select 111 | * @var string 112 | */ 113 | const INPUT_TYPE_SELECT = 'select'; 114 | 115 | /** 116 | * Input Type multiselect 117 | * @var string 118 | */ 119 | const INPUT_TYPE_MULTISELECT = 'multiselect'; 120 | 121 | /** 122 | * Input Type hierarchical checkboxes 123 | * @var string 124 | */ 125 | const INPUT_TYPE_CHECKBOX = 'hierarchical'; 126 | 127 | /** 128 | * Set Post Terms not set 129 | * @var string 130 | */ 131 | const SET_TERMS_NOT_SET = 'not_set'; 132 | 133 | /** 134 | * Set Post Terms append 135 | * @var string 136 | */ 137 | const SET_TERMS_APPEND = 'append'; 138 | 139 | /** 140 | * Set Post Terms override 141 | * @var string 142 | */ 143 | const SET_TERMS_OVERRIDE = 'override'; 144 | 145 | /** 146 | * Return Value Type IDs 147 | * @var string 148 | */ 149 | const RETURN_TYPE_ID = 'id'; 150 | 151 | /** 152 | * Return Value Type objects 153 | * @var string 154 | */ 155 | const RETURN_TYPE_OBJECT = 'object'; 156 | 157 | /** 158 | * Return Value Type links 159 | * @var string 160 | */ 161 | const RETURN_TYPE_LINK = 'link'; 162 | 163 | /** 164 | * Base directory 165 | * @var string 166 | */ 167 | private $base_dir; 168 | 169 | /** 170 | * Relative Uri from the WordPress ABSPATH constant 171 | * @var string 172 | */ 173 | private $base_uri_rel; 174 | 175 | /** 176 | * Absolute Uri 177 | * 178 | * This is used to create urls to CSS and JavaScript files. 179 | * @var string 180 | */ 181 | private $base_uri_abs; 182 | 183 | /** 184 | * WordPress Localization Text Domain 185 | * 186 | * The textdomain for the field is controlled by the helper class. 187 | * @var string 188 | */ 189 | private $l10n_domain; 190 | 191 | /** 192 | * Class Constructor - Instantiates a new Taxonomy Field 193 | * @param Acf $parent Parent Acf class 194 | */ 195 | public function __construct( $parent ) { 196 | //Call parent constructor 197 | parent::__construct( $parent ); 198 | 199 | //Get the textdomain from the Helper class 200 | $this->l10n_domain = ACF_Taxonomy_Field_Helper::L10N_DOMAIN; 201 | 202 | //Base directory of this field 203 | $this->base_dir = rtrim( dirname( realpath( __FILE__ ) ), DIRECTORY_SEPARATOR ); 204 | 205 | //Build the base relative uri by searching backwards until we encounter the wordpress ABSPATH 206 | //This may not work if the $base_dir contains a symlink outside of the WordPress ABSPATH 207 | $root = array_pop( explode( DIRECTORY_SEPARATOR, rtrim( realpath( ABSPATH ), '/' ) ) ); 208 | $path_parts = explode( DIRECTORY_SEPARATOR, $this->base_dir ); 209 | $parts = array(); 210 | while( $part = array_pop( $path_parts ) ) { 211 | if( $part == $root ) 212 | break; 213 | array_unshift( $parts, $part ); 214 | } 215 | $this->base_uri_rel = '/' . implode( '/', $parts ); 216 | $this->base_uri_abs = get_site_url( null, $this->base_uri_rel ); 217 | 218 | $this->name = 'taxonomy-field'; 219 | $this->title = __( 'Taxonomy', $this->l10n_domain ); 220 | } 221 | 222 | /** 223 | * Populates the fields array with defaults for this field type 224 | * 225 | * @param array $field 226 | * @return array 227 | */ 228 | private function set_field_defaults( &$field ) { 229 | $field[ self::FIELD_TAXONOMY ] = ( array_key_exists( self::FIELD_TAXONOMY, $field ) && isset( $field[ self::FIELD_TAXONOMY ] ) ) ? $field[ self::FIELD_TAXONOMY ] : 'category'; 230 | $field[ self::FIELD_INPUT_TYPE ] = ( array_key_exists( self::FIELD_INPUT_TYPE, $field ) && isset( $field[ self::FIELD_INPUT_TYPE ] ) ) ? $field[ self::FIELD_INPUT_TYPE ] : self::INPUT_TYPE_SELECT; 231 | $field[ self::FIELD_INPUT_SIZE ] = ( array_key_exists( self::FIELD_INPUT_SIZE, $field ) && isset( $field[ self::FIELD_INPUT_SIZE ] ) ) ? (int) $field[ self::FIELD_INPUT_SIZE ] : 5; 232 | $field[ self::FIELD_USE_TERMS ] = ( array_key_exists( self::FIELD_USE_TERMS, $field ) && isset( $field[ self::FIELD_USE_TERMS ] ) ) ? (int) $field[ self::FIELD_USE_TERMS ] : 0; //default false 233 | 234 | $field[ self::FIELD_SET_TERMS ] = ( array_key_exists( self::FIELD_SET_TERMS, $field ) && isset( $field[ self::FIELD_SET_TERMS ] ) ) ? $field[ self::FIELD_SET_TERMS ] : self::SET_TERMS_NOT_SET; 235 | if( $field[ self::FIELD_SET_TERMS ] == '1' ) $field[ self::FIELD_SET_TERMS ] = self::SET_TERMS_OVERRIDE; 236 | elseif( $field[ self::FIELD_SET_TERMS ] == '0' ) $field[ self::FIELD_SET_TERMS ] = self::SET_TERMS_NOT_SET; 237 | 238 | $field[ self::FIELD_RETURN_TYPE ] = isset( $field[ self::FIELD_RETURN_TYPE ] ) ? $field[ self::FIELD_RETURN_TYPE ] : self::RETURN_TYPE_LINK; 239 | return $field; 240 | } 241 | 242 | /** 243 | * Creates the taxonomy field for inside post metaboxes 244 | * 245 | * @see acf_Field::create_field() 246 | */ 247 | public function create_field( $field ) { 248 | $this->set_field_defaults( $field ); 249 | 250 | $field[ self::FIELD_VALUE ] = is_array( $field[ self::FIELD_VALUE ] ) ? $field[ self::FIELD_VALUE ] : array(); 251 | 252 | if( in_array( $field[ self::FIELD_INPUT_TYPE ], array( self::INPUT_TYPE_SELECT, self::INPUT_TYPE_MULTISELECT ) ) ) : 253 | ?> 254 | 265 | 269 |
270 |
271 | "; 274 | ?> 275 |
    276 | $name, 279 | 'checked_ontop' => false, 280 | 'selected_cats' => $field[ self::FIELD_VALUE ], 281 | 'taxonomy' => $field[ self::FIELD_TAXONOMY ], 282 | 'walker' => new ACF_Walker_Taxonomy_Field_Checklist($field) 283 | ) ); 284 | ?> 285 |
286 |
287 |
288 | set_field_defaults( $field ); 301 | 302 | $taxonomies = get_taxonomies( array(), 'objects' ); 303 | ksort( $taxonomies ); 304 | $tax_choices = array(); 305 | foreach( $taxonomies as $tax ) 306 | $tax_choices[ $tax->name ] = $tax->label; 307 | 308 | ?> 309 | 310 | 311 | 312 |

l10n_domain ); ?>

313 | 314 | 315 | parent->create_field( array( 317 | 'type' => 'select', 318 | 'name' => "fields[{$key}][" . self::FIELD_TAXONOMY . "]", 319 | 'value' => $field[ self::FIELD_TAXONOMY ], 320 | 'choices' => $tax_choices, 321 | ) ); 322 | ?> 323 | 324 | 325 | 326 | 327 | 328 |

l10n_domain ); ?>

329 | 330 | 331 | parent->create_field( array( 333 | 'type' => 'select', 334 | 'name' => "fields[{$key}][" . self::FIELD_INPUT_TYPE . "]", 335 | 'value' => $field[ self::FIELD_INPUT_TYPE ], 336 | 'class' => 'taxonomy_input_type', 337 | 'choices' => array( 338 | self::INPUT_TYPE_SELECT => __( 'Select', $this->l10n_domain ), 339 | self::INPUT_TYPE_MULTISELECT => __( 'Multi-Select', $this->l10n_domain ), 340 | self::INPUT_TYPE_CHECKBOX => __( 'Hierarchical Checkboxes', $this->l10n_domain ), 341 | ), 342 | ) ); 343 | ?> 344 | 345 | 346 | 347 | 348 | 349 |

l10n_domain ); ?>

350 | 351 | 352 | parent->create_field( array( 354 | 'type' => 'radio', 355 | 'name' => 'fields[' . $key . '][' . self::FIELD_SET_TERMS . ']', 356 | 'value' => $field[ self::FIELD_SET_TERMS ], 357 | 'layout' => 'horizontal', 358 | 'choices' => array( 359 | self::SET_TERMS_NOT_SET => __( 'Not Set', $this->l10n_domain), 360 | self::SET_TERMS_APPEND => __( 'Append Terms', $this->l10n_domain ), 361 | self::SET_TERMS_OVERRIDE => __( 'Override Terms', $this->l10n_domain ), 362 | ) 363 | ) ); 364 | ?> 365 | 366 | 367 | 368 | 369 | 370 |

l10n_domain ); ?>

371 | 372 | 373 | parent->create_field( array( 375 | 'type' => 'true_false', 376 | 'name' => 'fields[' . $key . '][' . self::FIELD_USE_TERMS . ']', 377 | 'value' => $field[ self::FIELD_USE_TERMS ], 378 | 'message' => __( 'Pre-populate the field value', $this->l10n_domain ), 379 | ) ); 380 | ?> 381 |

l10n_domain ); ?>

382 | 383 | 384 | 385 | 386 | 387 |

l10n_domain ); ?>

388 | 389 | 390 | parent->create_field(array( 392 | 'type' => 'radio', 393 | 'name' => 'fields[' . $key . '][' . self::FIELD_RETURN_TYPE . ']', 394 | 'value' => $field[ self::FIELD_RETURN_TYPE ], 395 | 'layout' => 'horizontal', 396 | 'choices' => array( 397 | self::RETURN_TYPE_LINK => __( 'Links', $this->l10n_domain), 398 | self::RETURN_TYPE_OBJECT => __( 'Objects', $this->l10n_domain ), 399 | self::RETURN_TYPE_ID => __( 'Term IDs', $this->l10n_domain ), 400 | ) 401 | ) ); 402 | ?> 403 | 404 | 405 | 406 | 407 | 408 |

l10n_domain ); ?>

409 | 410 | 411 | parent->create_field( array( 413 | 'type' => 'select', 414 | 'name' => "fields[{$key}][" . self::FIELD_INPUT_SIZE . "]", 415 | 'value' => $field[ self::FIELD_INPUT_SIZE ], 416 | 'choices' => array_combine( range( 3, 15, 2 ), range( 3, 15, 2 ) ), 417 | ) ); 418 | ?> 419 | 420 | 421 | set_field_defaults( $field ); 430 | 431 | if( in_array( $field[ self::FIELD_SET_TERMS ], array( self::SET_TERMS_APPEND, self::SET_TERMS_OVERRIDE ) ) ) { 432 | $terms = array(); 433 | foreach( (array) $value as $item ) { 434 | if( intval( $item ) > 0 ) 435 | $terms[] = intval( $item ); 436 | else 437 | $terms[] = strval( $item ); 438 | } 439 | wp_set_object_terms( $post_id, $terms, $field[ self::FIELD_TAXONOMY ], $field[ self::FIELD_SET_TERMS ] == self::SET_TERMS_APPEND ); 440 | } 441 | 442 | parent::update_value( $post_id, $field, $value ); 443 | } 444 | 445 | /** 446 | * Returns the values of the field 447 | * 448 | * @see acf_Field::get_value() 449 | * @param int $post_id 450 | * @param array $field 451 | * @return array 452 | */ 453 | public function get_value( $post_id, $field ) { 454 | $value = ( $field[ self::FIELD_USE_TERMS ] ) ? 455 | wp_get_object_terms( $post_id, $field[ self::FIELD_TAXONOMY ], array( 'fields' => 'ids' ) ) : 456 | parent::get_value( $post_id, $field ); 457 | $value = is_array( $value ) ? $value : array(); 458 | return $value; 459 | } 460 | 461 | /** 462 | * Returns the value of the field for the advanced custom fields API 463 | * 464 | * @see acf_Field::get_value_for_api() 465 | * @param int $post_id 466 | * @param array $field 467 | * @return string 468 | */ 469 | public function get_value_for_api( $post_id, $field ) { 470 | $this->set_field_defaults( $field ); 471 | 472 | $value = parent::get_value_for_api( $post_id, $field ); 473 | $value = is_array( $value ) ? $value : array(); 474 | 475 | $terms = array(); 476 | foreach( $value as $term_id ) { 477 | $term_id = intval( $term_id ); 478 | switch( $field[ self::FIELD_RETURN_TYPE ] ) { 479 | case self::RETURN_TYPE_ID: 480 | $terms[] = $term_id; 481 | break; 482 | case self::RETURN_TYPE_OBJECT: 483 | $terms[] = get_term( $term_id, $field[ self::FIELD_TAXONOMY ] ); 484 | break; 485 | case self::RETURN_TYPE_LINK: 486 | $term = get_term( $term_id, $field[ self::FIELD_TAXONOMY ] ); 487 | $terms[] = sprintf( 488 | '', 489 | esc_attr( get_term_link( $term, $field[ self::FIELD_TAXONOMY ] ) ), 490 | esc_html( $term->name ) 491 | ); 492 | break; 493 | } 494 | } 495 | 496 | switch( $field[ self::FIELD_RETURN_TYPE ] ) { 497 | case self::RETURN_TYPE_ID: 498 | case self::RETURN_TYPE_OBJECT: 499 | return $terms; 500 | case self::RETURN_TYPE_LINK: 501 | //Allow plugins to modify 502 | $terms = apply_filters( "term_links-{$field[ self::FIELD_TAXONOMY ]}", $terms ); 503 | return implode( '', $terms ); 504 | } 505 | return false; 506 | } 507 | } 508 | 509 | endif; //class_exists 'ACF_Taxonomy_Field' 510 | 511 | if( !class_exists( 'ACF_Walker_Taxonomy_Field_Checklist' ) ) : 512 | 513 | class ACF_Walker_Taxonomy_Field_Checklist extends Walker { 514 | var $tree_type = 'category'; 515 | var $db_fields = array ( 'parent' => 'parent', 'id' => 'term_id' ); 516 | private $field; 517 | 518 | function __construct( $field ) { 519 | $this->field = $field; 520 | } 521 | 522 | function start_lvl( &$output, $depth = 0, $args = array() ) { 523 | $indent = str_repeat("\t", $depth); 524 | $output .= "$indent\n"; 530 | } 531 | 532 | function start_el( &$output, $category, $depth, $args, $id = 0 ) { 533 | extract($args); 534 | 535 | if ( empty( $taxonomy ) ) 536 | $taxonomy = 'category'; 537 | 538 | if ( $taxonomy == 'category' ) 539 | $name = 'post_category'; 540 | else 541 | $name = $this->field[ ACF_Taxonomy_Field::FIELD_NAME ]; 542 | 543 | $class = in_array( $category->term_id, $popular_cats ) ? ' class="popular-category"' : ''; 544 | $output .= "\n
  • " . ''; 545 | } 546 | 547 | function end_el( &$output, $category, $depth = 0, $args = array() ) { 548 | $output .= "
  • \n"; 549 | } 550 | } 551 | 552 | endif; //class_exists 'ACF_Walker_Taxonomy_Field_Checklist' 553 | 554 | 555 | if( !class_exists( 'ACF_Walker_Taxonomy_Field_List' ) ) : 556 | 557 | class ACF_Walker_Taxonomy_Field_List extends Walker { 558 | var $tree_type = 'category'; 559 | var $db_fields = array ( 'parent' => 'parent', 'id' => 'term_id' ); 560 | private $field; 561 | 562 | function __construct( $field ) { 563 | $this->field = $field; 564 | } 565 | 566 | function start_el( &$output, $object, $depth, $args, $current_object_id = 0 ) { 567 | $output .= ''; 568 | } 569 | } 570 | endif; //class_exists 'ACF_Walker_Taxonomy_Field_List' 571 | 572 | 573 | if( !class_exists( 'ACF_Taxonomy_Field_Helper' ) ) : 574 | 575 | /** 576 | * Advanced Custom Fields - Taxonomy Field Helper 577 | * 578 | * @author Brian Zoetewey 579 | */ 580 | class ACF_Taxonomy_Field_Helper { 581 | /** 582 | * Singleton instance 583 | * @var ACF_Taxonomy_Field_Helper 584 | */ 585 | private static $instance; 586 | 587 | /** 588 | * Returns the ACF_Taxonomy_Field_Helper singleton 589 | * 590 | * $obj = ACF_Taxonomy_Field_Helper::singleton(); 591 | * @return ACF_Taxonomy_Field_Helper 592 | */ 593 | public static function singleton() { 594 | if( !isset( self::$instance ) ) { 595 | $class = __CLASS__; 596 | self::$instance = new $class(); 597 | } 598 | return self::$instance; 599 | } 600 | 601 | /** 602 | * Prevent cloning of the ACF_Taxonomy_Field_Helper object 603 | * @internal 604 | */ 605 | private function __clone() { 606 | } 607 | 608 | /** 609 | * WordPress Localization Text Domain 610 | * 611 | * Used in wordpress localization and translation methods. 612 | * @var string 613 | */ 614 | const L10N_DOMAIN = 'acf-taxonomy-field'; 615 | 616 | /** 617 | * Language directory path 618 | * 619 | * Used to build the path for WordPress localization files. 620 | * @var string 621 | */ 622 | private $lang_dir; 623 | 624 | /** 625 | * Constructor 626 | */ 627 | private function __construct() { 628 | $this->lang_dir = rtrim( dirname( realpath( __FILE__ ) ), '/' ) . '/languages'; 629 | 630 | add_action( 'init', array( &$this, 'register_field' ), 5, 0 ); 631 | add_action( 'init', array( &$this, 'load_textdomain' ), 2, 0 ); 632 | } 633 | 634 | /** 635 | * Registers the Field with Advanced Custom Fields 636 | */ 637 | public function register_field() { 638 | if( function_exists( 'register_field' ) ) { 639 | register_field( 'ACF_Taxonomy_Field', __FILE__ ); 640 | } 641 | } 642 | 643 | /** 644 | * Loads the textdomain for the current locale if it exists 645 | */ 646 | public function load_textdomain() { 647 | $locale = get_locale(); 648 | $mofile = $this->lang_dir . '/' . self::L10N_DOMAIN . '-' . $locale . '.mo'; 649 | load_textdomain( self::L10N_DOMAIN, $mofile ); 650 | } 651 | } 652 | endif; //class_exists 'ACF_Taxonomy_Field_Helper' 653 | 654 | //Instantiate the Addon Helper class 655 | ACF_Taxonomy_Field_Helper::singleton(); --------------------------------------------------------------------------------