Advertisement
vapvarun

Convert hashtags in activity content to clickable spans

Feb 12th, 2025 (edited)
113
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
PHP 4.03 KB | Software | 0 0
  1.  
  2. // Convert hashtags in activity content to clickable spans
  3. function convert_hashtags_to_spans($content) {
  4.     // Remove existing <a> tags around hashtags
  5.     $content = preg_replace('/<a[^>]*>(#\w+)<\/a>/', '$1', $content);
  6.  
  7.     // Convert hashtags to clickable spans
  8.     $content = preg_replace('/#(\w+)/', '<span class="activity-hashtag" data-hashtag="$1">#$1</span>', $content);
  9.  
  10.     return $content;
  11. }
  12. add_filter('bp_get_activity_content_body', 'convert_hashtags_to_spans');
  13.  
  14. // JavaScript for Auto-Submitting the BuddyPress Search Form or Redirecting
  15. add_action('wp_footer', function() {
  16.     ?>
  17.     <style>
  18.         .activity-hashtag, .hashtag-widget {
  19.             cursor: pointer;
  20.             color: #0073aa;
  21.             text-decoration: underline;
  22.         }
  23.  
  24.         .activity-hashtag:hover, .hashtag-widget:hover {
  25.             color: #005177;
  26.             text-decoration: none;
  27.         }
  28.        
  29.         .bpht-hashtags-wrapper.bpht-hashtags-wrapper-cloud > div span {
  30.     display: inline-block;
  31.     padding: 5px 15px;
  32.     line-height: normal;
  33.     text-decoration: none;
  34. }
  35.     </style>
  36.  
  37.     <script>
  38.         document.addEventListener('DOMContentLoaded', function () {
  39.             // Function to convert widget hashtag links to spans
  40.             function convertWidgetHashtags() {
  41.                 const hashtagLinks = document.querySelectorAll('.bpht-widget--hashtags a.hashtag');
  42.                 hashtagLinks.forEach(link => {
  43.                     const hashtag = link.textContent.trim();
  44.                     const dataHashtag = link.id || hashtag.replace('#', '');
  45.  
  46.                     const span = document.createElement('span');
  47.                     span.className = 'hashtag-widget';
  48.                     span.dataset.hashtag = dataHashtag;
  49.                     span.textContent = hashtag;
  50.  
  51.                     link.replaceWith(span);
  52.                 });
  53.             }
  54.  
  55.             convertWidgetHashtags(); // Convert on page load
  56.  
  57.             // Function to fill the form and submit it
  58.             function fillAndSubmitSearchForm(searchQuery) {
  59.                 const searchInput = document.getElementById('dir-activity-search');
  60.                 const searchForm = document.getElementById('dir-activity-search-form');
  61.  
  62.                 if (searchInput && searchForm) {
  63.                     // Set the search value directly
  64.                     searchInput.value = searchQuery;
  65.  
  66.                     // Trigger input and change events to ensure BuddyPress detects changes
  67.                     searchInput.dispatchEvent(new Event('input', { bubbles: true }));
  68.                     searchInput.dispatchEvent(new Event('change', { bubbles: true }));
  69.  
  70.                     // Submit the form
  71.                     searchForm.dispatchEvent(new Event('submit', { bubbles: true }));
  72.                 } else {
  73.                     // Redirect to the Activity page with the hashtag as an anchor
  74.                     const activityPageUrl = '/activity#' + searchQuery.replace('#', '');
  75.                     window.location.href = activityPageUrl;
  76.                 }
  77.             }
  78.  
  79.             // Check if there's a hashtag in the URL fragment on the Activity page
  80.             const hashTagFromURL = window.location.hash.substring(1); // Remove the '#' symbol
  81.             if (hashTagFromURL && document.getElementById('dir-activity-search-form')) {
  82.                 fillAndSubmitSearchForm(`#${hashTagFromURL}`);
  83.             }
  84.  
  85.             // Event delegation for hashtag clicks in both activity content and widgets
  86.             document.body.addEventListener('click', function (event) {
  87.                 const target = event.target.closest('.activity-hashtag, .hashtag-widget');
  88.  
  89.                 if (target) {
  90.                     event.preventDefault();  // Prevent default link behavior
  91.                     const searchQuery = target.dataset.hashtag;
  92.  
  93.                     if (searchQuery) {
  94.                         fillAndSubmitSearchForm(`#${searchQuery}`);
  95.                     }
  96.                 }
  97.             });
  98.         });
  99.     </script>
  100.     <?php
  101. });
  102.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement