I'm trying to remove the http, https and www from the links that are displayed in each wordpress post.
Turn https://www.example.com/
into example.com
Each link has a unique Id but grouped into classes. The code I currently have removes the http & www from the link if I target it directly but if I switch from getElementByID to getElementsByClassName it just stops working.
I've also tried various methods such as altering the code to Jquery and other targeting methods.
Any help would be much appreicated.
<html>
<a id="link101" class="group1" href="https://www.example1.com/">https://www.example1.com/</a>
<a id="link207" class="group1" href="https://www.example2.com/">https://www.example2.com/</a>
<a id="link55" class="group1" href="https://www.example3.com/">https://www.example3.com/</a>
<a id="link78" class="group2" href="https://www.example4.com/">https://www.example4.com/</a>
<a id="link180" class="group2" href="https://www.example5.com/">https://www.example5.com/</a>
<a id="link166" class="group2" href="https://www.example6.com/">https://www.example6.com/</a>
<a id="link120" class="group3" href="https://www.example7.com/">https://www.example7.com/</a>
<a id="link124" class="group3" href="https://www.example8.com/">https://www.example8.com/</a>
<a id="link310" class="group3" href="https://www.example9.com/">https://www.example9.com/</a>
</html>
Working Code below, but only on the targeted ID. I would have to repeat the script for every instance i'm looking to clean the displayed urls.
<script src="https://ajax.googleapis.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>
<Script>
$(document).ready(function removeFunction() {
var str = document.getElementById("link120").innerHTML;
var res = str.replace(/^(?:https?:\/\/)?(?:www\.)?/i, "");
document.getElementById("link120").innerHTML = res;
});
$(document).ready(function removeFunction() {
var str = document.getElementById("link124").innerHTML;
var res = str.replace(/^(?:https?:\/\/)?(?:www\.)?/i, "");
document.getElementById("link124").innerHTML = res;
});
$(document).ready(function removeFunction() {
var str = document.getElementById("link310").innerHTML;
var res = str.replace(/^(?:https?:\/\/)?(?:www\.)?/i, "");
document.getElementById("link310").innerHTML = res;
});
</Script>
My attempt at a solution but the code does not work when trying to apply the function to more than one element by targeting the class name:
Attempt #1:
<script src="https://ajax.googleapis.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>
<Script>
$(document).ready(function removeFunction() {
var str = document.getElementsByClassName("group3").innerHTML;
var res = str.replace(/^(?:https?:\/\/)?(?:www\.)?/i, "");
document.getElementsByClassName("group3").innerHTML = res;
});
</Script>
Attempt #2:
<Script>
$(document).ready(function removeFunction() {
var str = $('a[id ^= "link_"]').html();
var res = str.replaceWith(/^(?:https?:\/\/)?(?:www\.)?/i, "");
$('a[id ^= "link_"]').html() = res;
});
</Script>
ANSWER:
I was able to get the method to work through the below method:
<Script>
$(document).ready(function removeFunction() {
let post_id = '<?php global $post; echo $post->ID; ?>';
var str = document.getElementById("link" + post_id).innerHTML;
var res = str.replace(/^(?:https?:\/\/)?(?:www\.)?/i, "");
document.getElementById("link" + post_id).innerHTML = res;
});
</Script>