| 1 | <?php |
|---|
| 2 | add_filter( 'get_terms', __NAMESPACE__ . '\add_descriptions_to_tag_search', 10, 4 ); |
|---|
| 3 | add_filter( 'get_terms_args', __NAMESPACE__ . '\get_terms_args', 10, 2 ); |
|---|
| 4 | add_action( 'pre_post_tax_input', __NAMESPACE__ . '\pre_post_tax_input', 10, 2 ); |
|---|
| 5 | |
|---|
| 6 | /** |
|---|
| 7 | * Change the tags from being just the tag name to id + name + description. |
|---|
| 8 | * Must be used with the get_terms_args filter. |
|---|
| 9 | * |
|---|
| 10 | * @param $terms |
|---|
| 11 | * @param $taxonomies |
|---|
| 12 | * @param $args |
|---|
| 13 | * @param $term_query |
|---|
| 14 | * |
|---|
| 15 | * @return array of strings for dropdown. |
|---|
| 16 | */ |
|---|
| 17 | function add_descriptions_to_tag_search( $terms, $taxonomies, $args, $term_query) { |
|---|
| 18 | if ( ! ( is_tag_search() && 'post_tag' === $taxonomies[0] ) ) { |
|---|
| 19 | return $terms; |
|---|
| 20 | } |
|---|
| 21 | |
|---|
| 22 | $term_strings = array(); |
|---|
| 23 | foreach( $terms as $term ) { |
|---|
| 24 | $term_string = "<span style='display:none;'>$term->term_id:</span><strong>$term->name </strong>"; |
|---|
| 25 | if ( $term->description ) { |
|---|
| 26 | $term_string .= "<br /><em>$term->description</em>"; |
|---|
| 27 | } |
|---|
| 28 | |
|---|
| 29 | $term_strings[] = $term_string; |
|---|
| 30 | } |
|---|
| 31 | } |
|---|
| 32 | |
|---|
| 33 | /** |
|---|
| 34 | * For a tag search post_tag get_terms call, override the behaviour from $args['fields'] = 'name' to 'all', |
|---|
| 35 | * must be combined with @see add_descriptions_to_tag_search() above to work correctly. |
|---|
| 36 | * @param $args |
|---|
| 37 | * @param $taxonomies |
|---|
| 38 | * |
|---|
| 39 | * @return mixed |
|---|
| 40 | */ |
|---|
| 41 | function get_terms_args( $args, $taxonomies ) { |
|---|
| 42 | if ( ! ( is_tag_search() && 'post_tag' === $taxonomies[0] ) ) { |
|---|
| 43 | return $args; |
|---|
| 44 | } |
|---|
| 45 | $args['fields'] = 'all'; |
|---|
| 46 | return $args; |
|---|
| 47 | } |
|---|
| 48 | |
|---|
| 49 | function pre_post_tax_input( $tax_input ) { |
|---|
| 50 | if ( defined( 'DOING_CRON' ) || ( defined('WP_CLI') && WP_CLI ) ) { |
|---|
| 51 | return; |
|---|
| 52 | } |
|---|
| 53 | |
|---|
| 54 | |
|---|
| 55 | if ( isset( $tax_input['post_tag'] ) ) { |
|---|
| 56 | $incoming_terms = $tax_input['post_tag']; |
|---|
| 57 | |
|---|
| 58 | foreach ( $incoming_terms as $term ) { |
|---|
| 59 | // If the term already exists, WP sends just the ID. If it is new, the term name is passed. |
|---|
| 60 | if ( ! is_int( $term ) ) { |
|---|
| 61 | $tag_id = absint( strtok( $term, ':') ); |
|---|
| 62 | if ( 0 !== $tag_id ) { |
|---|
| 63 | $updated_terms[] = $tag_id; // We just use the ID from the tag dropdown |
|---|
| 64 | } else { |
|---|
| 65 | $updated_terms[] = $term; // For anywhere we haven't overriddent the default behaviour. |
|---|
| 66 | } |
|---|
| 67 | } else { |
|---|
| 68 | $updated_terms[] = $term; |
|---|
| 69 | } |
|---|
| 70 | } |
|---|
| 71 | |
|---|
| 72 | $tax_input['post_tag'] = $updated_terms; |
|---|
| 73 | } |
|---|
| 74 | |
|---|
| 75 | return $tax_input; |
|---|
| 76 | } |
|---|