0

I have a wordpress website which contains over 900 posts. I am affiliate with a number of companies however they have since changed the URL.

How can I detect a URL say www.abc.com.au and change it to www.xyz.com.au when they are <a target="_blank">?

hjpotter92
  • 78,589
  • 36
  • 144
  • 183
Ben Danyi
  • 35
  • 5
  • possible duplicate of [Change URL and redirect using jQuery](http://stackoverflow.com/questions/846954/change-url-and-redirect-using-jquery) – Cody Guldner Jul 28 '13 at 03:21
  • 2
    any reason why this is being done in JavaScript and not within the PHP side of things? Do your affiliates not care about search engines (I feel like they would)? – Cameron Jul 28 '13 at 04:09
  • 1
    JavaScript is an inappropriate tool for this task; anyone with JavaScript disabled will get dead links, and as @bonesbrigade notes, it will likely harm search engine rankings as well (since most crawlers don't run JavaScript). You'd be better off doing some batch change on your database. – icktoofay Jul 28 '13 at 05:23

2 Answers2

0

Use Jquery:

('a').each(function( ) {
  if($(this).attr("target") == "_blank" && $(this).attr("href") == "www.abc.com.au")
    $(this).attr("href", "www.xyz.com.au");
});

I did not test my code.

You can also use modrewrite and .htaccess.

-1

If you need to edit the links in your own posts it might be worth it to see if there is a wordpress bulk-update plugin that suites your needs, but in JavaScript on the page you can do something like

$('a[target="_blank"]').each(function() {
    var href = $(this).attr('href');
    if (href.indexOf('www.abc.com.au') > -1) {
        $(this).attr('href', href.replace('www.abc.com.au', 'www.xyz.com.au'));
    }
});

which finds all a tags with target _blank and then updates the href if it contains the original domain by replacing that string with the new one.

lemieuxster
  • 1,081
  • 7
  • 14