Advertisement
geminilabs

Untitled

Jul 31st, 2024 (edited)
183
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
PHP 0.99 KB | None | 0 0
  1. /**
  2.  * Automatically create categories for chatbots based on URL query
  3.  *
  4.  * Use like this: [site_reviews assigned_terms="chatbot_id"]
  5.  *
  6.  * @param int $ttid
  7.  * @param string|int $term
  8.  * @param string $taxonomy
  9.  *
  10.  * @return int
  11.  */
  12. add_filter('site-reviews/assigned_terms/term_id', function ($ttid, $term, $taxonomy) {
  13.     if (!empty($ttid) || 'chatbot_id' !== $term) {
  14.         return $ttid;
  15.     }
  16.     $url = filter_input(INPUT_SERVER, 'REQUEST_URI');
  17.     $path = wp_parse_url($url, PHP_URL_PATH);
  18.     $query = wp_parse_args(wp_parse_url($url, PHP_URL_QUERY));
  19.     if ('/chat/' !== $path) {
  20.         return $ttid;
  21.     }
  22.     $chatbot = $query['id'] ?? '';
  23.     if (!str_starts_with($chatbot, 'chatbot-') || 'chatbot-' === $chatbot) {
  24.         return $ttid;
  25.     }
  26.     require_once ABSPATH.'wp-admin/includes/taxonomy.php';
  27.     $term = wp_create_term($chatbot, $taxonomy);
  28.     if (is_wp_error($term)) {
  29.         return $ttid;
  30.     }
  31.     return $term['term_taxonomy_id'] ?? 0;
  32. }, 10, 3);
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement