element to obfuscate its link *| \**************************************************************************************/ // Add this code to your child theme's functions.php file, then just add the class "obfuscate" to any element to obfuscate its link by replacing it with a element with no readable link. // The obfuscated elements inherits the original 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. // On right click, the obfuscated link will be wrapped with a proper 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. // Edit 2022-04-05 - modified regex to allow for html elements and new lines into the element, modified callback so the obfuscated element inherits the original link's classes, modified JS to add mousewheel click and right click options. // 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 // Edit 2023-02-08 - improved class regex thanks to @MadMaxInfinity on Stack Overflow again. add_action('wp_loaded', 'buffer_start'); function buffer_start() { ob_start('akn_ofbuscate_buffer'); } add_action('shutdown', 'buffer_end'); function buffer_end() { ob_end_flush(); } function akn_ofbuscate_buffer($buffer) { $result = preg_replace_callback('#]+((?<=\s)href=(\"|\')([^\"\']*)(\'|\")[^>]+(?<=\s)class=(\"|\')[^\'\"]*(?]+(?<=\s)href=(\"|\')([^\"\']*)(\'|\"))[^>]*>(.*)<\/a>#miUs', function($matches) { preg_match('#]+(?<=\s)class=[\"|\\\']([^\\\'\"]+)[\"|\\\']#imUs',$matches[0],$matches_classes); $classes = trim(preg_replace('/\s+/',' ',str_replace('obfuscate','',$matches_classes[1]))); return ''.$matches[12].''; }, $buffer); return $result; } add_action('wp_footer', 'akn_ofbuscate_footer_js'); function akn_ofbuscate_footer_js() { ?>