Advertisement
Guest User

bbpress auto tags (update)

a guest
Feb 5th, 2014
169
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
PHP 1.59 KB | None | 0 0
  1. <?php
  2. /***
  3.     * Plugin Name: bbPress AutoTagger
  4.     * Plugin URI:  http://example.org/
  5.     * Description: --- description here --
  6.     * Version:     1.0
  7.     * Author:      Shmoo
  8.     * Author URI:  http://example.org/
  9.     * License:     Same as WordPress I guess.
  10. ***/
  11.  
  12. add_action( 'bbp_new_topic',  'shmoo_auto_save_tags', 10, 1 );
  13. add_action( 'bbp_edit_topic', 'shmoo_auto_save_tags', 10, 1 );
  14.  
  15.  
  16. function shmoo_auto_save_tags( $topic_id ) {
  17.  
  18.     $topic_id = 0;
  19.  
  20. // Topic Tags
  21.  
  22.  
  23.         // Get all bbPress topic tags inside an array + strip them to plain text
  24.         $bbp_topic_tags = wp_tag_cloud( array( 'format' => 'array', 'taxonomy' => 'topic-tag' ) );
  25.  
  26.                 function tag_stripper( &$items, $format ) {
  27.  
  28.                         $items = strip_tags( $items );
  29.  
  30.                 }
  31.                 array_walk( $bbp_topic_tags, 'tag_stripper' );
  32.  
  33.  
  34. // Topic Content
  35.  
  36.  
  37.         // Get the topic content
  38.         $bbp_topic_content = bbp_get_topic_content();
  39.  
  40.  
  41. // Match Topics Tags against Topic Content
  42.  
  43.  
  44.         // Check and echo matches
  45.         $matches = array();
  46.  
  47.         $match_found = preg_match_all( "/\b(" . implode($bbp_topic_tags,"|") . ")\b/i", $bbp_topic_content, $matches );
  48.  
  49.         if ( $match_found ) {
  50.  
  51.             $words = array_unique( $matches[0] );
  52.                 foreach( $words as $word ) {
  53.  
  54.                     $words = array_unique( array_merge( $words, wp_get_post_terms( $topic_id, bbp_get_topic_tag_tax_id(), array( 'fields' => 'names' ) ) ) );
  55.                         if ( !empty( $words ) )
  56.                             wp_set_post_terms( $topic_id, $words, bbp_get_topic_tag_tax_id(), true );
  57.  
  58.                 }
  59.  
  60.         }
  61.  
  62.  
  63. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement