rAthus

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

Apr 1st, 2022 (edited)
183
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. <?php
  2.  
  3. /*************************************************************************************\
  4. |* Links obuscation - 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 "deobfucate_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. add_action('wp_loaded', 'buffer_start');
  14. function buffer_start() {
  15.     ob_start('akn_ofbuscate_buffer');
  16. }
  17. add_action('shutdown', 'buffer_end');
  18. function buffer_end() {
  19.     ob_end_flush();
  20. }
  21. function akn_ofbuscate_buffer($buffer) {
  22.     $result = preg_replace_callback('#<a[^>]+(href=(\"|\')([^\"\']*)(\'|\")[^>]+class=(\"|\')[^\'\"]*obfuscate[^\'\"]*(\"|\')|class=(\"|\')[^\'\"]*obfuscate[^\'\"]*(\"|\')[^>]+href=(\"|\')([^\"\']*)(\'|\"))[^>]*>(.+(?!<a))<\/a>#imUs', function($matches) {
  23.         preg_match('#<a[^>]+class=[\"|\\\']([^\\\'\"]+)[\"|\\\']#imUs',$matches[0],$matches_classes);
  24.         $classes = trim(preg_replace('/\s+/',' ',str_replace('obfuscate','',$matches_classes[1])));
  25.         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>';
  26.     }, $buffer);
  27.     return $result;
  28. }
  29. add_action('wp_footer', 'akn_ofbuscate_footer_js');
  30. function akn_ofbuscate_footer_js() {
  31.     ?>
  32.         <script>
  33.             jQuery(document).ready(function($) {
  34.                 // options you can change
  35.                 var deobfuscate_on_right_click = true;
  36.                 // function to open link on click
  37.                 function akn_ofbuscate_clicked($el,force_blank) {
  38.                     if (typeof(force_blank)=='undefined')
  39.                         var force_blank = false;
  40.                     var link = atob($el.data('o'));
  41.                     var _blank = $el.data('b');
  42.                     if (_blank || force_blank)
  43.                         window.open(link);
  44.                     else
  45.                         location.href = link;
  46.                 }
  47.                 // trigger link opening on click
  48.                 $(document).on('click','.akn-obf-link',function() {
  49.                     var $el = $(this);
  50.                     if (!$el.closest('.akn-deobf-link').length)
  51.                         akn_ofbuscate_clicked($el);
  52.                 });
  53.                 // trigger link openin in new tab on mousewheel click
  54.                 $(document).on('mousedown','.akn-obf-link',function(e) {
  55.                     if (e.which==2) {
  56.                         var $el = $(this);
  57.                         if (!$el.closest('.akn-deobf-link').length) {
  58.                             akn_ofbuscate_clicked($el,true);
  59.                             return true;
  60.                         }
  61.                     }
  62.                 });
  63.                 // deobfuscate link on right click so the context menu is a legit menu with link options
  64.                 $(document).on('contextmenu','.akn-obf-link',function(e) {
  65.                     if (deobfuscate_on_right_click) {
  66.                         var $el = $(this);
  67.                         if (!$el.closest('.akn-deobf-link').length) {
  68.                             e.stopPropagation();
  69.                             var link = atob($el.data('o'));
  70.                             var _blank = $el.data('b');
  71.                             $el.wrap('<a class="akn-deobf-link" href="'+link+'"'+(_blank?' target="_BLANK"':'')+'></a>').parent().trigger('contextmenu');
  72.                             setTimeout(function() {
  73.                                 $el.unwrap();
  74.                             },10);
  75.                         }
  76.                     }
  77.                 });
  78.             });
  79.         </script>
  80.     <?php
  81. }
  82.  
RAW Paste Data Copied