Guest User

Untitled

a guest
Oct 22nd, 2018
87
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.27 KB | None | 0 0
  1. /**
  2. * Dynamic Select List for Contact Form 7
  3. * @usage [select name taxonomy:{$taxonomy} ...]
  4. *
  5. * @param Array $tag
  6. *
  7. * @return Array $tag
  8. */
  9. function dynamic_select_list( $tag ) {
  10.  
  11. // Only run on select lists
  12. if( 'select' !== $tag['type'] ) {
  13. return $tag;
  14. } else if ( empty( $tag['options'] ) ) {
  15. return $tag;
  16. }
  17.  
  18. $term_args = array();
  19.  
  20. // Loop thorugh options to look for our custom options
  21. foreach( $tag['options'] as $option ) {
  22.  
  23. $matches = explode( ':', $option );
  24.  
  25. if( ! empty( $matches ) ) {
  26.  
  27. switch( $matches[0] ) {
  28.  
  29. case 'taxonomy':
  30. $term_args['taxonomy'] = $matches[1];
  31. break;
  32.  
  33. case 'parent':
  34. $term_args['parent'] = intval( $matches[1] );
  35. break;
  36.  
  37. }
  38. }
  39.  
  40. }
  41.  
  42. // Ensure we have a term arguments to work with
  43. if( empty( $term_args ) ) {
  44. return $tag;
  45. }
  46.  
  47. // Merge dynamic arguments with static arguments
  48. $term_args = array_merge( $term_args, array(
  49. 'hide_empty' => false,
  50. ) );
  51.  
  52. $terms = get_terms( $term_args );
  53.  
  54. // Add terms to values
  55. if( ! empty( $terms ) && ! is_wp_error( $term_args ) ) {
  56.  
  57. foreach( $terms as $term ) {
  58.  
  59. $tag['values'][] = $term->name;
  60.  
  61. }
  62.  
  63. }
  64.  
  65. return $tag;
  66.  
  67. }
  68. add_filter( 'wpcf7_form_tag', 'dynamic_select_list', 10 );
  69.  
  70. /** Dynamic List for Contact Form 7 **/
  71. /** Usage: [select name term:taxonomy_name] **/
  72. function dynamic_select_list($tag, $unused){
  73. $options = (array)$tag['options'];
  74.  
  75. foreach ($options as $option)
  76. if (preg_match('%^term:([-0-9a-zA-Z_]+)$%', $option, $matches))
  77. $term = $matches[1];
  78.  
  79. //check if post_type is set
  80. if(!isset($term))
  81. return $tag;
  82.  
  83. $taxonomy = get_terms($term, array('hide_empty' => 0));
  84.  
  85. if (!$taxonomy)
  86. return $tag;
  87.  
  88. foreach ($taxonomy as $cat) {
  89. $tag['raw_values'][] = $cat->name;
  90. $tag['values'][] = $cat->name;
  91. $tag['labels'][] = $cat->name;
  92. }
  93.  
  94. $tag['raw_values'][] = 'Other';
  95. $tag['values'][] = 'Other';
  96. $tag['labels'][] = 'Other - Please Specify Below';
  97.  
  98. return $tag;
  99. }
  100. add_filter( 'wpcf7_form_tag', 'dynamic_select_list', 10, 2);
Add Comment
Please, Sign In to add comment