Problem: I have article with links to its original site and some external links to other sites. I want to remove links base on some condition/input, Like if there is link with google.com, it should be removed. I tried fol code but it removes all links.
<script>
$('#content a').each(function() {
$(this).replaceWith($(this).text());
});
</script>
I need code which work on my input. I will use it in WordPress. Thanks for time and effort.
Please use following Code sample
jQuery(document).ready(function(){
jQuery('#content a').each(function() {
if(jQuery(this).attr('href') == "http://optimumcreative.com/blog"){
//After this condition, it will only remove those links who have href value equals to http://optimumcreative.com/blog
jQuery(this).replaceWith(jQuery(this).text());
}
});
});;
<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>
<div id="content">
<div class="entry-content">
This is some text with <a href="http://optimumcreative.com/blog">links</a>. Some more text with <a href="http://stackoverflow.com">links </a>.
</div>
</div>
You will need to put your jQuery code inside document.ready block, so it executes when page is loaded. You can use wp_footer action to load this script as follows
function show_code_footer() {
echo '<script>
jQuery(document).ready(function(){
jQuery("#content a").each(function() {
jQuery(this).replaceWith(jQuery(this).text());
});
});
</script>';
}
add_action( 'wp_footer', 'show_code_footer' );
</div>