rAthus

[WordPress] [SEO] obfuscate links by adding a class to any <a> element

Apr 1st, 2022 (edited)
785
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
PHP 4.26 KB | None | 0 0
  1. <?php
  2.  
  3. /**************************************************************************************\
  4. |* Links obfuscation - add class "obfuscate" to any <a> element to obfuscate its link *|
  5. \**************************************************************************************/
  6.  
  7. // Add this code to your child theme's functions.php file, then just add the class "obfuscate" to any <a> element to obfuscate its link by replacing it with a <span> element with no readable link.
  8. // The obfuscated elements inherits the original <a> element's classes, along with a "akn-obf-link" class, so you might need to add CSS to style the "akn-obf-link" class so that it looks like a link to the visitor, maybe at least to add a cursor:pointer.
  9. // On right click, the obfuscated link will be wrapped with a proper <a> element with the "akn-deobf-link" for a brief moment, so that a proper context menu appears, you can remove that behaviour by setting the "deobfuscate_on_right_click" option to false in the code bellow.
  10.  
  11. // Edit 2022-04-05 - modified regex to allow for html elements and new lines into the <a> element, modified callback so the obfuscated element inherits the original link's classes, modified JS to add mousewheel click and right click options.
  12.  
  13. // Edit 2023-01-26 - greatly improved regex thanks to @MadMaxInfinity on Stack Overflow, it now both allows more matches in different scenarios and returns less false positives matches, more infos on his answer here: https://stackoverflow.com/a/75234749/2498324
  14.  
  15. // Edit 2023-02-08 - improved class regex thanks to @MadMaxInfinity on Stack Overflow again.
  16.  
  17. add_action('wp_loaded', 'buffer_start');
  18. function buffer_start() {
  19.     ob_start('akn_ofbuscate_buffer');
  20. }
  21. add_action('shutdown', 'buffer_end');
  22. function buffer_end() {
  23.     ob_end_flush();
  24. }
  25. function akn_ofbuscate_buffer($buffer) {
  26.     $result = preg_replace_callback('#<a[^>]+((?<=\s)href=(\"|\')([^\"\']*)(\'|\")[^>]+(?<=\s)class=(\"|\')[^\'\"]*(?<!\w|-)obfuscate(?!\w|-)[^\'\"]*(\"|\')|(?<=\s)class=(\"|\')[^\'\"]*(?<!\w|-)obfuscate(?!\w|-)[^\'\"]*(\"|\')[^>]+(?<=\s)href=(\"|\')([^\"\']*)(\'|\"))[^>]*>(.*)<\/a>#miUs', function($matches) {
  27.         preg_match('#<a[^>]+(?<=\s)class=[\"|\\\']([^\\\'\"]+)[\"|\\\']#imUs',$matches[0],$matches_classes);
  28.         $classes = trim(preg_replace('/\s+/',' ',str_replace('obfuscate','',$matches_classes[1])));
  29.         return '<span class="akn-obf-link'.($classes?' '.$classes:'').'" data-o="'.base64_encode($matches[3]?:$matches[10]).'" data-b="'.((strpos(strtolower($matches[0]),'_blank')!==false)?'1':'0').'">'.$matches[12].'</span>';
  30.     }, $buffer);
  31.     return $result;
  32. }
  33. add_action('wp_footer', 'akn_ofbuscate_footer_js');
  34. function akn_ofbuscate_footer_js() {
  35.     ?>
  36.         <script>
  37.             jQuery(document).ready(function($) {
  38.                 // options you can change
  39.                 var deobfuscate_on_right_click = true;
  40.                 // function to open link on click
  41.                 function akn_ofbuscate_clicked($el,force_blank) {
  42.                     if (typeof(force_blank)=='undefined')
  43.                         var force_blank = false;
  44.                     var link = atob($el.data('o'));
  45.                     var _blank = $el.data('b');
  46.                     if (_blank || force_blank)
  47.                         window.open(link);
  48.                     else
  49.                         location.href = link;
  50.                 }
  51.                 // trigger link opening on click
  52.                 $(document).on('click','.akn-obf-link',function() {
  53.                     var $el = $(this);
  54.                     if (!$el.closest('.akn-deobf-link').length)
  55.                         akn_ofbuscate_clicked($el);
  56.                 });
  57.                 // trigger link openin in new tab on mousewheel click
  58.                 $(document).on('mousedown','.akn-obf-link',function(e) {
  59.                     if (e.which==2) {
  60.                         var $el = $(this);
  61.                         if (!$el.closest('.akn-deobf-link').length) {
  62.                             akn_ofbuscate_clicked($el,true);
  63.                             return true;
  64.                         }
  65.                     }
  66.                 });
  67.                 // deobfuscate link on right click so the context menu is a legit menu with link options
  68.                 $(document).on('contextmenu','.akn-obf-link',function(e) {
  69.                     if (deobfuscate_on_right_click) {
  70.                         var $el = $(this);
  71.                         if (!$el.closest('.akn-deobf-link').length) {
  72.                             e.stopPropagation();
  73.                             var link = atob($el.data('o'));
  74.                             var _blank = $el.data('b');
  75.                             $el.wrap('<a class="akn-deobf-link" href="'+link+'"'+(_blank?' target="_BLANK"':'')+'></a>').parent().trigger('contextmenu');
  76.                             setTimeout(function() {
  77.                                 $el.unwrap();
  78.                             },10);
  79.                         }
  80.                     }
  81.                 });
  82.             });
  83.         </script>
  84.     <?php
  85. }
  86.  
Add Comment
Please, Sign In to add comment