1

I am currently trying to brain storm on what would be the best solution to find and replace a certain string throughout an entire website. This is a very large (WordPress) website so manually replacing things would be out of the question.

Essentially, my client has been in business for approx 65 years, throughout the website it is said that they have been in business "for over 60 years". Instead of it saying this I would like for it to automatically get the amount of years they have been in business and replace "over 60 years" with "* years in business* years".

I wrote this simple jQuery code that I know calculates the years they have been in business given the current year and that they were established in 1953.

var yearsActive = (new Date()).getFullYear() - 1953;

I did a little bit on research on search and replacing using jQuery and from what I understand, it is not recommended? Is this correct? Does anybody have any ideas as to how I might want to go about this problem?

Edit: I probably should have mentioned that I do have a solution that I am currently considering temporary, as I mentioned I was reading that such a solution might not be ideal.

$(document).ready(function() {
    var yearsAct = (new Date()).getFullYear() - 1953;
    var yearsActStr = yearsAct + " years";

    var replaced = $('body').html().replace(/over 60 years/g, yearsActStr);
    $('body').html(replaced);
});
Leo C
  • 93
  • 1
  • 1
  • 9
  • I assume you don't have terminal access, you could download a copy of the theme, open a code editor and search and replace. – Adam May 30 '18 at 17:35
  • 1
    Can't you just use your text editor to find all and replace? – pizzarob May 30 '18 at 17:35
  • This is a WordPress website as I mentioned so I don't think I be able to just use a text editor for this since all the posts and pages are stored in the DB. I know I could simply use a WP plugin for this but I am looking to make sure that the "years active" is obtained automatically since I don't want to have to repeat the find and replace process every new year. – Leo C May 30 '18 at 17:42

1 Answers1

0

Possible duplicate of:

Replace text in HTML page with jQuery

If you want a vanilla solution - a blog post in the response give you one.

Link for the lazy: http://blog.alexanderdickson.com/javascript-replacing-text

--- EDIT BASED ON COMMENT ---

You can try this:

  $('body :not(script)').contents().filter(function() {
    return this.nodeType === 3;
  }).replaceWith(function() {
      return this.nodeValue.replace('old-date-year','DD/MM/YYYY');
  });

(tested and worked on basic HTML elements - you will need to run a more indepth test. Also consider running this within some sort of document load compelted routine - if you are using Ajax - run after all Ajax routines have finished.)

alpharomeo
  • 418
  • 5
  • 13
  • That's essentially where I got the inspiration for my temporary solution (see edit). But as I mentioned, I have been reading that this could possibly cause issues. – Leo C May 30 '18 at 18:12